diff --git a/data_structures_and_algorithms/challenges/array_reverse/__pycache__/array_search.cpython-38.pyc b/data_structures_and_algorithms/challenges/array_reverse/__pycache__/array_search.cpython-38.pyc index 5d1378a..679e8e4 100644 Binary files a/data_structures_and_algorithms/challenges/array_reverse/__pycache__/array_search.cpython-38.pyc and b/data_structures_and_algorithms/challenges/array_reverse/__pycache__/array_search.cpython-38.pyc differ diff --git a/data_structures_and_algorithms/challenges/array_reverse/array_search.py b/data_structures_and_algorithms/challenges/array_reverse/array_search.py index 69aa2f4..e69de29 100644 --- a/data_structures_and_algorithms/challenges/array_reverse/array_search.py +++ b/data_structures_and_algorithms/challenges/array_reverse/array_search.py @@ -1,14 +0,0 @@ -def binary_search(arr, key): - low = 0 - high = len(arr) - while len(arr) > 0: - middle = (low+high) // 2 - if arr[middle] == key: - return middle - if arr[middle] < key: - low= middle - if arr[middle] > key: - high= middle - if middle == 0 or middle == len(arr) - 1: - return -1 - return -1 \ No newline at end of file diff --git a/data_structures_and_algorithms/challenges/array_reverse/array_shift.py b/data_structures_and_algorithms/challenges/array_reverse/array_shift.py index 0b06801..139597f 100644 --- a/data_structures_and_algorithms/challenges/array_reverse/array_shift.py +++ b/data_structures_and_algorithms/challenges/array_reverse/array_shift.py @@ -1,8 +1,2 @@ -def array_shift(l,b): - middle = len(l) // 2 - if len(l) % 2 == 0: - return l[:middle] + [b] + l[middle:] - else: - return l[:middle + 1] + [b] + l[middle +1:] diff --git a/data_structures_and_algorithms/data_structures/linked_list/__pycache__/linked_list.cpython-38.pyc b/data_structures_and_algorithms/data_structures/linked_list/__pycache__/linked_list.cpython-38.pyc index d4bbf7a..5f98fe4 100644 Binary files a/data_structures_and_algorithms/data_structures/linked_list/__pycache__/linked_list.cpython-38.pyc and b/data_structures_and_algorithms/data_structures/linked_list/__pycache__/linked_list.cpython-38.pyc differ 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 index d15924f..60782d9 100644 --- a/data_structures_and_algorithms/data_structures/linked_list/linked_list.py +++ b/data_structures_and_algorithms/data_structures/linked_list/linked_list.py @@ -1,7 +1,141 @@ +class Node: + def __init__(self, value): + self.value = value + self.next = None + + def __repr__(self): + return f'{self.value}' + + class LinkedList: - """ - Put docstring here - """ + 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) - # put your LinkedList implementation here - pass + + \ No newline at end of file diff --git a/tests/challenges/__pycache__/test_array_search.cpython-38-pytest-6.1.2.pyc b/tests/challenges/__pycache__/test_array_search.cpython-38-pytest-6.1.2.pyc index bc729e6..ca2a005 100644 Binary files a/tests/challenges/__pycache__/test_array_search.cpython-38-pytest-6.1.2.pyc and b/tests/challenges/__pycache__/test_array_search.cpython-38-pytest-6.1.2.pyc differ diff --git a/tests/challenges/__pycache__/test_array_shift.cpython-38-pytest-6.1.2.pyc b/tests/challenges/__pycache__/test_array_shift.cpython-38-pytest-6.1.2.pyc index 7a70476..01fa727 100644 Binary files a/tests/challenges/__pycache__/test_array_shift.cpython-38-pytest-6.1.2.pyc and b/tests/challenges/__pycache__/test_array_shift.cpython-38-pytest-6.1.2.pyc differ diff --git a/tests/challenges/test_array_search.py b/tests/challenges/test_array_search.py index 0ec091d..e69de29 100644 --- a/tests/challenges/test_array_search.py +++ b/tests/challenges/test_array_search.py @@ -1,66 +0,0 @@ -from data_structures_and_algorithms.challenges.array_reverse.array_search import binary_search - -# "Happy Path" cases -def test_simple(): - expected = 3 - actual = binary_search([1,2,3,4,5,6],4) - assert actual == expected - -def test_original_nums(): - expected = 2 - actual = binary_search([4,8,15,16,23,42], 15) - assert actual == expected - -def test_absence(): - expected = -1 - actual = binary_search([11,22,33,44,55,66,77], 90) - assert actual == expected - -# Expected Failure -def test_num_in_left(): - expected = 0 - actual = binary_search([11,22,33,44,55,66,77,89,100], 11) - assert actual == expected - -def test_num_in_right(): - expected = 8 - actual = binary_search([11,22,33,44,55,66,77,89,100], 100) - assert actual == expected - -def test_num_in_middle(): - expected = 4 - actual = binary_search([11,22,33,44,55,66,77,89,100], 55) - assert actual == expected - -def test_num_not_in_right(): - expected = -1 - actual = binary_search([11,22,33,44,55,66,77,89,100], 101) - assert actual == expected - -def test_num_not_in_left(): - expected = -1 - actual = binary_search([11,22,33,44,55,66,77,89,100], 10) - assert actual == expected - - - -# Edge Cases -def test_two_element_arr(): - expected = 1 - actual = binary_search([10, 34], 34) - assert actual == expected - -def test_two_element_arr_anbsence(): - expected = -1 - actual = binary_search([10, 34], 19) - assert actual == expected - -def test_empty_array(): - expected = -1 - actual = binary_search([], 19) - assert actual == expected - -def test_empty_array_negative(): - expected = -1 - actual = binary_search([], -1) - assert actual == expected \ No newline at end of file diff --git a/tests/data_structures/__pycache__/test_linked_list.cpython-38-pytest-6.1.2.pyc b/tests/data_structures/__pycache__/test_linked_list.cpython-38-pytest-6.1.2.pyc index e69de29..b50a061 100644 Binary files a/tests/data_structures/__pycache__/test_linked_list.cpython-38-pytest-6.1.2.pyc and b/tests/data_structures/__pycache__/test_linked_list.cpython-38-pytest-6.1.2.pyc differ diff --git a/tests/data_structures/test_linked_list.py b/tests/data_structures/test_linked_list.py index 88ec27d..7a8e85b 100644 --- a/tests/data_structures/test_linked_list.py +++ b/tests/data_structures/test_linked_list.py @@ -1,8 +1,105 @@ -from data_structures_and_algorithms.data_structures.linked_list.linked_list import ( - 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_instance(): - ll = LinkedList() - assert isinstance(ll, LinkedList) +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