Created
January 8, 2021 21:54
-
-
Save mooreniemi/5325752949e0d48745e22d22e4282506 to your computer and use it in GitHub Desktop.
This file contains 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
import math | |
def permutations(to_go, path_taken='', paths=[]): | |
if not to_go: | |
print(path_taken) | |
paths.append(path_taken) | |
else: | |
for i in range(len(to_go)): | |
here = to_go[i] | |
elsewhere = to_go[:i] + to_go[i+1:] | |
permutations(elsewhere, path_taken + here) | |
return paths | |
s = "abcd" | |
p = permutations(s) | |
assert len(p) == math.factorial(len(s)), f"There should be {len(s)}! permutations" | |
print(f"Found {len(p)} permutations of '{s}'") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment