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])
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(): 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 svenska.exists(ordet): print(ordet, end = ' ') else: svenska.put(ordet) print('\n')
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')
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)
# 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 print("Running binary tree: ") starttime = time.time() tree.exists(x) print("Time: "+ str( round(time.time() - starttime,decimals) )) # find the value in the list print("Running list: ") starttime = time.time()
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 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')
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)
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:
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
# 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()
# puts in words from inputted wordlist into the inputted tree def text_putter(tree, word_list): for word in word_list: tree.put(word) return tree starttime = time.time() eng_word_list = text_preparer("engelska.txt") swe_word_list = text_preparer("word3.txt") tree_swe = Bintree() tree_eng = Bintree() tree_swe = text_putter(tree_swe, swe_word_list) tree_eng = text_putter(tree_eng, eng_word_list) # for each word in the english list, check if the word exists in both the swedish tree and english tree # and that it occurs at least once for word in eng_word_list: swe_exist, swe_node = tree_swe.exists(word) eng_exist, eng_node = tree_eng.exists(word) if swe_exist and eng_exist and eng_node.value_occurence > 0: if eng_node.value_occurence > 1: # if a doublette is found, print only the word once, then set the occurence to zero eng_node.value_occurence = 0 print(word) print("\nTime: " + str(round(time.time() - starttime, 2)))