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