Write a function Tribonacci (signature, n), if we want to start with [1,1,1] as input, we can get the generalized Fibonacci sequence: [1, 1, 1, 3, 5, 9, 17, 31,…] The
input parameter signature is a list containing the first three digits, n represents the length of the returned sequence (non-negative), you need to return the complete generalized Fibonacci sequence
method 1:
def tribonacci(signature, n):
[signature.append(sum(signature[-3:])) for i in range(3, n)]
return signature[:n]
print(tribonacci([1,1,1],10))
print(tribonacci([300,200,100],0))
print(tribonacci([0.5,0.5,0.5],30))
Method 2:
def tribonacci(s, n):
for i in range(3, n):
s.append(s[i-1] + s[i-2] + s[i-3])
return s[:n]
print(tribonacci([1,1,1],10))
print(tribonacci([300,200,100],0))
print(tribonacci([0.5,0.5,0.5],30))