Skip to content

CSPT13-Karthik-Sprint-Challenge-Data-Structures #425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
38 changes: 38 additions & 0 deletions names/BST.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class BSTNode:
def __init__(self, value):
self.value = value
self.left = None
self.right = None

# Insert the given value into the tree
def insert(self, value):
# if root is not empty and value is greater than root, place value to right of root
if (value >= self.value) and (self.right != None):
self.right.insert(value)
# if root is empty and value is greater than root, create a node and place value to right of root
if (value >= self.value) and (self.right == None):
new_node = BSTNode(value)
self.right = new_node
# if root is not empty and value is lesser than root, place value to right of root
if (value < self.value) and (self.left != None):
self.left.insert(value)
# if root is empty and value is lesser than root, create a node and place value to left of root
if (value < self.value) and (self.left == None):
new_node = BSTNode(value)
self.left = new_node

# Return True if the tree contains the value
# False if it does not
def contains(self, target):
if target == self.value:
return True
if (target>self.value):
if self.right is not None:
return self.right.contains(target)
else:
return False
if (target<self.value):
if self.left is not None:
return self.left.contains(target)
else:
return False
53 changes: 47 additions & 6 deletions names/names.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import time

from BST import BSTNode
start_time = time.time()

f = open('names_1.txt', 'r')
Expand All @@ -12,11 +12,27 @@

duplicates = [] # Return the list of duplicates in this data structure

# Replace the nested for loops below with your improvements
for name_1 in names_1:
for name_2 in names_2:
if name_1 == name_2:
duplicates.append(name_1)
# # Replace the nested for loops below with your improvements
# for name_1 in names_1:
# for name_2 in names_2:
# if name_1 == name_2:
# duplicates.append(name_1)

#Using BST to cut runtime

head = names_1[0]
names_1 = names_1[1:]

bst = BSTNode(head)

for name in names_1:
bst.insert(name)

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

# RUNTIME : 0.16530179977416992 seconds , 64 Duplicates

end_time = time.time()
print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
Expand All @@ -26,3 +42,28 @@
# Python has built-in tools that allow for a very efficient approach to this problem
# What's the best time you can accomplish? Thare are no restrictions on techniques or data
# structures, but you may not import any additional libraries that you did not write yourself.

start_time = time.time()

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

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

duplicates1 = [] # Return the list of duplicates in this data structure

def intersection(lst1, lst2):
temp = set(lst2)
lst3 = [value for value in lst1 if value in temp]
return lst3

duplicates1 = intersection(names_3,names_4)

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

##Runtime reduced to 0.003970146179199219 seconds
15 changes: 14 additions & 1 deletion reverse/reverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,17 @@ def contains(self, value):
return False

def reverse_list(self, node, prev):
pass
# Iterative solution
# initialize 3 pointers. Prev = Null, curr = head, next is null
prev = None
current = self.head
# store the next node before changeing it
# change the next pointer to previous. this is where it does reverse
# move prev and current 1 step ahead
# repeat process till current node is not null.
while current:
next = current.next_node
current.next_node = prev
prev=current
current=next
self.head=prev
11 changes: 8 additions & 3 deletions ring_buffer/ring_buffer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
class RingBuffer:
def __init__(self, capacity):
pass
self.capacity = capacity
self.storage = [None]*capacity
self.curr = 0

def append(self, item):
pass
self.storage[self.curr] = item
self.curr += 1
if self.curr == self.capacity:
self.curr=0

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