From bb24990a5b84120468c387434f6c461d5ed4b37f Mon Sep 17 00:00:00 2001 From: Zandra Nguyen Date: Mon, 4 Apr 2022 20:25:13 -0700 Subject: [PATCH 1/2] Completed assignment. Pushing for PR --- linked_list/linked_list.py | 186 ++++++++++++++++++++++++++++++------- tests/linked_list_test.py | 43 +++++++-- 2 files changed, 187 insertions(+), 42 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 63993214..77b9878e 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -2,78 +2,177 @@ # Defines a node in the singly linked list class Node: - def __init__(self, value, next_node = None): + def __init__(self, value, next_node=None): self.value = value self.next = next_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 # 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 == None: + return None + return self.head.value # 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 + # This method adds a new node with the given value to the head of the list + new_node = Node(value) + new_node.next = self.head + 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 + # This method returns True or False if the list contains the given value. + if self.head == None: + return False + + current = self.head + + while current != None: + 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 + # This method returns the size of the list. + current = self.head + count = 0 + + while current != None: + count += 1 + current = current.next + + return count # 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 + # This method returns the value of the node at the given index. It returns None if the list does not have that many elements. + if index < 0: + return None + + current_index = 0 + current = self.head + + while current is not None and current_index < index: + current = current.next + current_index += 1 + + if current is None: + return None + + return current.value # 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 + # This method returns the value of the last node in the list. + if self.head == None: + return None + + current = self.head + + 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(n)? + def add_last(self, value): - pass + # This method adds a new node to the rear of the list. + if self.head == None: + new_node = Node(value) + self.head = new_node + return + + current = self.head + + while current.next != None: + current = current.next + + # new_node = Node(value) + # current.next = new_node + current.next = Node(value) # method to return the max value in the linked list # returns the data value and not the node + def find_max(self): - pass + # This method finds the largest value in the list, assuming you can use >, or < to compare each element in the list. + if self.head == None: + return None + + current = self.head + max_value = -100 + + while current != None: + 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 + + # If the first node is the one we want to delete, that's a + # special case. + if self.head and self.head.value == value: + self.head = self.head.next + return + + # Now iterate through the rest of the list and check + # for the target value at the node _after_ the current one. + current = self.head + while current: + if current.next and current.next.value == value: + current.next = current.next.next + return + + current = current.next # method to print all the values in the linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n)? + # Space Complexity: O(n)? + def visit(self): helper_list = [] current = self.head @@ -81,20 +180,37 @@ def visit(self): while current: helper_list.append(str(current.value)) current = current.next - + 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: ? - # Space Complexity: ? + # Time Complexity: O(n) ? + # Space Complexity: O(1)? + def reverse(self): - pass - + # If the list is empty or has only one element, then there is nothing to do + if not self.head or not self.head.next: + return + + # The list is of length AT LEAST 2. Time to reverse + # the pointers + p1 = self.head + p2 = self.head.next + p1.next = None # this is now the end of the new list + while p2: + temp = p2.next + p2.next = p1 # p2 now points at the previous node + p1 = p2 # advance p1 to the next node (in the old ordering) + if not temp: # if we're about to walk off the list, this is the new head + self.head = p2 + p2 = temp + ## Advanced/ Exercises # returns the value at the middle element in the singly linked list # Time Complexity: ? # Space Complexity: ? + def find_middle_value(self): pass @@ -125,4 +241,4 @@ def create_cycle(self): while current.next != None: current = current.next - current.next = self.head # make the last node link to first node + current.next = self.head # make the last node link to first node diff --git a/tests/linked_list_test.py b/tests/linked_list_test.py index 58a9d0cf..a14d6785 100644 --- a/tests/linked_list_test.py +++ b/tests/linked_list_test.py @@ -2,6 +2,8 @@ from linked_list.linked_list import LinkedList # Fixture to start each test with a new LinkedList + + @pytest.fixture() def list() -> LinkedList: return LinkedList() @@ -10,9 +12,11 @@ def list() -> LinkedList: def test_list_can_be_initialized(list): assert isinstance(list, LinkedList) + def test_get_first_returns_none_for_empty_list(list): assert list.get_first() == None + def test_add_first_adds_to_start_of_list(list): list.add_first(3) assert list.get_first() == 3 @@ -33,6 +37,7 @@ def test_search_returns_false_for_items_not_in_list(list): list.add_first(4) assert list.search(5) == False + def test_search_returns_true_for_items_in_front_of_list(list): list.add_first(3) list.add_first(4) @@ -53,18 +58,22 @@ def test_search_returns_true_for_items_in_middle_of_list(list): list.add_first(6) assert list.search(5) == True + def test_length_returns_0_for_empty_list(list): assert list.length() == 0 + def test_length_returns_1_for_list_with_1_element(list): list.add_first(3) assert list.length() == 1 + def test_length_returns_2_for_list_with_2_elements(list): list.add_first(3) list.add_first(4) assert list.length() == 2 + def test_length_increases_in_value_with_each_add_first(list): count = 0 @@ -73,9 +82,11 @@ def test_length_increases_in_value_with_each_add_first(list): count += 1 assert list.length() == count + def test_get_at_index_with_empty_list_is_none(list): assert list.get_at_index(3) == None + def test_get_at_index(list): list.add_first(1) @@ -88,9 +99,11 @@ def test_get_at_index(list): assert list.get_at_index(2) == 2 assert list.get_at_index(3) == 1 + def test_get_last_on_empty_list_returns_none(list): assert list.get_last() == None + def test_get_last_returns_the_last_element_of_a_list(list): list.add_first(5) assert list.get_last() == 5 @@ -98,10 +111,12 @@ def test_get_last_returns_the_last_element_of_a_list(list): list.add_first(4) assert list.get_last() == 5 + def test_add_last_on_empty_list(list): list.add_last(1) assert list.get_at_index(0) == 1 + def test_add_last_increases_length_and_new_item_appears_at_back_of_list(list): assert list.length() == 0 @@ -131,6 +146,7 @@ def test_add_last_increases_length_and_new_item_appears_at_back_of_list(list): def test_find_max_on_empty_list_should_return_none(list): assert list.find_max() == None + def test_find_max_on_list_where_max_is_first_item(list): list.add_first(1) list.add_first(2) @@ -140,6 +156,7 @@ def test_find_max_on_list_where_max_is_first_item(list): assert list.find_max() == 5 + def test_find_max_on_list_where_max_is_last_item(list): list.add_first(27) list.add_first(4) @@ -149,6 +166,7 @@ def test_find_max_on_list_where_max_is_last_item(list): assert list.find_max() == 27 + def test_find_max_on_list_where_max_is_middle_item(list): list.add_first(1) list.add_first(2) @@ -158,9 +176,11 @@ def test_find_max_on_list_where_max_is_middle_item(list): assert list.find_max() == 27 + def test_delete_on_empty_list_returns_none(list): assert list.delete(2) == None + def test_delete_can_remove_first_element_of_list(list): list.add_first(5) list.add_first(4) @@ -170,6 +190,7 @@ def test_delete_can_remove_first_element_of_list(list): assert list.length() == 2 assert list.get_last() == 5 + def test_delete_can_remove_last_element_of_list(list): list.add_first(5) list.add_first(4) @@ -179,6 +200,7 @@ def test_delete_can_remove_last_element_of_list(list): assert list.length() == 2 assert list.get_last() == 4 + def test_delete_can_remove_middle_element_of_list(list): list.add_first(5) list.add_first(4) @@ -188,15 +210,17 @@ def test_delete_can_remove_middle_element_of_list(list): assert list.length() == 2 assert list.get_last() == 5 + def test_reverse_will_reverse_five_element_list(list): for i in range(0, 5): list.add_first(i) - + list.reverse() - + for i in range(0, 5): assert list.get_at_index(i) == i + @pytest.mark.skip(reason="Going Further methods") def test_find_middle_value_returns_middle_element_of_five_element_list(list): list.add_first(10) @@ -206,6 +230,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") def test_find_middle_value_returns_element_at_index_two_of_six_element_list(list): list.add_first(10) @@ -216,10 +241,12 @@ 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") 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") def test_find_nth_from_n_when_length_less_than_n(list): list.add_first(5) @@ -230,6 +257,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") def test_find_nth_from_n(list): list.add_first(1) @@ -237,11 +265,12 @@ def test_find_nth_from_n(list): list.add_first(3) list.add_first(4) - assert list.find_nth_from_end(0) == 1 - assert list.find_nth_from_end(1) == 2 - assert list.find_nth_from_end(2) == 3 - assert list.find_nth_from_end(3) == 4 - assert list.find_nth_from_end(4) == None + assert list.find_nth_from_end(0) == 1 + assert list.find_nth_from_end(1) == 2 + assert list.find_nth_from_end(2) == 3 + assert list.find_nth_from_end(3) == 4 + assert list.find_nth_from_end(4) == None + @pytest.mark.skip(reason="Going Further methods") def test_has_cycle(list): From 93b88a905d93651dd86e48545c51f247aa37fe21 Mon Sep 17 00:00:00 2001 From: Zandra Nguyen Date: Mon, 4 Apr 2022 20:38:09 -0700 Subject: [PATCH 2/2] clean up reverse method --- linked_list/linked_list.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 77b9878e..641ebd1d 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -202,10 +202,10 @@ def reverse(self): temp = p2.next p2.next = p1 # p2 now points at the previous node p1 = p2 # advance p1 to the next node (in the old ordering) - if not temp: # if we're about to walk off the list, this is the new head - self.head = p2 p2 = temp + self.head = p1 + ## Advanced/ Exercises # returns the value at the middle element in the singly linked list # Time Complexity: ?