From 53681f199c46bebeca3c019d91af79c392f570b3 Mon Sep 17 00:00:00 2001
From: Ant Fitch
Date: Tue, 30 Jan 2018 18:50:07 -0800
Subject: [PATCH] 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.
---
searches/binary_search.py | 3 +++
1 file changed, 3 insertions(+)
diff --git a/searches/binary_search.py b/searches/binary_search.py
index 93bf189cc08f..7df45883c09a 100644
--- a/searches/binary_search.py
+++ b/searches/binary_search.py
@@ -110,6 +110,9 @@ def binary_search_by_recursion(sorted_collection, item, left, right):
>>> binary_search_std_lib([0, 5, 7, 10, 15], 6)
"""
+ if (right < left):
+ return None
+
midpoint = left + (right - left) // 2
if sorted_collection[midpoint] == item: