Compare commits

...

5 Commits
master ... main

  1. 11
      diff_by_n.py
  2. 84
      diff_by_one_fixed.py
  3. 2
      letter_coverage.py
  4. 2
      reverse_lexico.py
  5. 2
      tries.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 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==5: if depth==len(word):
variations.add(fragment) variations.add(fragment)
else: else:
for d in range(1,distance+1): 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) 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 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 This does not include `word` in the
variations. variations.
""" """
word_variations = set() word_variations = set()
gen_variations(word,'',d,0,word_variations) gen_variations(word,'',distance,0,word_variations)
word_variations = list(word_variations) word_variations = list(word_variations)
return word_variations return word_variations
@ -41,7 +42,7 @@ def get_all_variations(word,d):
def main(): def main():
""" """
Find pairs of SGB word vectors that differ by Find pairs of SGB word vectors that differ by
+/-d in each component. +/-distance 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

@ -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()

2
letter_coverage.py

@ -1,5 +1,5 @@
""" """
word_coverage.py letter_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.

2
reverse_lexico.py

@ -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 lexicographically sorted word:") print("Last reverse lexicographically sorted word:")
print(word) print(word)
break break

2
tries.py

@ -297,5 +297,5 @@ if __name__=="__main__":
_,_ = trie_search(n, verbose=True) _,_ = trie_search(n, verbose=True)
#trie_table() trie_table()

Loading…
Cancel
Save