-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathwordlist.py
executable file
·31 lines (26 loc) · 931 Bytes
/
wordlist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python
import random
import sys
def find_words(node, available, parents=None):
if parents is None:
parents = []
if '_leaf' in node:
yield ''.join(parents)
for char in (i for i in set(available) if i in node):
remaining = available[:]
remaining.remove(char)
for word in find_words(node[char], remaining, parents + [char]):
yield word
def read_words(filename):
root = {}
with open(filename) as f:
for line in (line.rstrip().lower() for line in f.readlines()):
node = root
for char in line:
node = node.setdefault(char, {})
node['_leaf'] = True
return root
available_characters = list(sys.argv[1])
length_comparator = lambda x, y: cmp(len(x), len(y))
words = sorted(find_words(read_words('/usr/share/dict/words'), available_characters), length_comparator)
print ' '.join(words)