From 5a911f531e4dc7f4d74aeae98d1dccbc63ab359e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C4=B1zgar=20Ozan?= Date: Mon, 14 Sep 2026 22:43:01 +0300 Subject: [PATCH] Fix out-of-range recursion in interpolation_search_by_recursion --- searches/interpolation_search.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/searches/interpolation_search.py b/searches/interpolation_search.py index cb3e0011d0da..4d94351b3481 100644 --- a/searches/interpolation_search.py +++ b/searches/interpolation_search.py @@ -99,16 +99,24 @@ def interpolation_search_by_recursion( 1 >>> interpolation_search_by_recursion([0, 5, 7, 10, 15], 100) is None True + >>> interpolation_search_by_recursion([0, 5, 7, 10, 15], 16) is None + True + >>> interpolation_search_by_recursion([0, 5, 7, 10, 15], -1) is None + True + >>> interpolation_search_by_recursion([0, 3, 6, 12, 14, 15, 20], 10) is None + True + >>> interpolation_search_by_recursion([], 1) is None + True >>> interpolation_search_by_recursion([5, 5, 5, 5, 5], 3) is None True """ if right is None: right = len(sorted_collection) - 1 + if left > right: + return None # avoid divided by 0 during interpolation if sorted_collection[left] == sorted_collection[right]: - if sorted_collection[left] == item: - return left - return None + return left if sorted_collection[left] == item else None point = left + ((item - sorted_collection[left]) * (right - left)) // ( sorted_collection[right] - sorted_collection[left]