Advertisement
Guest User

wordward draw solver

a guest
Aug 6th, 2023
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. # Define the path to the system dictionary file on Ubuntu
  2. dictionary_file_path = '/usr/share/dict/words'
  3.  
  4. def get_four_letter_words(file_path):
  5.     four_letter_words = []
  6.     with open(file_path, 'r') as file:
  7.         for line in file:
  8.             word = line.strip()
  9.             word = word.lower()
  10.             if len(word) == 4:
  11.                 four_letter_words.append(word)
  12.     return four_letter_words
  13.  
  14. four_letter_words_array = get_four_letter_words(dictionary_file_path)
  15.  
  16. def is_adjacent(word1, word2):
  17.     if ((word1[0] == word2[0]) + (word1[1] == word2[1]) + (word1[2] == word2[2]) + (word1[3] == word2[3])) == 3: return True
  18.     return sorted(word1) == sorted(word2)
  19.  
  20. def all_adjacent(word):
  21.     return filter(lambda w: is_adjacent(word, w), four_letter_words_array)
  22.  
  23. def difference(word1, word2):
  24.     diff = list(word2)
  25.     for let in word1:
  26.         if let in diff:
  27.             diff.remove(let)
  28.     return len(diff) + (word1[0] != word2[0]) + (word1[1] != word2[1]) + (word1[2] != word2[2]) + (word1[3] != word2[3])
  29.  
  30. def path_to(fr, to, path=[], d=None):
  31.     path = path + [fr]
  32.  
  33.     d = d if d is not None else difference(fr, to)
  34.     if d == 0:
  35.         return path
  36.     for word in all_adjacent(fr):
  37.         if word in path: continue
  38.         d2 = difference(word, to)
  39.         if d2 < d:
  40.             npath = path_to(word, to, path, d2)
  41.             if npath: return npath
  42.  
  43. word = "word"
  44.  
  45. while True:
  46.     inp = input(f"word is {word} > ").lower()
  47.     if len(inp) == 4:
  48.         word = inp
  49.     elif inp.startswith("-t"):
  50.         try:
  51.             to = inp.split()[1]
  52.             assert len(to) == 4
  53.         except:
  54.             print("incorrect command for -t")
  55.             continue
  56.  
  57.         path = path_to(word, to)
  58.         if not path: print("No path found")
  59.         else:
  60.             print(f"words leading from {word} to {to}")
  61.             for w in path:
  62.                 print(f"  {w}")
  63.     elif inp == "-l":
  64.         print(f"words adjancent to {word}")
  65.         for adjacent_word in all_adjacent(word):
  66.             print(f"  {adjacent_word}")
  67.  
  68.     elif inp == "-q":
  69.         print("Goodbye.")
  70.         break
  71.     else:
  72.         print("""Enter a four letter word to set the current word.
  73.  commands:
  74.    -t {word}
  75.      [to {word}] find a path from word to word.
  76.    -l
  77.     [list] list all adjacent words.
  78.    -q
  79.     [quit]""")
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement