Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import string
- def pathword(words,letters,word,exitword,visited,working,depth):
- print "Scanning ",word,type(word)
- visited[word] = 1
- difference = list(word)
- for i in range(len(difference)):
- templetters = letters[:]
- templetters.remove(difference[i])
- tempdifference = difference[:]
- del tempdifference[i]
- for j in templetters:
- tempword = [difference[x] for x in range(len(difference)) if x < i] + [j] + [difference[y] for y in range(len(difference)) if y > i]
- tempwordstring = "".join(tempword)
- if tempwordstring == exitword:
- print "Found word after ",depth," searches"
- return depth
- if tempwordstring in words:
- if visited[tempwordstring] == 0:
- depth += 1
- pathword(words,letters,tempwordstring,exitword,visited,working,depth)
- return depth
- def main(word,word2):
- words = set(open("words.txt").read().split('\n'))
- visited = dict((x, 0) for x in words)
- if not word in words or not word2 in words:
- print "ERRRRRRRRRRRROR: One of these words are not found: ",word," ",word2
- return
- results = pathword(words,list(string.letters[:26]),word,word2,visited,[],0)
- print "Words without candidates: ",results
- if __name__ == '__main__':
- main(sys.argv[1],sys.argv[2])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement