Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 112 additions & 16 deletions python/bst.py
Original file line number Diff line number Diff line change
@@ -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.")
80 changes: 57 additions & 23 deletions python/hash.py
Original file line number Diff line number Diff line change
@@ -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.")
79 changes: 65 additions & 14 deletions python/linked_list.py
Original file line number Diff line number Diff line change
@@ -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.data
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)
25 changes: 15 additions & 10 deletions python/queue.py
Original file line number Diff line number Diff line change
@@ -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)
25 changes: 15 additions & 10 deletions python/stack.py
Original file line number Diff line number Diff line change
@@ -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)