Skip to content
Merged
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
53 changes: 53 additions & 0 deletions names/binary_search_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class BinarySearchTree:
def __init__(self, value):
self.value = value
self.left = None
self.right = None

# Insert the given value into the tree
def insert(self, value):
parent = self
node = BinarySearchTree(value)
while True:
if value < parent.value:
if parent.left:
parent = parent.left # traverse down left
else:
parent.left = node
break
else:
if parent.right:
parent = parent.right # traverse down right
else:
parent.right = node
break

# Return True if the tree contains the value
# False if it does not
def contains(self, target):
node = self
while node.value != target:
if target < node.value and node.left:
node = node.left
elif target >= node.value and node.right:
node = node.right
else:
break
return node.value == target

# Return the maximum value found in the tree
def get_max(self):
node = self
while node.right:
node = node.right
return node.value

# Call the function `cb` on the value of each node
# You may use a recursive or iterative approach
def for_each(self, cb):
node = self
cb(node.value)
if node.left:
node.left.for_each(cb)
if node.right:
node.right.for_each(cb)
28 changes: 20 additions & 8 deletions names/names.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
import time
from binary_search_tree import BinarySearchTree

start_time = time.time()

f = open('names_1.txt', 'r')
f = open("names_1.txt", "r")
names_1 = f.read().split("\n") # List containing 10000 names
f.close()

f = open('names_2.txt', 'r')
f = open("names_2.txt", "r")
names_2 = f.read().split("\n") # List containing 10000 names
f.close()

###########

bst = BinarySearchTree(names_1[0])
for name_1 in names_1[1:]:
bst.insert(name_1)

duplicates = []
for name_1 in names_1:
for name_2 in names_2:
if name_1 == name_2:
duplicates.append(name_1)
for name_2 in names_2:
if bst.contains(name_2):
duplicates.append(name_2)

# duplicates = []
# for name_1 in names_1:
# for name_2 in names_2:
# if name_1 == name_2:
# duplicates.append(name_1)

end_time = time.time()
print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print (f"runtime: {end_time - start_time} seconds")
print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print(f"runtime: {end_time - start_time} seconds")

98 changes: 57 additions & 41 deletions reverse/reverse.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,63 @@
class Node:
def __init__(self, value=None, next_node=None):
# the value at this linked list node
self.value = value
# reference to the next node in the list
self.next_node = next_node
def __init__(self, value=None, next_node=None):
# the value at this linked list node
self.value = value
# reference to the next node in the list
self.next_node = next_node

def get_value(self):
return self.value
def get_value(self):
return self.value

def get_next(self):
return self.next_node
def get_next(self):
return self.next_node

def set_next(self, new_next):
# set this node's next_node reference to the passed in node
self.next_node = new_next

def set_next(self, new_next):
# set this node's next_node reference to the passed in node
self.next_node = new_next

class LinkedList:
def __init__(self):
# reference to the head of the list
self.head = None

def add_to_head(self, value):
node = Node(value)
if self.head is not None:
node.set_next(self.head)

self.head = node

def contains(self, value):
if not self.head:
return False
# get a reference to the node we're currently at; update this as we traverse the list
current = self.head
# check to see if we're at a valid node
while current:
# return True if the current value we're looking at matches our target value
if current.get_value() == value:
return True
# update our current node to the current node's next node
current = current.get_next()
# if we've gotten here, then the target node isn't in our list
return False

def reverse_list(self):
# TO BE COMPLETED
pass
def __init__(self):
# reference to the head of the list
self.head = None

def add_to_head(self, value):
node = Node(value)
if self.head is not None:
node.set_next(self.head)

self.head = node

def contains(self, value):
if not self.head:
return False
# get a reference to the node we're currently at; update this as we traverse the list
current = self.head
# check to see if we're at a valid node
while current:
# return True if the current value we're looking at matches our target value
if current.get_value() == value:
return True
# update our current node to the current node's next node
current = current.get_next()
# if we've gotten here, then the target node isn't in our list
return False

def reverse_list(self):
# traverse the list, re-assigning next nodes along the way
if not self.head or not self.head.get_next():
return # length is <= 1

current = self.head
next_node = current.get_next()
previous_node = None

while current.get_next():
current.set_next(previous_node)
previous_node = current
current = next_node
next_node = current.get_next()

current.set_next(previous_node)
self.head = current

24 changes: 16 additions & 8 deletions ring_buffer/ring_buffer.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
from copy import copy


class RingBuffer:
def __init__(self, capacity):
self.capacity = capacity
self.current = 0
self.storage = [None]*capacity
def __init__(self, capacity):
self.capacity = capacity
self.current = 0
self.storage = [None] * capacity

def append(self, item):
self.storage[self.current] = copy(item)
if self.current >= self.capacity - 1:
self.current = 0
else:
self.current += 1

def append(self, item):
pass
def get(self):
return [item for item in self.storage if item is not None]

def get(self):
pass
41 changes: 21 additions & 20 deletions ring_buffer/test_ring_buffer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
from ring_buffer import RingBuffer


class RingBufferTests(unittest.TestCase):
def setUp(self):
self.buffer = RingBuffer(5)
Expand All @@ -9,35 +10,35 @@ def setUp(self):
def test_ring_buffer(self):
self.assertEqual(len(self.buffer.storage), 5)

self.buffer.append('a')
self.buffer.append('b')
self.buffer.append('c')
self.buffer.append('d')
self.buffer.append("a")
self.buffer.append("b")
self.buffer.append("c")
self.buffer.append("d")
self.assertEqual(len(self.buffer.storage), 5)
self.assertEqual(self.buffer.get(), ['a', 'b', 'c', 'd'])
self.assertEqual(self.buffer.get(), ["a", "b", "c", "d"])

self.buffer.append('e')
self.buffer.append("e")
self.assertEqual(len(self.buffer.storage), 5)
self.assertEqual(self.buffer.get(), ['a', 'b', 'c', 'd', 'e'])
self.assertEqual(self.buffer.get(), ["a", "b", "c", "d", "e"])

self.buffer.append('f')
self.buffer.append("f")
self.assertEqual(len(self.buffer.storage), 5)
self.assertEqual(self.buffer.get(), ['f', 'b', 'c', 'd', 'e'])
self.assertEqual(self.buffer.get(), ["f", "b", "c", "d", "e"])

self.buffer.append('g')
self.buffer.append('h')
self.buffer.append('i')
self.buffer.append("g")
self.buffer.append("h")
self.buffer.append("i")
self.assertEqual(len(self.buffer.storage), 5)
self.assertEqual(self.buffer.get(), ['f', 'g', 'h', 'i', 'e'])
self.buffer.append('j')
self.buffer.append('k')
self.assertEqual(self.buffer.get(), ['k', 'g', 'h', 'i', 'j'])
self.assertEqual(self.buffer.get(), ["f", "g", "h", "i", "e"])

self.buffer.append("j")
self.buffer.append("k")
self.assertEqual(self.buffer.get(), ["k", "g", "h", "i", "j"])

for i in range(50):
self.buffer_2.append(i)
self.assertEqual(self.buffer2.get(), [45, 46, 47, 48, 49])
self.assertEqual(self.buffer_2.get(), [45, 46, 47, 48, 49])


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()