From d7712de936f0e233697c06a9285381aec38b1631 Mon Sep 17 00:00:00 2001 From: Laisha Wadhwa Date: Fri, 11 Oct 2019 10:40:21 +0530 Subject: [PATCH 1/3] added fibonacci_search.py --- searches/fibonacci_search.py | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 searches/fibonacci_search.py diff --git a/searches/fibonacci_search.py b/searches/fibonacci_search.py new file mode 100644 index 000000000000..c3ee3d96ea7a --- /dev/null +++ b/searches/fibonacci_search.py @@ -0,0 +1,37 @@ +''' +@params +arr: input array +val: the value to be searched +output: the index of element in the array or -1 if not found +return 0 if input array is empty +''' +def fibonacci_search(arr, val): + fib_N_2 = 0 + fib_N_1 = 1 + fibNext = fib_N_1 + fib_N_2 + length = len(arr) + if length == 0: + return 0 + while (fibNext < len(arr)): + fib_N_2 = fib_N_1 + fib_N_1 = fibNext + fibNext = fib_N_1 + fib_N_2 + index = -1; + while (fibNext > 1): + i = min(index + fib_N_2, (length-1)) + if (arr[i] < val): + fibNext = fib_N_1 + fib_N_1 = fib_N_2 + fib_N_2 = fibNext - fib_N_1 + index = i + elif (arr[i] > val): + fibNext = fib_N_2 + fib_N_1 = fib_N_1 - fib_N_2 + fib_N_2 = fibNext - fib_N_1 + else : + return i + if (fib_N_1 and index < length-1) and (arr[index+1] == val): + return index+1; + return -1 +#Testing the code +print(FibonacciSearch([1,6,7,0,0,0], 6)) From 7192b9827f48c3a624060034ca52a4c64462821f Mon Sep 17 00:00:00 2001 From: Laisha Wadhwa Date: Fri, 11 Oct 2019 10:47:40 +0530 Subject: [PATCH 2/3] added Fibonacci_search.py after error handling --- searches/fibonacci_search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searches/fibonacci_search.py b/searches/fibonacci_search.py index c3ee3d96ea7a..a3b6d6df4d22 100644 --- a/searches/fibonacci_search.py +++ b/searches/fibonacci_search.py @@ -34,4 +34,4 @@ def fibonacci_search(arr, val): return index+1; return -1 #Testing the code -print(FibonacciSearch([1,6,7,0,0,0], 6)) +print(fibonacci_search([1,6,7,0,0,0], 6)) From b3a6d315bf0e86cd120c4bb2007c3ec2096f40fb Mon Sep 17 00:00:00 2001 From: Laisha Wadhwa Date: Fri, 11 Oct 2019 22:00:21 +0530 Subject: [PATCH 3/3] added doctests --- searches/fibonacci_search.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/searches/fibonacci_search.py b/searches/fibonacci_search.py index a3b6d6df4d22..f76528b9c283 100644 --- a/searches/fibonacci_search.py +++ b/searches/fibonacci_search.py @@ -1,3 +1,5 @@ +#run using python fibonacci_search.py -v + ''' @params arr: input array @@ -6,6 +8,15 @@ return 0 if input array is empty ''' def fibonacci_search(arr, val): + + """ + >>> fibonacci_search([1,6,7,0,0,0], 6) + 1 + >>> fibonacci_search([1,-1, 5, 2, 9], 10) + -1 + >>> fibonacci_search([], 9) + 0 + """ fib_N_2 = 0 fib_N_1 = 1 fibNext = fib_N_1 + fib_N_2 @@ -33,5 +44,7 @@ def fibonacci_search(arr, val): if (fib_N_1 and index < length-1) and (arr[index+1] == val): return index+1; return -1 -#Testing the code -print(fibonacci_search([1,6,7,0,0,0], 6)) + +if __name__ == "__main__": + import doctest + doctest.testmod()