コード例 #1
0
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)
コード例 #2
0
def magic(queue):
    final_queue = LinkedQ()
    i = True
    while not queue.is_empty():
        value = queue.get()

        if i == False:
            final_queue.put(value)  #Put place value last

        elif i == True:
            queue.put(value)

        i = not (i)  # Toggles 'i'

    return final_queue
コード例 #3
0
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
コード例 #4
0
def queue_init():
    value_str = input(
        "Vilken ordning ligger korten i? Separera korten med mellanslag: ")
    value_list = value_str.split(" ")

    # Puts everything exept empty spaces in the list
    new_value_list = []
    for value in value_list:
        if value != "":
            new_value_list.append(value)
    value_list = new_value_list

    # Creates LinkedQ object
    queue = LinkedQ()
    for value in value_list:
        queue.put(value)

    return (queue)
コード例 #5
0
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)
コード例 #6
0
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