def uppgift4(): wordList = open('word3u.txt', 'rt', encoding='utf8').read().split(); wordListEngInput = open('englishu.txt', 'rt', encoding='utf8').read().split(); tree = Bintree(); treeEng = Bintree(); for word in wordList: tree.put(word); for word in wordListEngInput: word = word.strip('".!?, ').lower(); if (tree.exists(word) and treeEng.put(word)): print(word);
def search(startWord, endWord): quene = Queue() tree = Bintree() father = WordNode(None, startWord) child = WordNode() quene.put(father) tree.put(father) while not quene.isempty(): word = quene.get() if word.getWord() == endWord: child = word break else: children = generateChildren(word) for kid in children: if tree.put(kid): quene.put(kid) if not child.getWord() == None: path = [] while not child.getFather() == None: path += [child.getWord()] child = child.getFather() path += [father.getWord()] path.reverse() print('Väg mellan', startWord, 'och', endWord) for item in path: print(item + ' -> ') else: raise Exception('No path found')
def uppgift2(): # Problem vid ordnad lista. Blanda före insättning. wordList = open('15wordu.txt', 'rt', encoding='utf8').read().split(); tree = Bintree(); for word in wordList: tree.put(word); print('Trädets höjd är', tree.height());
def generateChildren(wordNodeFather): tree = Bintree() father = wordNodeFather.getWord() for i in range(0, len(father)): #for every letter in father for j in range(0, len(letters)): possibleWord = father[:i] + letters[j] + father[i + 1:] if wordTree.exists(possibleWord) and not possibleWord == father: tree.put(WordNode(wordNodeFather, possibleWord)) return tree.getTree()
def uppgift3(): wordList = open('word3u.txt', 'rt', encoding='utf8').read().split(); tree = Bintree(); for word in wordList: if not tree.put(word): print(word);
from Bintree import Bintree tree = Bintree() f = open("word3.txt", "r") lines = f.readlines() for line in lines: line = line.rstrip("\n") if tree.exists(line): print(line) else: tree.put(line) #tree.write()
if not isinstance(other, WordNode): return False return self.getWord() < other.getWord() def __gt__(self, other): if not isinstance(other, WordNode): return False return self.getWord() > other.getWord() def __eq__(self, other): if not isinstance(other, WordNode): return False return self.getWord() == other.getWord() wordList = open('word3u.txt', 'rt', encoding='utf8').read().split() letters = 'abcdefghijklmnopqrstuvwxyzåäö' wordTree = Bintree() for word in wordList: wordTree.put(word) def generateChildren(wordNodeFather): tree = Bintree() father = wordNodeFather.getWord() for i in range(0, len(father)): #for every letter in father for j in range(0, len(letters)): possibleWord = father[:i] + letters[j] + father[i + 1:] if wordTree.exists(possibleWord) and not possibleWord == father: tree.put(WordNode(wordNodeFather, possibleWord)) return tree.getTree()
from Bintree import Bintree sweTree = Bintree() engTree = Bintree() #Read the swedish words sweFile = open("word3.txt", "r") sweLines = sweFile.readlines() #Insert them into a BST for line in sweLines: line = line.rstrip("\n") if sweTree.exists(line): print(line) else: sweTree.put(line) #Read english words engFile = open("engelska.txt", "r") engLines = engFile.readlines() for line in engLines: line = line.rstrip("\n") line = line.split(" ") for word in line: if word == "": #That's not much of a word is it? pass else: if engTree.exists(word): #It's allready been inserted pass else:
from Bintree import Bintree wordTree = Bintree() reversedTree = Bintree() #BST for printed reversed words. f = open("word3.txt","r") lines = f.readlines() for line in lines: line = line.rstrip("\n") if not wordTree.exists(line): wordTree.put(line) print("This app find words that are the same when reversed!") input("Press enter to continue:") for line in lines: line = line.rstrip("\n") reversedLine = line[::-1] #Reverses the string if not line == reversedLine: #Gets rid of symmetric words if not reversedTree.exists(line): #Storage of allready shown reversed words. Do this before other search since it's quicker. if wordTree.exists(reversedLine): print(line) print(line[::-1]) print() reversedTree.put(line)