Skip to content

Instantly share code, notes, and snippets.

@mooreniemi
Created January 8, 2021 21:54
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 mooreniemi/5325752949e0d48745e22d22e4282506 to your computer and use it in GitHub Desktop.
Save mooreniemi/5325752949e0d48745e22d22e4282506 to your computer and use it in GitHub Desktop.
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