Skip to content

Commit 53681f1

Browse files
authored
Fixed error in binary_search_by_recursion
In binary_search_by_recursion, if you search array for a value that does not exist, you will get this error: RecursionError: maximum recursion depth exceeded in comparison To fix this, first check to make sure that the value is between left and right points like this: if (right < left): return None A similar construct has already been applied to binary_search, but did not exist in the recursive alternative.
1 parent c423419 commit 53681f1

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

searches/binary_search.py

+3
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ def binary_search_by_recursion(sorted_collection, item, left, right):
110110
>>> binary_search_std_lib([0, 5, 7, 10, 15], 6)
111111
112112
"""
113+
if (right < left):
114+
return None
115+
113116
midpoint = left + (right - left) // 2
114117

115118
if sorted_collection[midpoint] == item:

0 commit comments

Comments
 (0)