Skip to content

Instantly share code, notes, and snippets.

@shawnma
Created October 23, 2018 03:16
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 shawnma/862448cc3d98fec292230b3fe39bf779 to your computer and use it in GitHub Desktop.
Save shawnma/862448cc3d98fec292230b3fe39bf779 to your computer and use it in GitHub Desktop.
permutations of words
import copy, sys
words = set()
def load():
for f in open('words.txt').readlines():
words.add(f.strip())
#
# sr - string
# all - output set
# remaining - reminaing array
def perm(sr, all, remaining):
for c in remaining:
nw = sr + c
if (len(nw)>2):
all.add(nw)
p = copy.copy(remaining)
p.remove(c)
perm(nw, all, p)
s = set()
load()
perm('', s, [f for f in sys.argv[1]])
for f in s.intersection(words): print f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment