From ce010915f0a9edb2fa5386c921abcf24f8d14217 Mon Sep 17 00:00:00 2001 From: Julian West Date: Fri, 22 May 2020 22:24:05 -0400 Subject: [PATCH 1/3] Add Stack, Queue, LinkedList w/print --- python/linked_list.py | 79 +++++++++++++++++++++++++++++++++++-------- python/queue.py | 25 ++++++++------ python/stack.py | 25 ++++++++------ 3 files changed, 95 insertions(+), 34 deletions(-) diff --git a/python/linked_list.py b/python/linked_list.py index 7692833..62049ed 100644 --- a/python/linked_list.py +++ b/python/linked_list.py @@ -1,19 +1,70 @@ -class LinkList: - # write your __init__ method here that should store a 'head' value which the first Node in the LinkedList and a 'length' value which is the total number of Nodes in the LinkedList +class LinkedList: - def add(self, data): - # write your code to ADD an element to the Linked List - pass + def __init__(self): + self.head = None + self.tail = None + self.length = 0 - def remove(self, data): - # write your code to REMOVE an element from the Linked List - pass + def __repr__(self): + node = self.head + string = '' + index = 0 + while node: + string += f"{index}. {node.data}\n" + index += 1 + node = node.next + return string + + def add(self, data): + node = Node(data) + if self.head is None: + self.head = node + self.tail = node + else: + self.tail.next = node + node.next = None + self.tail = node + self.length += 1 + + def remove(self, data): + node = self.head + prev = None + while node: + if node.data == data: + if prev: + prev.next = node.next + else: + self.head = node.next + if node.next is None: + self.tail = node.next + del node + self.length -= 1 + return True + node = node.next + return False + + def get(self, data): + node = self.head + while node: + if node.data == data: + return node + node = node.next - def get(self, element_to_get): - # write you code to GET and return an element from the Linked List - pass -# ----- Node ------ class Node: - # store your DATA and NEXT values here - pass + def __init__(self, data): + self.data = data + self.next = None + + +if __name__ == '__main__': + ll = LinkedList() + for i in range(10): + ll.add(i) + print(ll) + print(ll.remove(0)) + print("Removed 0:") + print(ll) + ll.add(10) + print("Added 10:") + print(ll) diff --git a/python/queue.py b/python/queue.py index f52c1a1..6c40aa0 100644 --- a/python/queue.py +++ b/python/queue.py @@ -1,14 +1,19 @@ class Queue: - # write your __init__ method here that should store a 'total' value which is the total number of elements in the Queue and a 'queue' value which is an array of stored values in the Queue + # write your __init__ method here that should store a 'total' value which is the total number of elements in the Queue and a 'queue' value which is an array of stored values in the Queue - def enqueue(self): - # write your code to add data to the Queue following FIFO and return the Queue - pass + def __init__(self): + self.total = 0 # ?? + self.queue = [] - def dequeue(self, data): - # write your code to removes the data to the Queue following FIFO and return the Queue - pass + def enqueue(self, data): + # write your code to add data to the Queue following FIFO and return the Queue + self.queue.append(data) + return self.queue - def size(self): - # write your code that returns the size of the Queue - pass + def dequeue(self): + # write your code to removes the data to the Queue following FIFO and return the Queue + return self.queue.pop(0) + + def size(self): + # write your code that returns the size of the Queue + return len(self.queue) diff --git a/python/stack.py b/python/stack.py index dd102b1..d9a8bdf 100644 --- a/python/stack.py +++ b/python/stack.py @@ -1,14 +1,19 @@ class Stack: - # write your __init__ method here that should store a 'total' value which is the total number of elements in the Stack and a 'stack' value which is an array of stored values in the Stack + # write your __init__ method here that should store a 'total' value which is the total number of elements in the Stack and a 'stack' value which is an array of stored values in the Stack - def push(self): - # write your code to add data following LIFO and return the Stack - pass + def __init__(self): + self.total = 0 # ?? + self.stack = [] - def pop(self, data): - # write your code to removes the data following LIFO and return the Stack - pass + def push(self, data): + # write your code to add data following LIFO and return the Stack + self.stack.append(data) + return self.stack - def size(self): - # write your code that returns the size of the Stack - pass + def pop(self): + # write your code to removes the data following LIFO and return the Stack + return self.stack.pop() + + def size(self): + # write your code that returns the size of the Stack + return len(self.stack) From 34d240ccb41e8c109c2204068ed714789944165e Mon Sep 17 00:00:00 2001 From: Julian West Date: Sat, 23 May 2020 00:08:05 -0400 Subject: [PATCH 2/3] Add Hash --- python/hash.py | 80 ++++++++++++++++++++++++++++++------------- python/linked_list.py | 2 +- 2 files changed, 58 insertions(+), 24 deletions(-) diff --git a/python/hash.py b/python/hash.py index 6f3b20c..fe2933b 100644 --- a/python/hash.py +++ b/python/hash.py @@ -1,24 +1,58 @@ +import random + +from linked_list import LinkedList + + class HashTable: - def __init__(self, number_of_buckets): - # self.number_of_buckets = number_of_buckets - # self.table = [None] * self.number_of_buckets - pass - - def hash(self, value): - # here is where you'll turn your 'value' into a hash value that will return the index of your table to insert value - # IMPORTANT: Think about how you'd store values into the same index - pass - - def set(self, value): - # here is where you'll perform your logic to insert the value into your table - # you'll also call your hash method here to get the index where you'll store the value - pass - - def get(self, value): - # here is where you'll retreive your value from the hash table - # IMPORTANT: Think about how you'd retreive a value that from an index that has multiple values - pass - - def has_key(self, value): - # here is where you'll return a True or False value if there is a value stored at a specific index in your HashTable - pass + def __init__(self, number_of_buckets): + self.number_of_buckets = number_of_buckets + self.table = [LinkedList() for _ in range(self.number_of_buckets)] + + def __repr__(self): + string = '' + for index, bucket in enumerate(self.table): + string += f"{index}:\n{bucket}" + # for node in bucket: + # print(node) + return string + + def hash(self, value): + # here is where you'll turn your 'value' into a hash value that will return the index of your table to insert value + # IMPORTANT: Think about how you'd store values into the same index + h = 0 + for index, char in enumerate(value[:10]): + h += index * ord(char) + return h % self.number_of_buckets + + def set(self, value): + # here is where you'll perform your logic to insert the value into your table + # you'll also call your hash method here to get the index where you'll store the value + self.table[self.hash(value)].add(value) + + def get(self, value): + # here is where you'll retreive your value from the hash table + # IMPORTANT: Think about how you'd retreive a value that from an index that has multiple values + return self.table[self.hash(value)].get(value) + + def has_key(self, value): + # here is where you'll return a True or False value if there is a value stored at a specific index in your HashTable + return bool(self.table[hash(value)].length) + + +if __name__ == '__main__': + hash_table = HashTable(19) + words = [] + for i in range(100): + word = '' + for j in range(random.randint(3, 16)): + word += chr(random.randint(65, 90)) + data = f"{i:3} {word}" + # print(data) + words.append(data) + hash_table.set(data) + print(hash_table) + for word in words: + hash_word = hash_table.get(word) + if hash_word != word: + print(f"{word} != {hash_word}") + print("Done.") diff --git a/python/linked_list.py b/python/linked_list.py index 62049ed..61d8ef0 100644 --- a/python/linked_list.py +++ b/python/linked_list.py @@ -47,7 +47,7 @@ def get(self, data): node = self.head while node: if node.data == data: - return node + return node.data node = node.next From 605013e8102e6195a9318a873058793ca9a465df Mon Sep 17 00:00:00 2001 From: Julian West Date: Sat, 23 May 2020 09:06:05 -0400 Subject: [PATCH 3/3] Add BST --- python/bst.py | 128 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 112 insertions(+), 16 deletions(-) diff --git a/python/bst.py b/python/bst.py index 5cb2488..d517153 100644 --- a/python/bst.py +++ b/python/bst.py @@ -1,23 +1,119 @@ +import random + + +class Node: + # store your DATA and LEFT and RIGHT values here + def __init__(self, value): + self.value = value + self.left = None + self.right = None + + def __repr__(self): + return f"Value: {self.value} <- Left: {self.left} -> Right: {self.right}\n" + + class Bst: - def __init__(self): - self.parent = None - pass + def __init__(self): + self.root = None - def insert(self, value): - #This is where you will insert a value into the Binary Search Tree - pass + def __repr__(self): + string = '' + node = self.root + if node: + string += str(node) + if node.left: + string += str(node.left) + if node.right: + string += str(node.right) + return string - def contains(self, value): - # this is where you'll search the BST and return TRUE or FALSE if the value exists in the BST - pass + def insert(self, value): + # This is where you will insert a value into the Binary Search Tree + node = self.root + if node is None: + self.root = Node(value) + return True + add_left = None + while node: + prev = node + if value < node.value: + node = node.left + add_left = True + elif value > node.value: + node = node.right + add_left = False + else: + return False + if add_left: + prev.left = Node(value) + else: + prev.right = Node(value) - def remove(self, value): - # this is where you will remove a value from the BST - pass + def contains(self, value): + # this is where you'll search the BST and return TRUE or FALSE if the value exists in the BST + node = self.root + while node: + if value == node.value: + return True + if value < node.value: + node = node.left + else: + node = node.right + return False + def move(self, orphan: Node): + node = self.root + prev = None + is_left = None + while node: + prev = node + if orphan.value < node.value: + node = node.left + is_left = True + elif orphan.value > node.value: + node = node.right + is_left = False + else: + raise Exception() + if is_left: + prev.left = orphan + else: + prev.right = orphan + def remove(self, value): + # this is where you will remove a value from the BST + node = self.root + parent = None + while node: + if value == node.value: + if parent is None: + parent = self.root + if node.left: + parent = node.left + if node.right: + self.move(node.right) + elif node.right: + parent = node.right + del node + return True + if value < node.value: + parent = node + node = node.left + else: + parent = node + node = node.right + return False -# ----- Node ------ -class Node: - # store your DATA and LEFT and RIGHT values here - pass + +if __name__ == '__main__': + bst = Bst() + numbers = [] + for i in range(100): + number = random.randint(0, 1000) + numbers.append(number) + bst.insert(number) + print(bst) + for number in numbers: + if not bst.contains(number): + print(f"{number} missing") + print("Done.")