def main(): # Läser in svensk fil och gör binärträd. Skriver ut dubletter. svenska = Bintree() with open('word3.txt', 'r', encoding='utf-8') as svenskfil: for rad in svenskfil: ordet = rad.strip() # ett trebokstavsord per rad if ordet in svenska: print(ordet, end=' ') else: svenska.put(ordet) # in i sv-sökträd print('\n') # läser in engelsk fil och gör binärträd. Kollar om engelska ord finns i sv-träd engelska = Bintree() with open('engelska.txt', 'r', encoding='utf-8') as engelskfil: for rad in engelskfil: raden = rad.split() # lista med varje rad for ord in raden: ord = ord.strip(',."') # lista med varje ord if ord in engelska: pass # sortera bort dubletter else: engelska.put(ord) # in i eng-sökträd if svenska.__contains__(ord): print(ord, end=' ')
class Operation(object): def __init__(self, start_word, word_list): self.init_aplhabet() self.child_node_list = [] self.start_word = start_word self.word_list = word_list self.old_tree = Bintree() self.queue = LinkedQ() self.d = {} self.start_node = Node(self.start_word) self.queue.put(self.start_node) self.start_word_bucket_list = self.make_buckets(self.start_word) self.dict_handler() def make_buckets(self, word): bucket_list = [] for i in range(len(word)): bucket = word[:i] + '_' + word[i+1:] bucket_list.append(bucket) return bucket_list def init_aplhabet(self): self.alphabet = [] for x in range(ord("a"),ord("z")+1): self.alphabet.append(chr(x)) for x in ("å","ä","ö"): self.alphabet.append(x) def dict_handler(self): for line in self.word_list: word = line for i in range(len(word)): bucket = word[:i] + '_' + word[i+1:] if not(bucket in self.d): self.d[bucket] = [] # create list with bucket as key self.d[bucket].append(word) else: self.d[bucket].append(word) def make_children(self, parent_node): word = parent_node.value bucket_list = self.make_buckets(word) for bucket in bucket_list: word_list = self.d[bucket] for new_word in word_list: child_node = Node(new_word, parent=parent_node) if self.queue.isEmpty(): self.child_node_list.append(child_node) if not(self.old_tree.exists(new_word)): self.old_tree.put(new_word) self.queue.put(child_node)
def main(): # Skapar binärträd av ordlistan (svenska) och dumbarnen (gamla) # Frågar efter start- och slutord svenska = Bintree() # ordlistan gamla = Bintree() # dumbarnen with open('word3.txt', 'r', encoding = 'utf-8') as svenskfil: for rad in svenskfil: ordet = rad.strip() # ett trebokstavsord per rad if ordet in svenska: pass else: svenska.put(ordet) startord = input('Vad är startordet?\n') slutord = input('Vad är slutordet?\n') q = LinkedQ() q.enqueue(startord) gamla.put(startord) foundPath = None while not q.isEmpty(): # Gå igenom alla barn, barnbarn, ... ord = q.dequeue() if ord == slutord: foundPath = True break makechildren(ord, q, svenska, gamla) if foundPath: print('Det finns en väg från', startord, 'till', slutord) else: print('Det finns ingen väg från', startord, 'till', slutord)
def BintreeMaker(ordlista): svenska = Bintree() with open("word3.txt", "r", encoding="utf-8") as svenskfil: for rad in svenskfil: ordet = rad.strip() if ordet not in svenska: ordlista.append(ordet) svenska.put(ordet) return ordlista, svenska
def main(): svenska = Bintree() gamla = Bintree() with open('word3.txt', 'r', encoding = 'utf-8') as svenskfil: for rad in svenskfil: ordet = rad.strip() if svenska.exists(ordet) == True: svenska.put(ordet) else: pass
def main(): svenska = Bintree() with open('word3.txt', 'r', encoding = 'utf-8') as svenskfil: for rad in svenskfil: ordet = rad.strip() if not svenska.exists(ordet): svenska.put(ordet) # Om ordet inte finns i trädet lagras det i trädet if svenska.exists(ordet[::-1]) and ordet != ordet[::-1]: print(ordet + ' - ' + ordet [::-1])
def main(): svenska = Bintree() with open("word3.txt", "r", encoding="utf-8") as svenskfil: for rad in svenskfil: ordet = rad.strip() if ordet not in svenska: svenska.put(ordet) gamla = Bintree() startord = 'söt' slutord = 'sur' makechildren(startord, svenska, gamla)
def main(): svenska = Bintree() with open('word3.txt', 'r', encoding = 'utf-8') as svenskfil: for rad in svenskfil: ordet = rad.strip() if svenska.exists(ordet): print(ordet, end = ' ') else: svenska.put(ordet) print('\n')
def svenska(): # Skapar ett träd med svenska ord svenska = Bintree() with open("word3.txt", "r", encoding = "utf-8") as svenskfil: startord = input("vilket ord vill du starta på? ") slutord = input("vilket ord vill du ta dig till? ") for rad in svenskfil: ordet = rad.strip() # Ett trebokstavsord per rad if ordet in svenska: print(ordet, end = " ") else: svenska.put(ordet) # in i sökträdet print("\n")
def makechildren(startord): gamla = Bintree() for i in range(len(startord)): for letter in "abcdefghijklmnopqrstuvwxyzåäö": if startord[i] is letter: pass else: if i == 0: new_word = letter + startord[1] + startord[2] elif i == 1: new_word = startord[0] + letter + startord[2] elif i == 2: new_word = startord[0] + startord[1] + letter if new_word in svenska and new_word not in gamla: gamla.put(new_word) print(new_word)
def gamla(): # Skapar ett träd med engelska ord engelska = Bintree() svenska = Bintree() with open("word3.txt", "r", encoding = "utf-8") as svenskfil: for rad in svenskfil: ordet = rad.strip() svenska.put(ordet) with open("engelska.txt", "r", encoding="utf-8") as engelskafil: for line in engelskafil: word = line.split(" ") for i in word: if not i in engelska: engelska.put(i) if i in svenska: print(i, end = " ") print("\n")
class ChildOperation(): def __init__(self): self.start_word = "söt" self.end_word = "sur" self.swe_tree = Bintree() self.old_tree = Bintree() self.queue = LinkedQ() self.init_aplhabet() self.file_reader() # Creates a list with the swedish alphabet def init_aplhabet(self): self.alphabet = [] for x in range(ord("a"), ord("z") + 1): self.alphabet.append(chr(x)) self.alphabet.append("å") self.alphabet.append("ä") self.alphabet.append("ö") # Reads textfile, places all words in swe_tree def file_reader(self): file_name = "word3.txt" f = open(file_name, "r", encoding="utf-8") for line in f: self.swe_tree.put(line.replace("\n", "")) f.close() # Creates childs of 'word' def make_children(self, word): word_letter_list = [] i = 0 for letter in word: word_letter_list = list(word) for letter in self.alphabet: word_letter_list[i] = letter new_word = ''.join(word_letter_list) if new_word != word and self.swe_tree.exists( new_word) and not (self.old_tree.exists(new_word)): self.old_tree.put(new_word) self.queue.put(new_word) i += 1
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)
def main(): svenska = Bintree() gamla = Bintree() with open("word3.txt", "r", encoding="utf-8") as svenskfil: for rad in svenskfil: ordet = rad.strip() if ordet not in svenska: svenska.put(ordet) startord = input('Ange ett startord: ') parent = ParentNode(startord) slutord = input('Ange ett slutord: ') q = LinkedQ() q.enqueue(parent) try: while not q.isEmpty(): parent = q.dequeue() makechildren(parent, startord, slutord, svenska, q, gamla) print('Det finns ingen väg.') except SolutionFound: pass
def makechildren(startord): #gå igenom alla sätt att ändra startordet #kolla om det nya ordet finns i ordlista och inte i gamla #printa om det inte finns med i gamla och sätt in i gamla #finns det med i gamla så gör vi inget. gamla = Bintree() for i in range(len(startord)): for char in "abcdefghijklmnopqrstuvwxyzåäö": if startord[i] == char: pass else: new_word = startord[:i] + char + startord[i+1:] if new_word in svenska: if new_word not in gamla: print(new_word) gamla.put(new_word) else: pass else: pass
def main(): svenska = Bintree() engelska = Bintree() with open('word3.txt', 'r', encoding = 'utf-8') as svenskfil: for rad in svenskfil: ordet = rad.strip() if not svenska.exists(ordet): svenska.put(ordet) with open('engelska.txt', 'r', encoding = 'utf-8') as engelskfil: for rad in engelskfil: ordrad = rad.split() for ordet in ordrad: if engelska.exists(ordet): pass else: engelska.put(ordet) if svenska.exists(ordet): print(ordet, end = ' ') print('\n')
def main(): svenska = Bintree() gamla = Bintree() with open("word3.txt", "r", encoding="utf-8") as svenskfil: for rad in svenskfil: ordet = rad.strip() if ordet not in svenska: svenska.put(ordet) startord = input('Ange ett startord: ') slutord = input('Ange ett slutord: ') q = LinkedQ() q.enqueue(startord) kedja = None while not q.isEmpty(): nod = q.dequeue() kedja = makechildren(nod, slutord, svenska, q, gamla) if kedja is not None: break if kedja is None: print('Det finns ingen väg') else: print('Det finns en väg från', startord, 'till', slutord)
def main(): # Skapar binärträd av ordlistan (svenska) och dumbarnen (gamla) # Frågar efter start- och slutord svenska = Bintree() # ordlistan gamla = Bintree() # dumbarnen with open('word3.txt', 'r', encoding='utf-8') as svenskfil: for rad in svenskfil: ordet = rad.strip() # ett trebokstavsord per rad if ordet in svenska: pass else: svenska.put(ordet) startord = input('Vad är startordet?\n') slutord = input('Vad är slutordet?\n') q = LinkedQ() # skapar en kö node = ParentNode(startord) # skapa en ParentNode av startord q.enqueue(node) # lägger in startordet i kön gamla.put(startord) # lägger in startordet i gamlabarn-trädet foundPath = None while not q.isEmpty(): # Gå igenom alla barn, barnbarn, ... parent = q.dequeue() if parent.word == slutord: foundPath = True break makechildren(parent, q, svenska, gamla) if foundPath: print('Det finns en väg från', startord, 'till', slutord, '\nVägen är:') parent.writechain() # parent är slutordets ParentNode else: print('Det finns ingen väg från', startord, 'till', slutord)
def main(): gamla = Bintree() q = LinkedQ() ordlista = [] ordlista, svenska = BintreeMaker(ordlista) startordInput = input('Choose a start word') slutordInput = input('Choose an end word') if startordInput in svenska and slutordInput in svenska: gamla.put(startordInput) q.enqueue(startordInput) while not q.isEmpty(): node = q.dequeue() makechildren(node, ordlista, gamla, q) if slutordInput in gamla: print('Det finns en väg till ' + slutordInput) sys.exit() elif q.isEmpty(): print('Det finns ingen väg till ' + slutordInput) else: print('The word is not in the dictionary') main() return
from bintreeFile import Bintree svenska = Bintree() with open("word3.txt", "r", encoding="utf-8") as svenskfil: for rad in svenskfil: ordet = rad.strip() # Ett trebokstavsord per rad if ordet in svenska: print(ordet, end=" ") else: svenska.put(ordet) # in i sökträdet print("\n") ''' Om ordet finns i trädet redan, gör inget. Om ordet INTE finns i trädet, sätt in ordet i trädet och kolla om ordet finns i det svenska trädet med ord. Om ordet även fanns i det svenska trädet så skriver programmet ut det. ''' engelska = Bintree() with open("engelska.txt", "r", encoding="utf-8") as engelskfil: for rad in engelskfil: rad_strip = rad.strip() rad_ord = rad_strip.split() for ord_ in rad_ord: if ord_ in engelska: pass else: engelska.put(ord_) if ord_ in svenska: print(ord_, end=" ") print("\n")
class Operation(object): def __init__(self): self.init_aplhabet() self.child_node_list = [] self.start_word = "söt" self.old_tree = Bintree() self.queue = LinkedQ() self.d = {} self.start_node = Node(self.start_word) self.queue.put(self.start_node) self.start_word_bucket_list = self.make_buckets(self.start_word) self.file_reader() # Creates a list of bucketed words def make_buckets(self, word): bucket_list = [] for i in range(len(word)): bucket = word[:i] + '_' + word[i + 1:] bucket_list.append(bucket) return bucket_list # Creates alphabet attribute def init_aplhabet(self): self.alphabet = [] for x in range(ord("a"), ord("z") + 1): self.alphabet.append(chr(x)) for x in ("å", "ä", "ö"): self.alphabet.append(x) # Reads file and places lines in dict sorted def file_reader(self): self.word_list = [] file_name = "word3.txt" f = open(file_name, "r", encoding="utf-8") for line in f: word = line[:-1] for i in range(len(word)): bucket = word[:i] + '_' + word[i + 1:] if not (bucket in self.d): self.d[bucket] = [] # create list with bucket as key self.d[bucket].append(word) else: self.d[bucket].append(word) f.close() # Creates children of parent_node by getting the corresponding child from graph object def make_children(self, parent_node): word = parent_node.value bucket_list = self.make_buckets(word) for bucket in bucket_list: word_list = self.d[bucket] for new_word in word_list: child_node = Node(new_word, parent=parent_node) if self.queue.isEmpty(): self.child_node_list.append(child_node) if not (self.old_tree.exists(new_word)): self.old_tree.put(new_word) self.queue.put(child_node)
word_letter_list = [] i = 0 for letter in WORD: word_letter_list = list(WORD) for letter in alphabet: word_letter_list[i] = letter new_word = ''.join(word_letter_list) if new_word != WORD and swe_tree.exists(new_word) and not ( old_tree.exists(new_word)): old_tree.put(new_word) i += 1 swe_tree = Bintree() old_tree = Bintree() file_name = "word3.txt" f = open(file_name, "r", encoding="utf-8") for line in f: swe_tree.put(line.replace("\n", "")) f.close() start_word = "söt" end_word = "sur" make_children(start_word) print(old_tree.write())
from bintreeFile import Bintree import time import random starttime = time.time() tree = Bintree() file_name = "word3.txt" # create list and tree with random integers. value_list = [] for x in range(0,100000+1): value = random.randint(0,1000000) value_list.append(value) tree.put(value) random.shuffle(value_list) # find some value in that exists x = 0 while True: x += 1 if tree.exists(x): break print("Finding value: " + str(x)) decimals = 10 # find the value in the tree
from bintreeFile import Bintree, Node svenska = Bintree() # Skapa ett trädobjekt svenska.put("gurka") # Sortera in "gurka" i trädet if svenska.exists("gurka"): # Kolla om "gurka" finns i trädet svenska.write() # Skriver alla ord i bokstavsordning
from linkedQFile import LinkedQ from bintreeFile import Bintree class ParentNode: """ Klass som skapar en länkad lista där barnen pekar på sin förälder """ def __init__(self, word, parent=None): self.word = word self.parent = parent def writechain(self): if self.parent: self.parent.writechain() # kalla på writechain med sin parent print(self.word) if __name__ == '__main__': startord = input('Vad är startordet?\n') print('Hej') q = LinkedQ() # skapar en kö gamla = Bintree() # dumbarnen node = ParentNode(startord) # skapa en ParentNode av startord q.enqueue(node) # lägger in startordet i kön gamla.put(startord) # lägger in startordet i gamlabarn-trädet
from bintreeFile import Bintree import random import time def main(): <<<<<<< HEAD svenska = Bintree() slumplista = random.sample(range(100000), 100000) for n in slumplista: svenska.put(n) while True: search = input('Sök efter ett nummer mellan 1-100000: ') t0 = time.time() if svenska.exists(int(search)): search_time = time.time() - t0 print('Binärsökningen tog ' + str(search_time*1000000) + ' mikrosekunder\n') t0 = time.time() for n in slumplista: if n == int(search): break print('Linjärsökningen tog ' + str((time.time() - t0)*1000000) + ' mikrosekunder\n') print(slumplista[0:10]) ======= svenska = Bintree() svenska.put('gurka') svenska.put('ananas') svenska.put('ödla') svenska.put('gurra') svenska.put('tomat')
class Operation(object): def __init__(self, start_time): self.init_aplhabet() # Used for file with 3 letter-words –––––––––––––– self.g = buildGraph("word3.txt") self.start_word = "söt" self.end_word = "sur" # Used for file with 5 letter-words –––––––––––––– # self.start_word = "anstå" # self.end_word = "anslå" # self.g = buildGraph("word5.txt") self.old_tree = Bintree() self.queue = LinkedQ() self.start_time = start_time self.start_node = Node(self.start_word) self.queue.put(self.start_node) # Creates alphabet attribute def init_aplhabet(self): self.alphabet = [] for x in range(ord("a"), ord("z") + 1): self.alphabet.append(chr(x)) for x in ("å", "ä", "ö"): self.alphabet.append(x) # Creates children of parent_node by getting the corresponding child from graph object def make_children(self, parent_node): word = parent_node.word parent_vertex = self.g.getVertex( word) # Gets the vertex corresponding with the word if parent_vertex == None: print("Error there is no way to " + self.end_word) pass else: connected_vertices = parent_vertex.getConnections( ) # Gets the connected vertecies for vertex in connected_vertices: vertex_name = vertex.getId() # Gets the word new_word = vertex_name child_node = Node(new_word, parent=parent_node) if new_word == self.end_word: self.queue.put(child_node) write_chain(child_node, self.start_time, self) if not (self.old_tree.exists(new_word)): self.old_tree.put(new_word) self.queue.put(child_node) if new_word != self.end_word and self.queue.isEmpty(): print("Error there is no way to " + self.end_word) raise SystemExit
startord = ParentNode(input("Vilket ord vill du börja med?, söt: ")) slutord = input("Vilket ord vill du sluta med?, sur: ") class ParentNode: def __init__(self, word, parent = None): self.word = word self.parent = parent def writeChain(x): with open ('word3.txt', 'r', encoding = 'utf-8') as svenskfil: for rad in svenskfil: ordet = rad.split() for orden in ordet: svenska.put(orden) def makeChildren(start, s**t): ords = start.word alfabetet = 'qwertyuiopåasdfghjklöäzxcvbnm' for bokstav in alfabetet: for i in range(len(ords)): barn = ords[:i]+bokstav+ords[i+1:] if barn == s**t: writeChain(barn) if svenska.exists(barn) and not gamla.exists(barn): gamla.put(barn) q.putLast(barn) makeChildren(startord)
# Author: Christian Abdelmassih, Alexandra Runhem from bintreeFile import Bintree file_name = "word3.txt" tree = Bintree() output_tree = Bintree() # place all values in the tree f = open(file_name, "r", encoding="utf-8") for line in f: tree.put(line.replace("\n", "")) f.close() # reverse the word and check if both words exists in the tree but are diffrent # if they are, place the word in the output tree. f = open(file_name, "r", encoding="utf-8") for line in f: line = line.replace("\n", "") line_rev = line[::-1] if tree.exists(line_rev) and line != line_rev: print(line + " " + line_rev) f.close()
from bintreeFile import Bintree from random import shuffle import time #Testar put svenska = Bintree() svenska.put('gurka') if str(svenska.root) == "gurka": print("Root funkar") else: print("Fel rot") #Testar exists if svenska.exists('gurka'): print('\nExists funkar!\n') else: print('\nExists funkar inte') x = [[i] for i in range(1000000)] shuffle(x) position = 0 found = False t0 = time.time() while position < len(x) and not found: if x[position] == 5: found == True position = position + 1 searchtime = time.time() - t0 print(searchtime) taltree = Bintree() for b in x:
hash_value += ord(tkn) * counter counter += 2 length = len(str(hash_value)) - 1 # Salts the hash value with a different number depending on the amount of digits if length > 3: salt = "7" * length hash_value -= int(salt) elif length <= 3: salt = "7" * (length + 1) hash_value += int(salt) print(hash_value) return int(hash_value) tree = Bintree() member1 = Account('ernesto', 'e', 878) tree.put('ernesto', member1) site = Viastream() site.title("Viastream.com") site.geometry("1050x590") site.update_idletasks() # Updates the geometry of the window centerw = int( site.winfo_screenwidth() / 2 - site.winfo_width() / 2) # Used the center the window on the screen centerh = int(site.winfo_screenheight() / 2 - site.winfo_height() / 2) - 20 site.geometry("+{}+{}".format(centerw, centerh)) site.resizable(0, 0) # Makes to window unable to resize site.mainloop()
# count = 0; startord = input("Vilket är ditt startord? ") slutord = input("Vilket är ditt slutord? ") q = LinkedQ() q.enqueue(startord) ordlista = Bintree() gamla = Bintree() with open("word3.txt", "r", encoding="utf-8") as svenskfil: for rad in svenskfil: ordet = rad.strip() # Ett trebokstavsord per rad if ordet in ordlista: continue #print(ordet, end = " ") else: ordlista.put(ordet) # in i sökträdet while not q.isEmpty(): nod = q.dequeue() makechildren(nod, q) # count += 1 if nod == slutord: print("Det finns en väg till", slutord) # print("Antal gånger i while", count) break
from bintreeFile import Bintree from linkedQFile import LinkedQ alfabet = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'x', 'y', 'z', 'å', 'ä', 'ö') # All the words will be stored in svenska in a bintree svenska = Bintree() # Used to prevent duplicates gamla = Bintree() with open("word3.txt", "r", encoding="utf-8") as svenskfil: for rad in svenskfil: ordet = rad.strip() # Ett trebokstavsord per rad svenska.put(ordet) # 1. The word that will be checked with all different letters. # 2. The word that is looked for # 3. The current queue. def makechildren(word, end, q): # We know that the word exist, therefore being put in the dup. box. gamla.put(word) i = 0 start_word = list(word) while i < 3: for letter in alfabet: # Changes the word (position i) with the letter start_word[i] = letter # Takes the list and joins them with "" distance between them, a.k.a. making strings.
from bintreeFile import Bintree sv = Bintree() sv.put('beda') sv.put('ada') sv.put('caesar') sv.write()
from bintreeFile import Bintree svenska = Bintree() with open("word3.txt", "r", encoding="utf-8") as svenskfil: for rad in svenskfil: ordet = rad.strip() if ordet in svenska: print(ordet, end=" ") else: svenska.put(ordet) print("\n") engelska = Bintree() with open("engelska.txt", "r", encoding="utf-8") as engelskfil: for rad in engelskfil: rad = rad.strip().split() for ordet in rad: if ordet in engelska: pass else: engelska.put(ordet) if ordet in svenska: print(ordet, end=" ") print("\n")
from bintreeFile import Bintree, Node startord = input("Vilket ord vill du borja med?") slutord = input("Vilket ord vill du sluta med?") svenska = Bintree() gamla = Bintree() with open('word3.txt', 'r', encoding = 'utf-8') as svenskfil: for rad in svenskfil: ordet = rad.strip() if svenska.exists(ordet) == True: svenska.put(ordet) else: pass def makeChildren(x): alfabetet = "qwertyuiopåasdfghjklöäzxcvbnm" for tecken in alfabetet: for i in range(len(x)): child=x[:i]+tecken+x[i+1:] if svenska.exists(child) and not gamla.exists(child): gamla.put(child) print(child) makeChildren(startord)
print(slutordsnod.word) alfabet = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'x', 'y', 'z', 'å', 'ä', 'ö') # All the words will be stored in svenska in a bintree svenska = Bintree() # Used to prevent duplicates gamla = Bintree() with open("word3.txt", "r", encoding="utf-8") as svenskfil: for rad in svenskfil: ordet = rad.strip() # Ett trebokstavsord per rad svenska.put(ordet) def makechildren(word, end, queue): # We know that the word exist, therefore being put in the dup. box. i = 0 start_word = list(word.word) while i < 3: for letter in alfabet: # Changes the word (position i) with the letter start_word[i] = letter # Takes the list and joins them with "" distance between them, a.k.a. making strings. new_word = "".join(start_word) if new_word in svenska: if new_word not in gamla: gamla.put(new_word)
from linkedQFile import LinkedQ from bintreeFile import Bintree svenska = Bintree() # Skapar ett tomt binärt sökträd för de svenska orden gamla = Bintree() # Skapar ett tomt binärt sökträd för de gamla orden '''Sätter in alla orden i filen till det binära sökträdet''' with open("word3.txt", "r", encoding="utf-8") as svenskfil: for rad in svenskfil: ordet = rad.strip() # Ett trebokstavsord per rad if ordet in svenska: pass else: svenska.put(ordet) # in i sökträdet ''' Klassen för våra ParentNodes. Som tar emot ordet och en parent-pekare writechain skriver ut resultatet från startordet till slutordet rekursivt. ''' class ParentNode: def __init__(self, word, parent=None): self.word = word self.parent = parent def writechain(self, child): if child is not None: self.writechain(child.parent) print(child.word)
from bintreeFile import Bintree svenska = Bintree() with open("word3.txt", "r", encoding="utf-8") as svenskfil: for rad in svenskfil: ordet = rad.strip() if ordet in svenska: print(ordet, end=" ") else: svenska.put(ordet) print("\n") engelska = Bintree() with open("engelska.txt", "r", encoding="utf-8") as engelskfil: text = engelskfil.read() words = text.split() for word in words: if word not in engelska: engelska.put( word) # sorterar in engelska ord i trädet (inga dubletter) if word in svenska: # printar ut alla ord som är samma som de svenska print(word, end=' ')