Skip to content
Merged
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 ALGO3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# data-structures-and-algorithms-python


# Challenge Summary
IT was eady
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.
## Challenge Description
I did a tests to a shift array
## Approach & Efficiency
TDD
## Solution
![Getting Started](dddddddddddd.png)
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

## Solution![Getting Started](ALGO3.png)


https://github.com/MsDiala/data-structures-and-algorithms-python/pull/3
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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
Binary file not shown.
66 changes: 66 additions & 0 deletions tests/challenges/test_array_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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