def test_includes_two_three_fail(): tester = LinkedList() tester.insert(2) tester.insert(3) actual = tester.includes(8) actual = tester.includes(7) assert actual == False
def zipList(l1, l2): list1 = l1.head list2 = l2.head merged_list = LinkedList() if list1 == None: return list2 if list2 == None: return list1 while list1 is not None and list2 is not None: if list1 is not None and merged_list.head == None: merged_list.insert(list1.data) else: merged_list.append_node(list1.data) if list2 is not None and merged_list.head == None: merged_list.insert(list2.data) else: merged_list.append_node(list2.data) if list1 and list1.next: list1 = list1.next else: list1 = None if list2 and list2.next: list2 = list2.next else: list2 = None return merged_list.head
def eeney_meeney_miney_moe(names: list, k: int) -> str: """Play the Eeney Meeney Miney Moe game with a given list of names and an integer k that defines what person should be removed from the list. The game continues before only 1 person is left Args: names (list): Names of the people playing the game k (int): On what count the person should leave the game Returns: str: Name of the winnig person """ # Create a LL with indexes matching the given names length ll = LinkedList() for i in range(len(names)): ll.append(i) ll = circle_ll(ll) # Get the last left index curr = prev = ll.head count = 0 while curr.next != curr: count += 1 if not count % k: prev.next = curr.next prev, curr = curr, curr.next idx = curr.val return names[idx]
def test_linked_list_includes_true(): value = 10 node = Node(10) ll = LinkedList(node) actual = ll.includes(value) expected = True assert actual == expected
def test_includes_two_three_pass(): tester = LinkedList() tester.insert(2) tester.insert(3) actual = tester.includes(2) actual = tester.includes(3) assert actual == True
def test_linked_list_insert(): value = 10 ll = LinkedList() ll.insert(value) actual = ll.head.value expected = value assert actual == expected
def test_cast_linked_list_to_list(self): linked_list = LinkedList() third = Node(3) second = Node(2, third) first = Node(1, second) linked_list.head = first assert linked_list.to_list() == [1, 2, 3]
def test_linked_list_includes_false(): value = 10 other_value = 11 node = Node(10) ll = LinkedList(node) actual = ll.includes(other_value) expected = False assert actual == expected
def test_linked_list_insert_multiple(): value1 = 'Monday' value2 = 'Tuesday' value3 = 'Wednesday' ll = LinkedList(Node(value1)) ll.insert(value2) ll.insert(value3) actual = ll.head.value expected = value3 assert actual == expected
class TestLinkedList: ll1 = LinkedList() ll2 = LinkedList(1) def test_ll_1_pass(self): assert self.ll1.head == None def test_ll_2_pass(self): assert isinstance(self.ll2.head, Node) def test_ll_3_pass(self): assert self.ll2.head.val == 1 def test_ll_4_pass(self): assert self.ll2.head.next == None def test_ll_insert_1_pass(self): self.ll1.insert(5) assert self.ll1.head.val == 5 def test_ll_insert_2_pass(self): assert self.ll1.head.next == None def test_ll_insert_3_pass(self): self.ll1.insert(4) assert self.ll1.head.val == 4 def test_ll_insert_4_pass(self): assert self.ll1.head.next.val == 5 def test_ll_insert_5_pass(self): self.ll1.insert(3) assert self.ll1.head.val == 3 def test_ll_insert_6_pass(self): assert self.ll1.head.next.val == 4 def test_ll_includes_1_pass(self): assert self.ll1.includes(5) == True def test_ll_includes_2_pass(self): assert self.ll1.includes(4) == True def test_ll_includes_3_pass(self): assert self.ll1.includes(3) == True def test_ll_includes_4_pass(self): assert self.ll1.includes(1) == False def test_ll_str_1_pass(self): self.ll2.insert(2) self.ll2.insert(3) self.ll2.insert(4) self.ll2.insert(5) assert str(self.ll2) == '{5} -> {4} -> {3} -> {2} -> {1} -> None'
def test_append(): lst = LinkedList() lst.insert("a") lst.insert("c") lst.insert("f") lst.append("b") assert lst.includes("b") == True
def test_head_to_the_first_el(): linked_list = LinkedList() linked_list.insert('a') assert linked_list.head.value == 'a' linked_list.insert('b') assert linked_list.head.value == 'b' linked_list.insert('c') assert linked_list.head.value == 'c' linked_list.insert('d') assert linked_list.head.value == 'd' assert linked_list.head.next.value == 'c'
def test_k_in_middle(): ll = LinkedList() ll.append_node("a") ll.append_node(3) ll.append_node("3") actual = ll.find_kth_value_from_end(1) expected = 3 assert actual == expected
def test_insert_before_first(): lst = LinkedList() lst.insert("{c}") lst.insert("{b}") lst.insert("{a}") lst.insert_before("{a}", "{d}") actual = lst.__str__() expected = "{d} -> {a} -> {b} -> {c} -> None" assert actual == expected
def test_insert_after_last(): lst = LinkedList() lst.insert("{c}") lst.insert("{b}") lst.insert("{a}") lst.insert_after("{c}", "{d}") actual = lst.__str__() expected = "{a} -> {b} -> {c} -> {d} -> None" assert actual == expected
def test_append(): lst = LinkedList() lst.insert("a") lst.insert("c") lst.insert("f") lst.append("b") actual = lst.__str__() expected = "{f} -> {c} -> {a} -> {b} -> None" assert actual == expected
def test_append(): ll = LinkedList() ll.insert(3) ll.insert(2) ll.insert(1) ll.append_node(4) actual = ll.return_all() expected = [1, 2, 3, 4] assert actual == expected
def test_list2_smaller(): l1 = LinkedList() l2 = LinkedList() l1.append_node("a") l1.append_node("b") l1.append_node("c") l2.append_node(1) l2.append_node(2) actual = zipList(l1, l2) expected = ["a", 1, "b", 2, "c"] while actual: assert actual.data == expected.pop(0) actual = actual.next
def test_is_k_longer_than_length(): ll = LinkedList() ll.append_node(1) ll.append_node(2) ll.append_node(3) with pytest.raises(IndexError): ll.find_kth_value_from_end(4)
def test_linked_list_kth_greater_than(): ll = LinkedList() ll.append(1) ll.append(3) ll.append(8) ll.append(2) with pytest.raises(Exception): ll.kthFromEnd(6)
def test_can_insert_after_last(): tester = LinkedList() tester.insert(4) tester.insert(3) tester.insert(2) tester.insert(1) tester.insert_after(4, 5) assert tester
def test_can_insert_after_middle(): tester = LinkedList() tester.insert(4) tester.insert(3) tester.insert(2) tester.insert(1) tester.insert_after(2, "Middle") assert tester
def test_can_insert_before_first(): tester = LinkedList() tester.insert(4) tester.insert(3) tester.insert(2) tester.insert(1) tester.insert_before(1, "Before first") assert tester
def test_can_insert_before_middle(): tester = LinkedList() tester.insert(4) tester.insert(3) tester.insert(2) tester.insert(1) tester.insert_before(3, "Middle") assert tester
def test_linked_list_kth_equal_length(): ll = LinkedList() ll.append(1) ll.append(3) ll.append(8) ll.append(2) with pytest.raises(Exception): ll.kthFromEnd(4)
def test_linked_list_kth_negative_int(): ll = LinkedList() ll.append(1) ll.append(3) ll.append(8) ll.append(2) with pytest.raises(Exception): ll.kthFromEnd(-4)
def test_negative_input(): ll = LinkedList() ll.append_node(1) ll.append_node(2) ll.append_node(3) # test will pass if code in block causes expected ERROR with pytest.raises(ValueError): ll.find_kth_value_from_end(-1)
def add(self, key, val): # gets a random "index/bucket" number hashed = self._hash(key) # If there is already something at that index value then make a linked list at that index if not self.indicies[hashed]: self.indicies[hashed] = LinkedList() # append the new node with its key and value self.indicies[hashed].append_node([key, val])
def test_get_nodes_by_index(self): linked_list = LinkedList.from_list([1, 2, 3]) assert linked_list[0] == linked_list.head assert linked_list[1] == linked_list.head.next assert linked_list[2] == linked_list.head.next.next with raises(IndexError): _ = linked_list[3]
def test_check_if_element_is_in_linked_list(self): linked_list = LinkedList.from_list([1, 2, 3]) assert 1 in linked_list assert 2 in linked_list assert 3 in linked_list assert 4 not in linked_list assert 0 not in linked_list