Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.06 KB | None | 0 0
  1. import sys
  2.  
  3. def get_targs():
  4.     ''' Read words from console '''
  5.     start_word = ''
  6.     finish_word = ''
  7.     key = False
  8.     depth = 0
  9.     if len(sys.argv) == 3:
  10.         start_word = sys.argv[1]
  11.         finish_word = sys.argv[2]
  12.     elif len(sys.argv) == 5 and sys.argv[3] == '--all':
  13.         start_word = sys.argv[1]
  14.         finish_word = sys.argv[2]
  15.         key = True
  16.         depth = sys.argv[4]
  17.     elif (len(sys.argv) != 3 and len(sys.argv) != 5) or sys.argv[1] == 'help':
  18.         print ('Run the program again and write 2 words with a space and key "--all" and number, if you want to have all of the chain length number')
  19.         sys.exit()
  20.     return start_word, finish_word, key, depth
  21.  
  22.  
  23. def correct_length(start_word, finish_word):
  24.     ''' Check that the words were the same length '''
  25.     return len(start_word) == len(finish_word)
  26.  
  27.  
  28. def get_similar(word, words):
  29.     ''' Looking in the dictionary words that are different from the previous
  30.    one by one letter'''
  31.     same_words = set()
  32.     for dictionary_word in words:
  33.         is_prefix_eq = False
  34.         for ch in range(len(word)):
  35.             if dictionary_word[ch] != word[ch]:
  36.                 if is_prefix_eq:
  37.                     break
  38.                 is_prefix_eq = True
  39.         else:
  40.             same_words.add(dictionary_word)
  41.     return same_words
  42.  
  43.  
  44. def get_good_words(start_word, words):
  45.     ''' Select all the words from the dictionary, which length is equal
  46.    to the original word'''
  47.     good_words = {i.strip() for i in words if len(i.strip()) == len(start_word)}
  48.     return good_words
  49.  
  50.  
  51. def build_tree(start_word, finish_word, dictionary, words):
  52.     ''' Building "a tree" of words and words like them '''
  53.     queue = [start_word]
  54.     while len(queue) != 0:
  55.         current = queue.pop(0)
  56.         similar_words = get_similar(current, words)
  57.         for word in similar_words:
  58.             if word not in dictionary:
  59.                 queue.append(word)
  60.                 dictionary[word] = current
  61.             if word == finish_word:
  62.                 return word
  63.     return None
  64.  
  65. def print_result(word, finish_word, dictionary):
  66.     '''Print results for situation without keys'''
  67.     if word is None:
  68.         print('Our dictionary have no this words, sorry :(')
  69.         return
  70.     path = [finish_word]
  71.     while dictionary[word] is not None:
  72.         path.append(dictionary[word])
  73.         word = dictionary[word]
  74.     for word in path[::-1]:
  75.         print(word)
  76.  
  77. def equal_words(start_word, finish_word):
  78.     '''Check words for equality'''
  79.     if start_word == finish_word:
  80.         print(start_word)
  81.         return True
  82.     return False
  83.  
  84. def DFS(current_word, finish_word, trace, words, depth):
  85.     '''Depth-first search for searching all chains that length is depth'''
  86.     if len(trace) > depth:
  87.         return
  88.     if current_word == finish_word:
  89.         yield trace
  90.         return
  91.     for word in get_similar(current_word, words):
  92.         if word in trace:
  93.             continue
  94.         for i in DFS(word, finish_word, trace + [word], words, depth):
  95.             yield i
  96.  
  97. def main():
  98.     start_word, finish_word, key, depth = get_targs()
  99.     if equal_words(start_word, finish_word):
  100.         sys.exit()
  101.  
  102.     if not correct_length(start_word, finish_word):
  103.         print('Your words have differnt length')
  104.         sys.exit()
  105.  
  106.     words = get_good_words(start_word, open('runouns.txt', encoding='utf-8'))
  107.  
  108.     dictionary = {start_word: None}
  109.     if key == False:
  110.         tree = build_tree(start_word, finish_word, dictionary, words)
  111.         print_result(tree, finish_word, dictionary)
  112.     if key == True:
  113.         traces = DFS(start_word, finish_word, [start_word], words, int(depth))
  114.         list_of_traces = list(traces)
  115.         if len(list_of_traces) == 0:
  116.             print('We have not chains, that have this depth. Run programm again and enter a number that is greater than the previous depth')
  117.         else:
  118.             for i in list_of_traces:
  119.                 for j in i:
  120.                    print(j)
  121.                 print()
  122.  
  123. if __name__ == "__main__":
  124.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement