From c580eba1cc2d649c327d7b04303a33146f79eaed Mon Sep 17 00:00:00 2001 From: MsDiala Date: Tue, 8 Dec 2020 20:25:49 +0200 Subject: [PATCH 1/2] g --- data_structures_and_algorithms/data_structures/ll-zip/ll_zip.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 data_structures_and_algorithms/data_structures/ll-zip/ll_zip.py diff --git a/data_structures_and_algorithms/data_structures/ll-zip/ll_zip.py b/data_structures_and_algorithms/data_structures/ll-zip/ll_zip.py new file mode 100644 index 0000000..e69de29 From f2c1c42bb199aff9e3dca142d52a819240a18390 Mon Sep 17 00:00:00 2001 From: MsDiala Date: Tue, 8 Dec 2020 20:50:27 +0200 Subject: [PATCH 2/2] dd --- .../linked_list/__pycache__/linked_list.py | 140 ++++++++++++++++++ .../linked_list/__pycache__/ll_zip.py | 31 ++++ .../linked_list/linked_list.py | 7 - .../data_structures/ll-zip/ll_zip.py | 0 tests/data_structures/ll_ziptest.py | 72 +++++++++ tests/data_structures/test_linked_list.py | 105 +++++++++++++ 6 files changed, 348 insertions(+), 7 deletions(-) create mode 100644 data_structures_and_algorithms/data_structures/linked_list/__pycache__/linked_list.py create mode 100644 data_structures_and_algorithms/data_structures/linked_list/__pycache__/ll_zip.py delete mode 100644 data_structures_and_algorithms/data_structures/linked_list/linked_list.py delete mode 100644 data_structures_and_algorithms/data_structures/ll-zip/ll_zip.py create mode 100644 tests/data_structures/ll_ziptest.py diff --git a/data_structures_and_algorithms/data_structures/linked_list/__pycache__/linked_list.py b/data_structures_and_algorithms/data_structures/linked_list/__pycache__/linked_list.py new file mode 100644 index 0000000..f6ef545 --- /dev/null +++ b/data_structures_and_algorithms/data_structures/linked_list/__pycache__/linked_list.py @@ -0,0 +1,140 @@ +class Node: + def __init__(self, value): + self.value = value + self.next = None + + def __repr__(self): + return f'{self.value}' + + +class LinkedList: + def __init__(self): + self.head = None + self.tail = None + + self.length = 0 + + '''Adds node to head of linked list''' + + def insert(self, value): + node = Node(value) + node.next = self.head + self.head = node + self.length += 1 + + '''Takes in a target value and iterates through Linked List + until the value is found, or the end of the list is reached + returns a boolean''' + + def includes(self, value): + current = self.head + + while current: + if current.value == value: + return True + current = current.next + + return False + + def __str__(self): + values = '' + current = self.head + while current: + values += f'Node: {str(current.value)} ' + current = current.next + return values + + '''Iterates to the end of a Linked List and appends a value''' + + def append(self, value): + new_node = Node(value) + if self.head == None: + self.head = new_node + return + last = self.head + + while last.next: + last = last.next + + last.next = new_node + + + def insertBefore(self, exisiting_value, new_value): + new_node = Node(new_value) + current = self.head + + while current.next: + if current.next.value == exisiting_value: + new_node.next = current.next + current.next = new_node + return + else: + current = current.next + + + def insertAfter(self, existing_value, new_value): + new_node = Node(new_value) + current = self.head + while current: + if current.value == existing_value: + + old_node = current.next + current.next = new_node + new_node.next = old_node + + return + else: + current = current.next + + + def length(self): + current = self.head + count = 0 + while (current): + count += 1 + current = current.next + return count + + + def nthFromEnd(self,n=0): + if type(n)!=int: + return'Exception' + if n>self.length() or n<0: + return'Exception' + current= self.head + counter= 0 + while current.next(): + current= current.next + counter+=1 + if counter>n: + current= current.next + return current.value + + + + + + + + + + + + + + + +node1 = Node(1) +node2 = Node(2) +node3 = Node(3) +node4 = Node(4) +node5 = Node(5) +ll = LinkedList() +ll.insert(node1) +ll.insert(node2) +ll.insert(node3) +ll.insertBefore(node2,node4) +ll.insertAfter(node3,node5) +print(ll) + + \ No newline at end of file diff --git a/data_structures_and_algorithms/data_structures/linked_list/__pycache__/ll_zip.py b/data_structures_and_algorithms/data_structures/linked_list/__pycache__/ll_zip.py new file mode 100644 index 0000000..3458224 --- /dev/null +++ b/data_structures_and_algorithms/data_structures/linked_list/__pycache__/ll_zip.py @@ -0,0 +1,31 @@ +from data_structures_and_algorithms.data_structures.linked_list.linked_list import LinkedList ,Node + +def zip_lists( list1, list2): + """takes two linked lists and merges them starting with head1 then head2""" + + if not list1 : + return list1 + if not list2 : + return list1 + output =LinkedList() + current1 =list1.head + current2 =list2.head + while current1 : + output.append(current1.value) + if current2 : + output.append(current2.value) + current2 = current2.next + current1= current1.next + while current2 : + output.append(current2.value) + current2 =current2.next + return output + + + +if __name__ == "__main__": + + listl = LinkedList() + listl.append(5) + listl.append(10) + diff --git a/data_structures_and_algorithms/data_structures/linked_list/linked_list.py b/data_structures_and_algorithms/data_structures/linked_list/linked_list.py deleted file mode 100644 index d15924f..0000000 --- a/data_structures_and_algorithms/data_structures/linked_list/linked_list.py +++ /dev/null @@ -1,7 +0,0 @@ -class LinkedList: - """ - Put docstring here - """ - - # put your LinkedList implementation here - pass diff --git a/data_structures_and_algorithms/data_structures/ll-zip/ll_zip.py b/data_structures_and_algorithms/data_structures/ll-zip/ll_zip.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/data_structures/ll_ziptest.py b/tests/data_structures/ll_ziptest.py new file mode 100644 index 0000000..f951694 --- /dev/null +++ b/tests/data_structures/ll_ziptest.py @@ -0,0 +1,72 @@ +from data_structures_and_algorithms.data_structures.linked_list.ll_zip import zip_lists +from data_structures_and_algorithms.data_structures.linked_list.linked_list import Node ,LinkedList + + + +def test_empty_list() : + + list1 = LinkedList() + list2 = LinkedList() + list1.insert("carrot") + list1.insert("banana") + list1.insert("apple") + actual = str(zip_lists(list1, list2)) + expected = "{'carrot'} ->{'banana'} ->{'apple'} ->" + assert actual == expected + +def test_list_two_impty(): + list1 = LinkedList() + list2 = LinkedList() + list2.insert("carrot") + list2.insert("banana") + list2.insert("apple") + actual = str(zip_lists(list1, list2)) + expected = "{'carrot'} ->{'banana'} ->{'apple'} ->" + assert actual == expected + +def test_list_two(): + list1 = LinkedList() + list2 = LinkedList() + list1.insert("carrot") + list2.insert("1") + list1.insert("banana") + list2.insert("2") + + list1.insert("apple") + list2.insert("3") + actual = str(zip_lists(list1, list2)) + expected = "{'carrot'} ->{'1'} ->{'banana'} ->{'2'} ->{'apple'} ->{'3'} ->" + assert actual == expected + +def test_zipLists2(): + list1 = LinkedList() + list2 = LinkedList() + list1.append(3) + list1.append(2) + list1.append(1) + list1.append(0) + list1.append(0) + + list2.append(8) + list2.append(7) + list2.append(6) + expected = "{'3'} ->{'8'} ->{'2'} ->{'7'} ->{'1'} ->{'6'} ->{'0'} ->{'0'} ->" + actual = str(zip_lists(list1, list2)) + assert actual == expected + +def test_zipLists3(): + list1 = LinkedList() + list2 = LinkedList() + list1.append(3) + list1.append(2) + list1.append(1) + + + list2.append(8) + list2.append(7) + list2.append(6) + list2.append(5) + list2.append(5) + expected = "{'3'} ->{'8'} ->{'2'} ->{'7'} ->{'1'} ->{'6'} ->{'5'} ->{'5'} ->" + actual = str(zip_lists(list1, list2)) + assert actual == expected diff --git a/tests/data_structures/test_linked_list.py b/tests/data_structures/test_linked_list.py index 88ec27d..5ee6158 100644 --- a/tests/data_structures/test_linked_list.py +++ b/tests/data_structures/test_linked_list.py @@ -6,3 +6,108 @@ def test_instance(): ll = LinkedList() assert isinstance(ll, LinkedList) +from data_structures_and_algorithms.data_structures.linked_list.linked_list import Node, LinkedList +import pytest + +def test_list_creation(): + actual = LinkedList() + assert actual.head == None + +def test_insert_method(): + lst_one = LinkedList() + lst_one.insert(1) + lst_one.insert(2) + lst_one.insert(3) + + assert lst_one.head.value == 3 + assert lst_one.head.next.value == 2 + assert lst_one.head.next.next.value == 1 + +def test_includes_method(): + lst_two = LinkedList() + lst_two.insert('apples') + lst_two.insert('pickles') + lst_two.insert('chips') + + assert lst_two.includes('pickles') == True + assert lst_two.includes('whales') == False + + + + +def test_to_string_method(): + lst_three = LinkedList() + lst_three.insert(1) + lst_three.append(2) + lst_three.append(3) + lst_three.append(4) + lst_three.append(5) + + actual = lst_three.__str__() + expected = 'Node: 1 Node: 2 Node: 3 Node: 4 Node: 5 ' + assert actual == expected + +def test_append(): + lst_four = LinkedList() + lst_four.insert('apple') + lst_four.append('banana') + assert lst_four.head.next.value == 'banana' + +def test_insert_before(): + lst_five = LinkedList() + lst_five.insert(1) + lst_five.append(2) + lst_five.append(3) + lst_five.append(5) + lst_five.insertBefore(5, 4) + + assert lst_five.head.next.next.value == 3 + assert lst_five.head.next.next.next.value == 4 + assert lst_five.head.next.next.next.next.value == 5 + +def test_insert_after(): + lst_six = LinkedList() + lst_six.insert(1) + lst_six.append(2) + lst_six.append(3) + lst_six.append(4) + lst_six.append(6) + lst_six.insertAfter(4, 5) + + assert lst_six.head.next.next.value == 3 + assert lst_six.head.next.next.next.value == 4 + assert lst_six.head.next.next.next.next.value == 5 + assert lst_six.head.next.next.next.next.next.value == 6 + + + +def test_nth_greater_than(): + list = LinkedList() + list.append(1) + list.append(2) + list.append(3) + with pytest.raises(Exception): + assert list.nthFromEnd(8) +# initialList = LinkedList([5,6,7,8]) +# assert initialList.nthFromEnd(4) == 5 + +def test_nth_negative(): + list = LinkedList() + list.append(1) + list.append(2) + list.append(3) + with pytest.raises(Exception): + assert list.nthFromEnd(-30) + + +def test_nth_list_of_one(): + list = LinkedList() + list.append(1) + assert list.nthFromEnd(0) == 1 + +def test_nth_average_use(): + list = LinkedList() + list.append(1) + list.append(2) + list.append(3) + assert list.nthFromEnd(1) == 2 \ No newline at end of file