Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.43 KB | None | 0 0
  1. import sys
  2. import string
  3.  
  4. def pathword(words,letters,word,exitword,visited,working,depth):
  5.     print "Scanning ",word,type(word)
  6.     visited[word] = 1
  7.     difference = list(word)
  8.     for i in range(len(difference)):
  9.         templetters = letters[:]
  10.         templetters.remove(difference[i])
  11.         tempdifference = difference[:]
  12.         del tempdifference[i]
  13.         for j in templetters:
  14.             tempword = [difference[x] for x in range(len(difference)) if x < i] + [j] + [difference[y] for y in range(len(difference)) if y > i]
  15.             tempwordstring = "".join(tempword)
  16.             if tempwordstring == exitword:
  17.                 print "Found word after ",depth," searches"
  18.                 return depth
  19.             if tempwordstring in words:
  20.                 if visited[tempwordstring] == 0:
  21.                     depth += 1
  22.                     pathword(words,letters,tempwordstring,exitword,visited,working,depth)
  23.     return depth        
  24.    
  25.  
  26.  
  27.  
  28. def main(word,word2):
  29.     words = set(open("words.txt").read().split('\n'))
  30.     visited = dict((x, 0) for x in words)
  31.     if not word in words or not word2 in words:
  32.             print "ERRRRRRRRRRRROR: One of these words are not found: ",word," ",word2
  33.             return
  34.     results = pathword(words,list(string.letters[:26]),word,word2,visited,[],0)
  35.     print "Words without candidates: ",results
  36.        
  37.  
  38.  
  39. if __name__ == '__main__':
  40.     main(sys.argv[1],sys.argv[2])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement