Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
6894d8ed37 | |||
0ed76ea02b | |||
9a23a47211 | |||
279066d064 | |||
c8960d6015 |
11
diff_by_n.py
11
diff_by_n.py
@ -9,12 +9,13 @@ Find pairs of SGB word vectors that differ by +/-n.
|
||||
from get_words import get_words
|
||||
import timeit
|
||||
|
||||
|
||||
def gen_variations(word,fragment,distance,depth,variations):
|
||||
"""
|
||||
Recursive backtracking method to assemble strings
|
||||
differing by +/-distance at each position
|
||||
"""
|
||||
if depth==5:
|
||||
if depth==len(word):
|
||||
variations.add(fragment)
|
||||
else:
|
||||
for d in range(1,distance+1):
|
||||
@ -24,15 +25,15 @@ def gen_variations(word,fragment,distance,depth,variations):
|
||||
gen_variations(word,new_fragment,distance,depth+1,variations)
|
||||
|
||||
|
||||
def get_all_variations(word,d):
|
||||
def get_all_variations(word, distance):
|
||||
"""
|
||||
Return all possible words that differ
|
||||
from `word` by +/-d in each index.
|
||||
from `word` by +/-distance in each index.
|
||||
This does not include `word` in the
|
||||
variations.
|
||||
"""
|
||||
word_variations = set()
|
||||
gen_variations(word,'',d,0,word_variations)
|
||||
gen_variations(word,'',distance,0,word_variations)
|
||||
|
||||
word_variations = list(word_variations)
|
||||
return word_variations
|
||||
@ -41,7 +42,7 @@ def get_all_variations(word,d):
|
||||
def main():
|
||||
"""
|
||||
Find pairs of SGB word vectors that differ by
|
||||
+/-d in each component.
|
||||
+/-distance in each component.
|
||||
|
||||
To do this, iterate through each word,
|
||||
generate the possible candidate matchings,
|
||||
|
@ -1,84 +0,0 @@
|
||||
"""
|
||||
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()
|
@ -1,5 +1,5 @@
|
||||
"""
|
||||
word_coverage.py
|
||||
letter_coverage.py
|
||||
|
||||
Compute the minimum number of words from five-letter-words
|
||||
needed to cover N letters from the alphabet.
|
||||
|
@ -46,6 +46,6 @@ if __name__=="__main__":
|
||||
print("-"*40)
|
||||
for word in words:
|
||||
if(in_reverse_sorted_order(word)):
|
||||
print("Last lexicographically sorted word:")
|
||||
print("Last reverse lexicographically sorted word:")
|
||||
print(word)
|
||||
break
|
||||
|
Loading…
x
Reference in New Issue
Block a user