Skip to content

initial commit #424

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 4 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
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Sprint Challenge: Data Structures

In this week's Sprint you implemented some classic and fundamental data structures and learned about how to go about evaluating their respective runtimes and performance. This Sprint Challenge aims to assess your comfort with these topics through exercises that build on the data structures you implemented and the algorithmic intuition you've started to build up.
## Initial commit

================================================================================================
======https://github.com/LambdaSchool/Sprint-Challenge--Data-Structures-Python/pull/424=========
================================================================================================


In this week's Sprint you implemented some classic and fundamental data structures and learned about how to go about evaluating their respective run times and performance. This Sprint Challenge aims to assess your comfort with these topics through exercises that build on the data structures you implemented and the algorithmic intuition you've started to build up.

## Instructions

Expand Down
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

########## RETURNING INSERT VALUES ##########

def insert(self, value):
if value < self.value:
if self.left is None:
self.left = BSTNode(value)
else:
self.left.insert(value)
elif value >= self.value:
if self.right is None:
self.right = BSTNode(value)
else:
self.right.insert(value)


########## RETURNING TRUE VALUE ##########
########## IF DOES NOT ITS FALSE #########


def contains(self, target):
if self.value == target:
return True
if target < self.value:
if not self.left:
return False
else:
return self.left.contains(target)
else:
if not self.right:
return False
else:
return self.right.contains(target)
21 changes: 15 additions & 6 deletions names/names.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
from bst import BSTNode

start_time = time.time()

Expand All @@ -11,16 +12,24 @@
f.close()

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

bst = BSTNode("Mike")
# 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)
bst.insert(name_1)

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

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")

########## SPEED TIMES ##########
########## BEFORE runtime: 6.172563314437866 seconds ##########
########## AFTER runtime: 0.1124579906463623 seconds ##########
########## SHAVED NEARLY 6 SECONDS OFF TIME ##########

# ---------- Stretch Goal -----------
# Python has built-in tools that allow for a very efficient approach to this problem
Expand Down
42 changes: 38 additions & 4 deletions reverse/reverse.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Node:
########## INITIALIZE CONSTRUCTOR ##########
def __init__(self, value=None, next_node=None):
self.value = value
self.next_node = next_node
Expand All @@ -13,9 +14,12 @@ def set_next(self, new_next):
self.next_node = new_next

class LinkedList:
########## INITIALIZING THE HEAD ##########
def __init__(self):
self.head = None



########## NODE INSERTED IN FRONT ##########
def add_to_head(self, value):
node = Node(value)

Expand All @@ -24,19 +28,49 @@ def add_to_head(self, value):

self.head = node



########## TRUE OR FALSE ##########
def contains(self, value):
if not self.head:
return False



########## LOOPING THROUGH NODES ##########
current = self.head

while current:
########## MAKING SURE THIS IS THE NODE WE LOOKED FOR ##########
if current.get_value() == value:
return True

current = current.get_next()

return False


########## 3 POINTERS == PREV/NULL == HEAD & NEXT ##########
def reverse_list(self, x=None, y=None):
if self.head == None:
return None
current = self.head
prev = None

while current != None:
next = current.next_node

current.next_node = prev

prev = current
current = next

self.head = prev


########## PRINTING LINKEDLIST ##########
def print_list(self):
current = self.head
while(current):
print(current.value)
current = current.next_node

def reverse_list(self, node, prev):
pass
17 changes: 14 additions & 3 deletions ring_buffer/ring_buffer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
class RingBuffer:
def __init__(self, capacity):
pass
self.capacity = capacity
self.buffer = [] # SIZE/COUNT ##########
self.current = 0 # EMPTY ARRAY - OBJECT ##########

def append(self, item):
pass
########## APPENDING AN ELEMENT ##########
if len(self.buffer) < self.capacity:
self.buffer.append(item)
elif len(self.buffer) == self.capacity:
self.buffer[self.current] = item
# OVERWRITE OLDEST ELEMENT ##########
self.current = (self.current + 1) % self.capacity

def get(self):
pass
########## RETURNING A LIST OF ELEMENTS [OLDEST-NEWEST] ##########
if self.buffer is not None:
# SLICED WITH THE COLON ON THE END IS FOR TESTING AND IF IT IS IN THE FRONT IT WILL FAIL ##########
return self.buffer[:self.current]+self.buffer[self.current:]