From 949b49035cd5e3ecc370d325793712ba25518515 Mon Sep 17 00:00:00 2001 From: Neha-Kumari31 Date: Sun, 2 Aug 2020 23:25:43 -0400 Subject: [PATCH 1/2] Finished SC --- names/bst.py | 36 +++++++++++++++++++++++++ names/names.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 names/bst.py diff --git a/names/bst.py b/names/bst.py new file mode 100644 index 000000000..83c34b146 --- /dev/null +++ b/names/bst.py @@ -0,0 +1,36 @@ +class BSTNode: + def __init__(self, value): + self.value = value + self.left = None + self.right = None + + def insert(self, value): + + if value < self.value: + if not self.left: + self.left = BSTNode(value) + else: + self.left.insert(value) + + if value >= self.value: + if not self.right: + self.right = BSTNode(value) + else: + self.right.insert(value) + + + def contains(self, target): + + if target == self.value: + 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) \ No newline at end of file diff --git a/names/names.py b/names/names.py index ea158997f..188152dd1 100644 --- a/names/names.py +++ b/names/names.py @@ -10,13 +10,47 @@ names_2 = f.read().split("\n") # List containing 10000 names f.close() + 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) +# 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: +# if name_1 in names_2: +# duplicates.append(name_1) +from bst import BSTNode + +# CREATE TREE WITH names_1 LIST +start_time = time.time() + +tree = BSTNode(names_1[0]) +for name in names_1: + tree.insert(name) + +duplicates = [name for name in names_2 if tree.contains(name)] + +end_time = time.time() +print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") +print (f"runtime: {end_time - start_time} seconds") + + +# CREATE TREE WITH names_2 LIST (just in case order is better) +# ... it's not +start_time = time.time() + +tree = BSTNode(names_2[0]) +for name in names_2: + tree.insert(name) + +duplicates = [name for name in names_1 if tree.contains(name)] + +end_time = time.time() +print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") +print (f"runtime: {end_time - start_time} seconds") end_time = time.time() print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") @@ -26,3 +60,34 @@ # 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. +# CREATE TREE WITH names_2 LIST (just in case order is better) +# ... it's not +# ... it's not. it's the same +start_time = time.time() + +tree = BSTNode(names_2[0]) + +# 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. + +# LIST COMPREHENSION +# ~ 1.5 seconds which is def better than the original + +start_time = time.time() + +duplicates = [name for name in names_1 if name in names_2] + +end_time = time.time() +print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") +print (f"runtime: {end_time - start_time} seconds") + +# USING SETS + +start_time = time.time() + +duplicates = set(names_1) & set(names_2) + +end_time = time.time() +print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") +print (f"runtime: {end_time - start_time} seconds") From 31c02677821470954a64d354eaf9a051abf4c29c Mon Sep 17 00:00:00 2001 From: Neha-Kumari31 Date: Mon, 28 Sep 2020 23:30:43 -0400 Subject: [PATCH 2/2] Finished SC --- names/bst.py | 37 +++++++++++++++++++++------------- names/names.py | 41 ++++++++++---------------------------- reverse/reverse.py | 13 +++++++++++- ring_buffer/ring_buffer.py | 32 ++++++++++++++++++++++++----- 4 files changed, 72 insertions(+), 51 deletions(-) diff --git a/names/bst.py b/names/bst.py index 83c34b146..84c36a8dc 100644 --- a/names/bst.py +++ b/names/bst.py @@ -1,36 +1,45 @@ +''' +Binary search trees are a data structure that enforce an ordering over +the data they store. That ordering in turn makes it a lot more efficient +at searching for a particular piece of data in the tree. ''' + 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): - + #compare the current value of the node(self.value) if value < self.value: - if not self.left: + #insert the value in left + if self.left is None: + # insert node value if no node in the left side self.left = BSTNode(value) else: + # repeat the process for the next node self.left.insert(value) - if value >= self.value: - if not self.right: - self.right = BSTNode(value) + if self.right is None: + self.right =BSTNode(value) + else: self.right.insert(value) - + # Return True if the tree contains the value + # False if it does not def contains(self, target): - - if target == self.value: + if target ==self.value: return True - if target < self.value: - if not self.left: + if self.left is None: return False else: return self.left.contains(target) - else: - if not self.right: + + if target >=self.value: + if self.right is None: return False - else: - return self.right.contains(target) \ No newline at end of file + else: + return self.right.contains(target) \ No newline at end of file diff --git a/names/names.py b/names/names.py index 188152dd1..e993a3bf2 100644 --- a/names/names.py +++ b/names/names.py @@ -38,34 +38,11 @@ print (f"runtime: {end_time - start_time} seconds") -# CREATE TREE WITH names_2 LIST (just in case order is better) -# ... it's not -start_time = time.time() - -tree = BSTNode(names_2[0]) -for name in names_2: - tree.insert(name) -duplicates = [name for name in names_1 if tree.contains(name)] - -end_time = time.time() -print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") -print (f"runtime: {end_time - start_time} seconds") +# ---------- Stretch Goal ----------- -end_time = time.time() -print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") -print (f"runtime: {end_time - start_time} seconds") -# ---------- Stretch Goal ----------- -# 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. -# CREATE TREE WITH names_2 LIST (just in case order is better) -# ... it's not -# ... it's not. it's the same -start_time = time.time() -tree = BSTNode(names_2[0]) # 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 @@ -74,20 +51,22 @@ # LIST COMPREHENSION # ~ 1.5 seconds which is def better than the original +# CREATE TREE WITH names_2 LIST (just in case order is better) +# ... it's not start_time = time.time() -duplicates = [name for name in names_1 if name in names_2] +tree = BSTNode(names_2[0]) +for name in names_2: + tree.insert(name) + +duplicates = [name for name in names_1 if tree.contains(name)] end_time = time.time() print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") print (f"runtime: {end_time - start_time} seconds") -# USING SETS -start_time = time.time() -duplicates = set(names_1) & set(names_2) -end_time = time.time() -print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") -print (f"runtime: {end_time - start_time} seconds") +# USING SETS + diff --git a/reverse/reverse.py b/reverse/reverse.py index 6116252d1..9728a066e 100644 --- a/reverse/reverse.py +++ b/reverse/reverse.py @@ -39,4 +39,15 @@ def contains(self, value): return False def reverse_list(self, node, prev): - pass + #Points head to the node and set previous node None + prev = None + node = self.head + # continue till node is not empty + while node is not None: + next_node = node.next_node + node.next_node =prev + prev = node + node = next_node + self.head = prev + + diff --git a/ring_buffer/ring_buffer.py b/ring_buffer/ring_buffer.py index 37e9fb0dd..b626f8a19 100644 --- a/ring_buffer/ring_buffer.py +++ b/ring_buffer/ring_buffer.py @@ -1,9 +1,31 @@ class RingBuffer: def __init__(self, capacity): - pass - + self.capacity = capacity + self.data = [] + self.index =0 + + def append(self, item): - pass - + # check if the buffer is full + if len(self.data) == self.capacity: + # overwrite the past value with new item + self.data.pop(self.index) + self.data.insert(self.index,item) + self.index = (self.index +1 ) % self.capacity + else: + self.data.append(item) + def get(self): - pass \ No newline at end of file + return self.data + +buffer = RingBuffer(3) +print(buffer.get()) +buffer.append('a') +buffer.append('b') +buffer.append('c') +print(buffer.get()) +buffer.append('d') +print(buffer.get()) + +buffer.append('e') +print(buffer.get()) \ No newline at end of file