Skip to content

Instantly share code, notes, and snippets.

@alanwsmith
Created November 16, 2022 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanwsmith/5f586414bc8a2022ee4ebae01a8d27b6 to your computer and use it in GitHub Desktop.
Save alanwsmith/5f586414bc8a2022ee4ebae01a8d27b6 to your computer and use it in GitHub Desktop.
This was an attempt at finding permutations for an array/list. It works for 4 items, but that's it. Next stop: Heap's algorithm
def perms(data):
return_data = []
counter = 1
for x in range(1, len(data) + 1):
counter *= x
shapes = [
[0, 1, 2, 3],
[0, 1, 3, 2],
[0, 2, 1, 3],
[0, 2, 3, 1],
[0, 3, 1, 2],
[0, 3, 2, 1],
]
for count in range(0, counter):
mod = count % len(data)
div = int(count/ len(data))
item = []
for slot in range(0, len(data)):
item.append(
data[shapes[div][(slot + mod) % len(data)]]
)
return_data.append(item)
return return_data
results = perms(['a', 'b', 'c', 'd'])
for result in results:
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment