Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added IMG_20201207_011706.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 29 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
# data-structures-and-algorithms-python


# Challenge Summary
Write a function called BinarySearch which takes in 2 parameters: a sorted array and the search key. Without utilizing any of the built-in methods available to your language, return the index of the array’s element that is equal to the search key, or -1 if the element does not exist.
<!-- Short summary or background information -->

.append(value) which adds a new node with the given value to the end of the list
## Challenge Description
wRITE A FUNCTION WHICH
TAKES TWO PARAMS SORTED IN ARRAY AND SEARCH KEY AND RETURN INDEX OF ARR ELEMENT WHICH = TO SEARCH KEY OR "-1" IF IT'S NOT EXIST
INPUT => [2,4,6,8],6
OUTPUT => 2
<!-- Description of the challenge -->
.append(value) which adds a new node with the given value to the end of the list
.insertBefore(value, newVal) which add a new node with the given newValue immediately before the first value node
.insertAfter(value, newVal) which add a new node with the given newValue immediately after the first value node

## Solution![Getting Started](ALGO3.png)
## Approach & Efficiency
<!-- What approach did you take? Why? What is the Big O space/time for this approach? -->
1. Can successfully instantiate an empty linked list.
* list = LinkedList() -> None
2. Can properly insert into the linked list.
* list.insert(2) -> { 2 } -> NULL
3. The head property will properly point to the first node in the linked list.
* print (list.head.value) -> 2
4. Can properly insert multiple nodes into the linked list.
* list.insert(4)
* list.insert(5)
* list.insert(6)
5. Will return true when finding a value within the linked list that exists.
* list.includes(5) -> True
6. Will return false when searching for a value in the linked list that does not exist.
* list.includes(20) -> False
7. Can properly return a collection of all the values that exist in the linked list.
* print(list) -> "{ 6 } -> { 5 } -> { 4 } -> { 2 } -> NULL"


https://github.com/MsDiala/data-structures-and-algorithms-python/pull/3
## Solution
<!-- Embedded whiteboard image -->
![Getting Started](IMG_20201207_011706.jpg)
https://github.com/MsDiala/data-structures-and-algorithms-python/pull/6
Original file line number Diff line number Diff line change
@@ -1,7 +1,103 @@
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



# put your LinkedList implementation here
pass

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)
77 changes: 71 additions & 6 deletions tests/data_structures/test_linked_list.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,73 @@
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

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.insert_before(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.insert_after(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_instance():
ll = LinkedList()
assert isinstance(ll, LinkedList)