From b0eb369a2b8bbcd4d5be0e70384aa3e144913fdd Mon Sep 17 00:00:00 2001 From: sharadbhat Date: Wed, 23 Aug 2017 00:30:29 +0530 Subject: [PATCH 1/6] Added interpolation_search --- pygorithm/searching/interpolation_search.py | 65 +++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 pygorithm/searching/interpolation_search.py diff --git a/pygorithm/searching/interpolation_search.py b/pygorithm/searching/interpolation_search.py new file mode 100644 index 0000000..49e8896 --- /dev/null +++ b/pygorithm/searching/interpolation_search.py @@ -0,0 +1,65 @@ +""" +Author: SHARAD BHAT +Created On: 22nd August 2017 + + - Best O() + - Average O(log(logn)) + - Worst O(n) +""" + +import inspect + +def search(_list, target): + """ + This function performs an interpolation search + on a sorted list and returns the index + of item if successful else returns False + + :param _list: list to search + :param target: item to search for + :return: index of item if successful else returns False + """ + + if type(_list) is not list: + raise TypeError("binary search only excepts lists, not {}".format(str(type(_list)))) + + # First element + low = 0 + # Last element + high = len(_list) - 1 + + # List is assumed to be sorted + while low <= high and target >= _list[low] and target <= _list[high]: + position = low + int(((float(high - low) / (_list[high] - _list[low])) * (target - _list[low]))) + + if _list[position] == target: + return position + + # If target is greater, search in right half + if _list[position] < target: + low = position + 1 + + # If target is smaller, search in left half + else: + high = position - 1 + + return False + + + +def time_complexities(): + """ + Return information on functions + time complexity + :return: string + """ + return "Best Case: O(1), Average Case: O(log(logn)), Worst Case: O(logn)" + + +def get_code(): + """ + easily retrieve the source code + of the function + :return: source code + """ + return inspect.getsource(search) From 0fdbe1c794cc717638662fb54ceccddbc5e5c8d0 Mon Sep 17 00:00:00 2001 From: Sharad Bhat Date: Wed, 23 Aug 2017 00:40:22 +0530 Subject: [PATCH 2/6] Update interpolation_search.py --- pygorithm/searching/interpolation_search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygorithm/searching/interpolation_search.py b/pygorithm/searching/interpolation_search.py index 49e8896..66f047b 100644 --- a/pygorithm/searching/interpolation_search.py +++ b/pygorithm/searching/interpolation_search.py @@ -2,7 +2,7 @@ Author: SHARAD BHAT Created On: 22nd August 2017 - - Best O() + - Best O(1) - Average O(log(logn)) - Worst O(n) """ From 6c9fecc27d9f1b2ab195f8a2ec6d811b52ee3c42 Mon Sep 17 00:00:00 2001 From: Sharad Bhat Date: Wed, 23 Aug 2017 01:48:02 +0530 Subject: [PATCH 3/6] Update interpolation_search.py --- pygorithm/searching/interpolation_search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygorithm/searching/interpolation_search.py b/pygorithm/searching/interpolation_search.py index 66f047b..cce030e 100644 --- a/pygorithm/searching/interpolation_search.py +++ b/pygorithm/searching/interpolation_search.py @@ -21,7 +21,7 @@ def search(_list, target): """ if type(_list) is not list: - raise TypeError("binary search only excepts lists, not {}".format(str(type(_list)))) + raise TypeError("interpolation search only excepts lists, not {}".format(str(type(_list)))) # First element low = 0 From f77f4cc833dac4adcd2b6fb43366976d68e830f2 Mon Sep 17 00:00:00 2001 From: Sharad Bhat Date: Wed, 23 Aug 2017 01:51:48 +0530 Subject: [PATCH 4/6] Update interpolation_search.py --- pygorithm/searching/interpolation_search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygorithm/searching/interpolation_search.py b/pygorithm/searching/interpolation_search.py index cce030e..352c41d 100644 --- a/pygorithm/searching/interpolation_search.py +++ b/pygorithm/searching/interpolation_search.py @@ -21,7 +21,7 @@ def search(_list, target): """ if type(_list) is not list: - raise TypeError("interpolation search only excepts lists, not {}".format(str(type(_list)))) + raise TypeError("interpolation search only expects lists, not {}".format(str(type(_list)))) # First element low = 0 From cf0fa503782538c34614986d45fe1c14bae2d9c7 Mon Sep 17 00:00:00 2001 From: Sharad Bhat Date: Wed, 23 Aug 2017 01:53:00 +0530 Subject: [PATCH 5/6] Update interpolation_search.py --- pygorithm/searching/interpolation_search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygorithm/searching/interpolation_search.py b/pygorithm/searching/interpolation_search.py index 352c41d..beb8f9f 100644 --- a/pygorithm/searching/interpolation_search.py +++ b/pygorithm/searching/interpolation_search.py @@ -21,7 +21,7 @@ def search(_list, target): """ if type(_list) is not list: - raise TypeError("interpolation search only expects lists, not {}".format(str(type(_list)))) + raise TypeError("interpolation search only accpets lists, not {}".format(str(type(_list)))) # First element low = 0 From 0e240fdef8135e112ebc5ab9a5c560fde760a9bf Mon Sep 17 00:00:00 2001 From: Sharad Bhat Date: Wed, 23 Aug 2017 01:53:22 +0530 Subject: [PATCH 6/6] Update interpolation_search.py --- pygorithm/searching/interpolation_search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygorithm/searching/interpolation_search.py b/pygorithm/searching/interpolation_search.py index beb8f9f..f11e35a 100644 --- a/pygorithm/searching/interpolation_search.py +++ b/pygorithm/searching/interpolation_search.py @@ -21,7 +21,7 @@ def search(_list, target): """ if type(_list) is not list: - raise TypeError("interpolation search only accpets lists, not {}".format(str(type(_list)))) + raise TypeError("interpolation search only accepts lists, not {}".format(str(type(_list)))) # First element low = 0