From f55bab35882157bfd71b974c26aa24d67e376453 Mon Sep 17 00:00:00 2001 From: Alie Ibarra Date: Mon, 20 Jun 2022 16:15:47 -0700 Subject: [PATCH 1/5] Passes Tests Through Length --- linked_list/linked_list.py | 74 +++++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 16 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 63993214..9dffd642 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -1,43 +1,72 @@ # Defines a node in the singly linked list +from requests import head + + class Node: - def __init__(self, value, next_node = None): + def __init__(self, value, next_node = None, prev_node = None): self.value = value self.next = next_node + self.previous = prev_node # Defines the singly linked list class LinkedList: def __init__(self): - self.head = None # keep the head private. Not accessible outside this class + self.head = None # keep the head private. Not accessible outside this class + self.tail = None # returns the value in the first node # returns None if the list is empty - # Time Complexity: ? - # Space Complexity: ? + #---------------------- + # Time Complexity: O(1) + # Space Complexity: O(1) def get_first(self): - pass + if self.head: + return self.head.value + return None # method to add a new node with the specific data value in the linked list # insert the new node at the beginning of the linked list - # Time Complexity: ? - # Space Complexity: ? + #---------------------- + # Time Complexity: O(1) + # Space Complexity: O(1) def add_first(self, value): - pass + if self.head is None: + self.head = self.tail = Node(value) + else: + new_node = Node(value, self.head) + self.head.previous = new_node + self.head = new_node # method to find if the linked list contains a node with specified value # returns true if found, false otherwise - # Time Complexity: ? - # Space Complexity: ? + #---------------------- + # Time Complexity: O(n) + # Space Complexity: O(1) def search(self, value): - pass + current = self.head + if current == None: + return False + while current: + if current.value == value: + return True + current = current.next + return False # method that returns the length of the singly linked list - # Time Complexity: ? - # Space Complexity: ? + #---------------------- + # Time Complexity: O(n) + # Space Complexity: O(1) def length(self): - pass + counter = 0 + current = self.head + + while current != None: + current = current.next + counter +=1 + return counter # method that returns the value at a given index in the linked list # index count starts at 0 @@ -89,8 +118,21 @@ def visit(self): # Time Complexity: ? # Space Complexity: ? def reverse(self): - pass - + if not self.head: + return + + current_node = self.head + previous_node = None + + while current_node: + next = current_node.next + current_node.next = previous_node + previous_node = current_node + current_node = next + + self.head = previous_node + + ## Advanced/ Exercises # returns the value at the middle element in the singly linked list # Time Complexity: ? From 65eca5c06e2728e4c65f420836d3e7b4f70f5b58 Mon Sep 17 00:00:00 2001 From: Alie Ibarra Date: Mon, 20 Jun 2022 17:02:20 -0700 Subject: [PATCH 2/5] Passes 24 Tests --- linked_list/linked_list.py | 75 +++++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 17 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 9dffd642..edd63271 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -16,9 +16,9 @@ def __init__(self): self.head = None # keep the head private. Not accessible outside this class self.tail = None + #---------------------- # returns the value in the first node # returns None if the list is empty - #---------------------- # Time Complexity: O(1) # Space Complexity: O(1) def get_first(self): @@ -26,10 +26,9 @@ def get_first(self): return self.head.value return None - + #---------------------- # method to add a new node with the specific data value in the linked list # insert the new node at the beginning of the linked list - #---------------------- # Time Complexity: O(1) # Space Complexity: O(1) def add_first(self, value): @@ -40,9 +39,9 @@ def add_first(self, value): self.head.previous = new_node self.head = new_node + #---------------------- # method to find if the linked list contains a node with specified value # returns true if found, false otherwise - #---------------------- # Time Complexity: O(n) # Space Complexity: O(1) def search(self, value): @@ -55,8 +54,8 @@ def search(self, value): current = current.next return False - # method that returns the length of the singly linked list #---------------------- + # method that returns the length of the singly linked list # Time Complexity: O(n) # Space Complexity: O(1) def length(self): @@ -68,38 +67,77 @@ def length(self): counter +=1 return counter + #---------------------- # method that returns the value at a given index in the linked list # index count starts at 0 # returns None if there are fewer nodes in the linked list than the index value - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def get_at_index(self, index): - pass + counter = 0 + current = self.head + while current != None: + if counter == index: + return current.value + counter += 1 + current = current.next + return None + + #---------------------- # method that returns the value of the last node in the linked list # returns None if the linked list is empty - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def get_last(self): - pass + current = self.head + if current == None: + return None + while current.next != None: + current = current.next + return current.value + + #---------------------- # method that inserts a given value as a new last node in the linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def add_last(self, value): - pass + if self.head == None: + self.add_first(value) + + else: + new_node = Node(value) + current = self.head + while current.next: + current = current.next + current.next = new_node + + #---------------------- # method to return the max value in the linked list # returns the data value and not the node def find_max(self): - pass + max_value = 0 + if self.head == None: + return None + + current = self.head + while current: + if current.value > max_value: + max_value = current.value + current = current.next + + return max_value + #---------------------- # method to delete the first node found with specified value - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def delete(self, value): pass + #---------------------- # method to print all the values in the linked list # Time Complexity: ? # Space Complexity: ? @@ -113,6 +151,7 @@ def visit(self): print(", ".join(helper_list)) + #---------------------- # method to reverse the singly linked list # note: the nodes should be moved and not just the values in the nodes # Time Complexity: ? @@ -133,6 +172,7 @@ def reverse(self): self.head = previous_node + #---------------------- ## Advanced/ Exercises # returns the value at the middle element in the singly linked list # Time Complexity: ? @@ -140,6 +180,7 @@ def reverse(self): def find_middle_value(self): pass + #---------------------- # find the nth node from the end and return its value # assume indexing starts at 0 while counting to n # Time Complexity: ? From ae2ed0bbda96e884bbaba96941f4188b62d21bb0 Mon Sep 17 00:00:00 2001 From: Alie Ibarra Date: Mon, 20 Jun 2022 17:12:47 -0700 Subject: [PATCH 3/5] Passes 27 Tests --- linked_list/linked_list.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index edd63271..485a6132 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -135,7 +135,30 @@ def find_max(self): # Time Complexity: O(n) # Space Complexity: O(1) def delete(self, value): - pass + current = self.head + + if current == None: + return + + while current: + if current.value == value: + if current == self.head: + self.head = current.next + self.head.previous = None + return + + if current == self.tail: + self.tail = current.previous + self.tail.next = None + return + + current.previous.next = current.next + current.next.previous = current.previous + return + else: + current = current.next + + #---------------------- # method to print all the values in the linked list From c417fe9c83e0a32f1252f9a602f0c8466439591b Mon Sep 17 00:00:00 2001 From: Alie Ibarra Date: Tue, 21 Jun 2022 10:30:18 -0700 Subject: [PATCH 4/5] Passes 32 Tests --- linked_list/linked_list.py | 10 +++++++--- tests/linked_list_test.py | 12 ++++++------ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 485a6132..9e7d9d49 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -201,16 +201,20 @@ def reverse(self): # Time Complexity: ? # Space Complexity: ? def find_middle_value(self): - pass + mid_value = int(self.length() / 2) + (print(f"Mid Value: {mid_value}")) + return self.get_at_index(mid_value) #---------------------- # find the nth node from the end and return its value # assume indexing starts at 0 while counting to n # Time Complexity: ? - # Space Complexity: ? + # Space Complexity: O(1) def find_nth_from_end(self, n): - pass + nth_from_end = self.length() - (n+1) + return self.get_at_index(nth_from_end) + #---------------------- # checks if the linked list has a cycle. A cycle exists if any node in the # linked list links to a node already visited. # returns true if a cycle is found, false otherwise. diff --git a/tests/linked_list_test.py b/tests/linked_list_test.py index 58a9d0cf..270064a0 100644 --- a/tests/linked_list_test.py +++ b/tests/linked_list_test.py @@ -197,7 +197,7 @@ def test_reverse_will_reverse_five_element_list(list): for i in range(0, 5): assert list.get_at_index(i) == i -@pytest.mark.skip(reason="Going Further methods") +#@pytest.mark.skip(reason="Going Further methods") def test_find_middle_value_returns_middle_element_of_five_element_list(list): list.add_first(10) list.add_first(30) @@ -206,7 +206,7 @@ def test_find_middle_value_returns_middle_element_of_five_element_list(list): list.add_first(20) assert list.find_middle_value() == 50 -@pytest.mark.skip(reason="Going Further methods") +#@pytest.mark.skip(reason="Going Further methods") def test_find_middle_value_returns_element_at_index_two_of_six_element_list(list): list.add_first(10) list.add_first(30) @@ -216,11 +216,11 @@ def test_find_middle_value_returns_element_at_index_two_of_six_element_list(list list.add_first(100) assert list.find_middle_value() == 60 -@pytest.mark.skip(reason="Going Further methods") +#@pytest.mark.skip(reason="Going Further methods") def test_nth_from_n_when_list_is_empty(list): assert list.find_nth_from_end(3) == None -@pytest.mark.skip(reason="Going Further methods") +#@pytest.mark.skip(reason="Going Further methods") def test_find_nth_from_n_when_length_less_than_n(list): list.add_first(5) list.add_first(4) @@ -230,7 +230,7 @@ def test_find_nth_from_n_when_length_less_than_n(list): assert list.find_nth_from_end(6) == None -@pytest.mark.skip(reason="Going Further methods") +#@pytest.mark.skip(reason="Going Further methods") def test_find_nth_from_n(list): list.add_first(1) list.add_first(2) @@ -243,7 +243,7 @@ def test_find_nth_from_n(list): assert list.find_nth_from_end(3) == 4 assert list.find_nth_from_end(4) == None -@pytest.mark.skip(reason="Going Further methods") +#@pytest.mark.skip(reason="Going Further methods") def test_has_cycle(list): assert list.has_cycle() == False From 097c8122237dba7e616df6195bf6ec3547c177ba Mon Sep 17 00:00:00 2001 From: Alie Ibarra Date: Wed, 22 Jun 2022 15:25:21 -0700 Subject: [PATCH 5/5] Refactored, Passes All required tests and 5/6 Advanced Excercises --- linked_list/linked_list.py | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 9e7d9d49..b7b9fa14 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -155,15 +155,12 @@ def delete(self, value): current.previous.next = current.next current.next.previous = current.previous return - else: - current = current.next - - + current = current.next #---------------------- # method to print all the values in the linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def visit(self): helper_list = [] current = self.head @@ -177,8 +174,8 @@ def visit(self): #---------------------- # method to reverse the singly linked list # note: the nodes should be moved and not just the values in the nodes - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def reverse(self): if not self.head: return @@ -195,20 +192,18 @@ def reverse(self): self.head = previous_node - #---------------------- - ## Advanced/ Exercises + #---------------------- Advanced/ Exercises ----------------------- # returns the value at the middle element in the singly linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def find_middle_value(self): mid_value = int(self.length() / 2) - (print(f"Mid Value: {mid_value}")) return self.get_at_index(mid_value) #---------------------- # find the nth node from the end and return its value # assume indexing starts at 0 while counting to n - # Time Complexity: ? + # Time Complexity: O(n) # Space Complexity: O(1) def find_nth_from_end(self, n): nth_from_end = self.length() - (n+1) @@ -221,7 +216,7 @@ def find_nth_from_end(self, n): # Time Complexity: ? # Space Complexity: ? def has_cycle(self): - pass + return False # Helper method for tests # Creates a cycle in the linked list for testing purposes