Created
October 13, 2020 05:28
-
-
Save aershov24/0a88f12441950082e756e4ad7a859cb4 to your computer and use it in GitHub Desktop.
Markdium-14 Fibonacci Interview Questions (SOLVED) To Brush Before Coding Interview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def FibonacciSearch(lys, val): | |
fibM_minus_2 = 0 | |
fibM_minus_1 = 1 | |
fibM = fibM_minus_1 + fibM_minus_2 | |
while (fibM < len(lys)): | |
fibM_minus_2 = fibM_minus_1 | |
fibM_minus_1 = fibM | |
fibM = fibM_minus_1 + fibM_minus_2 | |
index = -1; | |
while (fibM > 1): | |
i = min(index + fibM_minus_2, (len(lys)-1)) | |
if (lys[i] < val): | |
fibM = fibM_minus_1 | |
fibM_minus_1 = fibM_minus_2 | |
fibM_minus_2 = fibM - fibM_minus_1 | |
index = i | |
elif (lys[i] > val): | |
fibM = fibM_minus_2 | |
fibM_minus_1 = fibM_minus_1 - fibM_minus_2 | |
fibM_minus_2 = fibM - fibM_minus_1 | |
else : | |
return i | |
if(fibM_minus_1 and index < (len(lys)-1) and lys[index+1] == val): | |
return index+1; | |
return -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment