Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
632c34a
Ring-Buffer: Append: Testing for less than capacity and doing a direc…
G3Ram Jan 11, 2020
c369d3c
Ring-Buffer: Get: Completed the get functionality. This was just disp…
G3Ram Jan 11, 2020
e07e41b
Ring-buffer:Append: If the current is head then adding an value will …
G3Ram Jan 11, 2020
b28c237
Ring-Buffer: Append: If the current is tail then the next value to be…
G3Ram Jan 11, 2020
4e92c8d
Ring-Buffer: Append: If its a value in the middle then it will be rep…
G3Ram Jan 11, 2020
57e7205
Ring-Buffer: Append and Get: Fully tested. Test runs ok
G3Ram Jan 11, 2020
cf6d9be
Ring-Buffer: ArrayRingBuffer: Fixing the issue of self.storage throwi…
G3Ram Jan 11, 2020
a9ea94b
Ring-Buffer: Array ring buffer: Completed get
G3Ram Jan 11, 2020
af8c7d0
Ring-Buffer:ArrayRingBuffer: Added the return statement for get
G3Ram Jan 11, 2020
979c512
STRETCH: Ring Buffer Completed. Tested all ok.
G3Ram Jan 11, 2020
655c622
Names: Used list comprehension to reduce the time to 2.6 sec
G3Ram Jan 11, 2020
8c4374f
Names: Added pseudo code
G3Ram Jan 11, 2020
c9bf16d
Names: Wowgit add . Using BST reduced the runttime to 0.123sec
G3Ram Jan 11, 2020
1597819
Names: Adding binary search from previous homework
G3Ram Jan 11, 2020
9d8a04d
STRETCH: Names: Completed and tested. BST - 0.131, Binary Search - 0.046
G3Ram Jan 11, 2020
701c59a
Reverse: Pseudo code
G3Ram Jan 11, 2020
328cf93
Reverse: Very very confusing. Got a code written. yet to test it out
G3Ram Jan 11, 2020
48271f3
Reverse: Completed and tested. All test passed. Runs ok
G3Ram Jan 11, 2020
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
133 changes: 133 additions & 0 deletions names/binary_search_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@

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):
# solution without recursion
# current = self
# traverse_nodes = True
# while traverse_nodes:
# if current.value > value and current.left:
# current = current.left
# elif current.value <= value and current.right:
# current = current.right
# elif current.value > value and not current.left:
# current.left = BinarySearchTree(value)
# traverse_nodes = False
# elif current.value < value and not current.right:
# current.right = BinarySearchTree(value)
# traverse_nodes = False
# check if new value is less than the current node
if value < self.value:
#is there already a value at self.left
# If there is no self.left value set the new left child to be the value
if not self.left:
self.left = BinarySearchTree(value)
else:
self.left.insert(value)
else:
if not self.right:
self.right=BinarySearchTree(value)
else:
self.right.insert(value)


# Return True if the tree contains the value
# False if it does not

def contains(self, target):
# current = self
# traverse_nodes = True
# while traverse_nodes:
# if current.value is target:
# return True
# elif current.value > target and current.left:
# current = current.left
# elif current.value <= target and current.right:
# current = current.right
# elif current.value > target and not current.left:
# return False
# elif current.value < target and not current.right:
# return False
if self.value == target:
return True
if target < self.value:
# Go left
if not self.left:
return False
else:
return self.left.contains(target)
else:
#Go right
if not self.right:
return False
else:
return self.right.contains(target)


# Return the maximum value found in the tree
def get_max(self):
# highest = self.value
# current = self
# traverse_nodes = True
# while traverse_nodes:
# if current.right:
# current = current.right
# elif not current.right:
# traverse_nodes = False
# if current.value >= highest:
# highest = current.value
# return highest
if not self:
return None

if not self.right:
return self.value
self.right.get_max()

# current_tree_root = self
# while current_tree_root.right:
# current_tree_root = current_tree_root.right
# return current_tree_root.value

# Call the function `cb` on the value of each node
# You may use a recursive or iterative approach
def for_each(self, cb):
cb(self.value)

if self.left:
self.left.for_each(cb)
if self.right:
self.right.for_each(cb)

# DAY 2 Project -----------------------

# Print all the values in order from low to high
# Hint: Use a recursive, depth first traversal
def in_order_print(self, node):
pass

# Print the value of every node, starting with the given node,
# in an iterative breadth first traversal
def bft_print(self, node):
pass

# Print the value of every node, starting with the given node,
# in an iterative depth first traversal
def dft_print(self, node):
pass

# STRETCH Goals -------------------------
# Note: Research may be required

# Print In-order recursive DFT
def pre_order_dft(self, node):
pass

# Print Post-order recursive DFT
def post_order_dft(self, node):
pass
53 changes: 49 additions & 4 deletions names/names.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
from binary_search_tree import BinarySearchTree

start_time = time.time()

Expand All @@ -11,10 +12,23 @@
f.close()

duplicates = []
for name_1 in names_1:
for name_2 in names_2:
if name_1 == name_2:
duplicates.append(name_1)
# for name_1 in names_1:
# for name_2 in names_2:
# if name_1 == name_2:
# duplicates.append(name_1)
# duplicates = [x for x in names_1 for y in names_2 if x == y]
# Using Binary search tree
# Input the contents in names1 to the tree
# Loop names2 and traverse through binary search tree to see if it exists
# update duplicate if exists

binarySearchTree = BinarySearchTree(names_1[0])
for name in names_1:
binarySearchTree.insert(name)

for name in names_2:
if binarySearchTree.contains(name):
duplicates.append(name)

end_time = time.time()
print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
Expand All @@ -24,3 +38,34 @@
# Python has built-in tools that allow for a very efficient approach to this problem
# What's the best time you can accomplish with no restrictions on techniques or data
# structures?

# For the stretch goal I am going to use Binary search.
# Sort the first array and then loop the second array to find item in the first sorted array

def binary_search(arr, target):
# arr.sort()
if len(arr) == 0:
return -1 # array empty

low = 0
high = len(arr)-1
while low <= high:
middle = (low + high) // 2
if arr[middle] == target:
return 1
elif arr[middle] > target:
high = middle - 1
else:
low = middle + 1
return -1

duplicates = []
start_time = time.time()
names_1.sort()
for name in names_2:
if binary_search(names_1, name ) == 1:
duplicates.append(name)
end_time = time.time()

print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print (f"runtime: {end_time - start_time} seconds")
35 changes: 34 additions & 1 deletion reverse/reverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,37 @@ def contains(self, value):

def reverse_list(self):
# TO BE COMPLETED
pass
# The last node becomes the head
# The links are fully reversed. So loop through and push the .next = .next.next
# check if there is a head
if not self.head:
return self.head
current_node = self.head
previous_node = None
next_node = current_node.next_node
while (next_node):
# Because we are reversing the order
current_node.next_node = previous_node
previous_node = current_node
current_node = next_node
next_node = current_node.next_node
current_node.next_node = previous_node #For the last node
self.head = current_node
return self.head.value

def __str__(self):
arr = []
if (self.head is not None):
current_node = self.head
while (current_node.next_node):
arr.append(current_node.value)
current_node = current_node.next_node
arr.append(current_node.value)
return str(arr)

silly = LinkedList()
silly.add_to_head('a')
silly.add_to_head('b')
silly.add_to_head('c')
silly.reverse_list()
print(silly)
118 changes: 114 additions & 4 deletions ring_buffer/ring_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,139 @@
class RingBuffer:
def __init__(self, capacity):
self.capacity = capacity
# current refers to the current item that needs to be removed or replaced
self.current = None
self.storage = DoublyLinkedList()

def append(self, item):
pass
#Check if the length is less than the capacity. If so it does not need any replacement.
# It just needs to be added to the double linked list
# Also if the length is the same as capacity then the first item pushed in will be the
# one to be replaced.
if self.storage.length < self.capacity:
self.storage.add_to_tail(item)
if (self.storage.length == self.capacity):
self.current = self.storage.head
else:
if (self.current is self.storage.head):
print("------CURRENT IS HEAD ----------")
self.storage.head.value = item
self.current = self.storage.head.next
elif (self.current is self.storage.tail):
print("------CURRENT IS TAIL ----------")
self.storage.tail.value = item
self.current = self.storage.head
else:
i = 1
current_node = self.storage.head
# We will be traversing the DLL and replace the value
# To do this we will need to create a new DLL and keep adding the values
# from old list and change the value which equals current
print("------CREATING NEW DLL TO REPLACE----------")
print("------CURRENT ITEM TO REPLACE IS ----", self.current.value)
new_dll = DoublyLinkedList()
is_value_updated = False
while (i<self.storage.length+1):
if (self.current.value is current_node.value and not is_value_updated):
current_node.value = item
if (current_node.next is None):
self.current = self.storage.head
else:
self.current = current_node.next
is_value_updated = True
new_dll.add_to_tail(current_node.value)
i += 1
current_node = current_node.next
self.storage = new_dll



def get(self):
# Note: This is the only [] allowed
list_buffer_contents = []

# TODO: Your code here
i = 1
current_node = self.storage.head
while (i < self.storage.length + 1):
if (current_node.value is not None):
list_buffer_contents.append(current_node.value)
current_node = current_node.next
i += 1

return list_buffer_contents

# ring_buffer = RingBuffer(5)
# ring_buffer.append('a')
# ring_buffer.append('b')
# ring_buffer.append('c')
# ring_buffer.append('d')
# print(ring_buffer.storage.length)
# print(ring_buffer.get())
# ring_buffer.append('e')
# print(ring_buffer.storage.length)
# print(ring_buffer.get())
# ring_buffer.append('f')
# print(ring_buffer.storage.length)
# print(ring_buffer.get())
# ring_buffer.append('g')
# print('------ADDING H AND I-----')
# ring_buffer.append('h')
# ring_buffer.append('i')
# print(ring_buffer.storage.length)
# print(ring_buffer.get()) #['f', 'g', 'h', 'i', 'e']

# ring_buffer.append('j')
# print(ring_buffer.storage.length)
# print(ring_buffer.get())
# ring_buffer.append('k')
# print(ring_buffer.get()) #['k', 'g', 'h', 'i', 'j']

# ----------------Stretch Goal-------------------


class ArrayRingBuffer:
def __init__(self, capacity):
pass
self.capacity = capacity
# Because we are using Python List it is better to set the current as index.
# Not setting the current as value cause two values can be same so swapping out will
# be difficult. Index is the right approach
self.current = 0
self.storage = ['' for x in range(capacity)]

def append(self, item):
pass
print("!!!!!!!!!!!!!!!ITEM IS ---------->>", item)
self.storage[self.current] = item
if (self.current == self.capacity-1):
self.current = 0
else:
self.current += 1

def get(self):
pass
return [x for x in self.storage if x != '']

ring_buffer = ArrayRingBuffer(5)
ring_buffer.append('a')
ring_buffer.append('b')
ring_buffer.append('c')
ring_buffer.append('d')
print(len(ring_buffer.storage))
print(ring_buffer.get())
ring_buffer.append('e')
print(len(ring_buffer.storage))
print(ring_buffer.get())
ring_buffer.append('f')
print(len(ring_buffer.storage))
print(ring_buffer.get())
ring_buffer.append('g')
print('------ADDING H AND I-----')
ring_buffer.append('h')
ring_buffer.append('i')
print(len(ring_buffer.storage))
print(ring_buffer.get()) #['f', 'g', 'h', 'i', 'e']

ring_buffer.append('j')
print(len(ring_buffer.storage))
print(ring_buffer.get())
ring_buffer.append('k')
print(ring_buffer.get()) #['k', 'g', 'h', 'i', 'j']