Skip to content

Instantly share code, notes, and snippets.

@patricklucas
Created January 30, 2012 16:43
Show Gist options
  • Save patricklucas/1705354 to your computer and use it in GitHub Desktop.
Save patricklucas/1705354 to your computer and use it in GitHub Desktop.
import string
import sys
def perms(s):
letter_map = [c in string.letters for c in s]
num_letters = sum(letter_map)
num_perms = 2**num_letters
for perm in xrange(num_perms):
letter_perms = [
c.swapcase() if (1 << i & perm) else c
for i, c in enumerate(c for c in reversed(s) if c in string.letters)
]
yield ''.join(
letter_perms.pop() if is_letter else s[i]
for i, is_letter in enumerate(letter_map)
)
for perm in perms(sys.argv[1]):
print perm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment