diff --git a/ALGO3.png b/ALGO3.png new file mode 100644 index 0000000..c0fead8 Binary files /dev/null and b/ALGO3.png differ diff --git a/README.md b/README.md index 857a1f0..832fd67 100644 --- a/README.md +++ b/README.md @@ -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) \ No newline at end of file +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 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 new file mode 100644 index 0000000..5d1378a Binary files /dev/null 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 new file mode 100644 index 0000000..69aa2f4 --- /dev/null +++ b/data_structures_and_algorithms/challenges/array_reverse/array_search.py @@ -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 \ 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 new file mode 100644 index 0000000..bc729e6 Binary files /dev/null and b/tests/challenges/__pycache__/test_array_search.cpython-38-pytest-6.1.2.pyc differ diff --git a/tests/challenges/test_array_search.py b/tests/challenges/test_array_search.py new file mode 100644 index 0000000..0ec091d --- /dev/null +++ b/tests/challenges/test_array_search.py @@ -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 \ No newline at end of file