def makechildren(startord): alphabet = "abcdefghijklmnopqrstuvwxyzåäö" gamla.put(startord.word) for i in range(len(startord.word)): # Varje element i startordet for j in alphabet: # Ersätter med varje bokstav i alfabetet newWord = list(startord.word) newWord[i] = j newWord = "".join(newWord) if newWord in svenska and newWord not in gamla: gamla.put(newWord) newNode = ParentNode(newWord) newNode.parent = startord q.enqueue(newNode)
def makeChildren(startord): alfabet = 'abcdefghijklmnoprstuvwxyzåäö' gamla.put(startord.word) for letter in range(len(startord.word)): for bokstav in alfabet: nyttord =list(startord.word) nyttord[letter]= bokstav nyttord = "".join(nyttord) if svenska.__contains__(nyttord) and not gamla.__contains__(nyttord): gamla.put(nyttord) nynod = ParentNode(nyttord) nynod.parent= startord q.enqueue(nynod)
def makeChildren(parentNode, endWord, swedish, q): alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','å','ä','ö'] for i in range(len(parentNode.word)): wordList = list(parentNode.word)#läs in startordet som en lista för att lättare kunna byta ut bokstäver for letter in alphabet: wordList = list(wordList) wordList[i] = letter wordList = ''.join(wordList) child = ParentNode(wordList, parentNode) if swedish.exists(wordList) and not child.exists(): q.put(child) if wordList == endWord: writeChain(child) ## child.printParents() print('\n') #sys.exit() return q
def main(): swedish = Bintree() #Läs in hela filen till ett binärträd with open("word3.txt", "r", encoding = "utf-8") as swedishFile: for row in swedishFile: word = row.strip() swedish.put(word) startWord = input('Mata in startord: ') endWord = input('Mata in slutord: ') pn = ParentNode(startWord) q = ListQ() q.put(pn) while not q.isEmpty(): parentNode = q.get() q = makeChildren(parentNode, endWord, swedish, q) print('Det ar omojligt att hitta en vag till', endWord)