Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.93 KB | None | 0 0
  1. import sys
  2. import time
  3. import arguments
  4.  
  5.  
  6. def get_words(dictionary, encoding):
  7.     words = {}
  8.     try:
  9.         with open(dictionary, encoding=encoding) as f:
  10.             for line in f:
  11.                 line = line.strip()
  12.                 n = len(line)
  13.                 if n not in words.keys():
  14.                     words[n] = set()
  15.                 words[n].add(line)
  16.         return words
  17.     except TypeError:
  18.         report(2, dictionary)
  19.     except FileNotFoundError:
  20.         report(2, dictionary)
  21.     except LookupError:
  22.         report(3, encoding)
  23.  
  24.  
  25. def node(word1, word2):
  26.     cur_pos = 0
  27.     diff = 0
  28.     while cur_pos < len(word1) and diff < 2:
  29.         diff += int(word1[cur_pos] != word2[cur_pos])
  30.         cur_pos += 1
  31.     return True if diff == 1 else False
  32.  
  33.  
  34. def find_adjacent(vertex):
  35.     adj = set()
  36.     n = len(vertex)
  37.     for word in words[n]:
  38.         if node(word, vertex):
  39.             adj.add(word)
  40.     return adj
  41.  
  42.  
  43. def make_graph(n, words):
  44.     graph = {}
  45.     flag = False
  46.     for word1 in words[n]:
  47.         for word2 in words[n]:
  48.             print(word1, " ", word2)
  49.             if not flag:
  50.                 graph[word2] = set()
  51.             if node(word1, word2):
  52.                 graph[word1].add(word2)
  53.                 graph[word2].add(word1)
  54.         flag = True
  55.     return graph
  56.  
  57.  
  58. def check(first, last):
  59.     n = len(first)
  60.     if n != len(last):
  61.         report(0)
  62.         return False
  63.     if first not in words[n]:
  64.         report(1, first)
  65.         return False
  66.     if last not in words[n]:
  67.         report(1, last)
  68.         return False
  69.     return True
  70.  
  71.  
  72. def search(first, last, words, quick):
  73.     if check(first, last):
  74.         parent = {}
  75.         n = len(first)
  76.         container = [first]
  77.         used = {word: False for word in words[n]}
  78.         used[first] = True
  79.         while container:
  80.             cur = container[-1] if quick else container[0]
  81.             container = container[:-1] if quick else container[1:]
  82.             for word in find_adjacent(cur):
  83.                 if not used[word]:
  84.                     container.append(word)
  85.                     used[word] = True
  86.                     parent[word] = cur
  87.                 if word == last:
  88.                     return (True, parent)
  89.         return (used[last], parent)
  90.  
  91.  
  92. def way(first, last, parent):
  93.     temp = [last]
  94.     cur = last
  95.     while cur != first:
  96.         cur = parent[cur]
  97.         temp.append(cur)
  98.     return temp
  99.  
  100.  
  101. def show_answer(first, last, words):
  102.     if search(first, last, words, arguments.args.q)[0]:
  103.         parent = search(first, last, words, arguments.args.q)[1]
  104.         ans = way(first, last, parent)
  105.         steps = len(ans)
  106.         print("________________________________________________")
  107.         kind = "random" if arguments.args.q else "optimal"
  108.         print("The {} way takes {} steps to get \
  109. from '{}' to '{}'".format(kind, steps, first, last))
  110.         for i in range(steps):
  111.             print(ans[steps - i - 1])
  112.     else:
  113.         print("________________________________________________")
  114.         print("Sorry! I tried hard but I haven't found no way")
  115.  
  116.  
  117. def report(error_code, param=""):
  118.     messages = []
  119.     messages.append("Your words have different lengths! \
  120. How do you imagine me to do that?")
  121.     messages.append("Sorry, I'm not poliglot. \
  122. I haven't found the word '{}' in your dictionary.".format(param))
  123.     messages.append("Sorry, I can't find \
  124. '{}' in current directory".format(param))
  125.     messages.append("Sorry, I don't know \
  126. anything about encoding '{}'".format(param))
  127.     print (messages[error_code])
  128.     sys.exit()
  129.  
  130.  
  131. words = get_words(arguments.args.d, arguments.args.e)
  132. start = time.time()
  133. show_answer(arguments.args.first, arguments.args.last, words)
  134. finish = time.time()
  135. print("________________________________________________")
  136. print("It took me {:2.3f} seconds to solve this \
  137. problem".format(finish - start))
  138. print("Hope, it didn't seem too long")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement