Created
November 16, 2022 16:23
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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