Write a Python function, named “find_fibonacci”, which accepts a list as input. The function will find and return the elements that satisfy the Fibonacci property:
Example : Input list : [2, 8, 4, 6, 1, 7, 8, 4, 7, 9, 4, 13]
Returned list : [[6,1,7], [1,7,8], [9,4,13]]
Source code
def find_fibonacci(list1):
list2=[]
for i in range(len(list1)):
fib=[]
if (list1[i-2]+list1[i-1]==list1[i]):
fib.append(list1[i-2])
fib.append(list1[i-1])
fib.append(list1[i])
list2.append(fib)
return list2
print(find_fibonacci([2, 8, 4, 6, 1, 7, 8, 4, 7, 9, 4, 13]))
Output
Comments
Leave a comment