forked from charlesreid1/five-letter-words
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff_by_one_fixed.py
84 lines (68 loc) · 2.31 KB
/
diff_by_one_fixed.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
diff_by_one.py
Donald Knuth, Art of Computer Programming, Volume 4 Facsimile 0
Exercise #28
Find pairs of SGB word vectors that differ by +/-1 in each component.
"""
from get_words import get_words
def gen_variations(word,fragment,depth,variations):
"""
Recursive backtracking method to assemble strings
differing by +/-1 at each position
"""
if depth==5:
variations.add(fragment)
else:
# make a choice
fragment_p1 = fragment + chr(ord(word[depth])+1)
fragment_m1 = fragment + chr(ord(word[depth])-1)
for new_fragment in [fragment_p1,fragment_m1]:
gen_variations(word,new_fragment,depth+1,variations)
def get_all_variations(word):
"""
Return all possible words that differ
from `word` by +/-1 in each index.
This does not include `word` in the
variations.
"""
word_variations = set()
gen_variations(word,'',0,word_variations)
word_variations = list(word_variations)
return word_variations
def main():
"""
Find pairs of SGB word vectors that differ by +/-1 in each component.
To do this, iterate through each word,
generate the 32 possible candidate matchings,
and if they exist, add the pair to a set.
"""
# words is a hash table (unsorted)
words = get_words()
#words = words[:1000]
words = set(get_words())
# List of string tuples
off_by_one = set()
# Iterate over every word
for iw,word in enumerate(words):
# Generate all 2^5 = 32 possible candidate
# matches for this word (total 185k strings)
all_vars = get_all_variations(word)
for word_var in all_vars:
# O(1) check if this variation exists
# in the five letter words set
if word_var in words:
# Found a new (unordered) pair
if word<word_var:
left=word
right=word_var
else:
left=word_var
right=word
off_by_one.add((left,right))
off_by_one = list(off_by_one)
off_by_one.sort()
for o in off_by_one:
print("{:s} {:s}".format(o[0],o[1]))
print("Found {0:d} pairs of words that differ by +/-1 in each component.".format(len(off_by_one)))
if __name__=="__main__":
main()