Leonardo Fibonacci in a rare portrait of his younger days

I assume you are all familiar with the famous Fibonacci sequence, having to get each number as the sum of the previous two (and typically starting with either `[0,1]` or `[1,1]` as the first numbers).

While there are plenty of variation on it ([including](https://www.codewars.com/kata/tribonacci-sequence) [a few](https://www.codewars.com/kata/fibonacci-tribonacci-and-friends) [I wrote](https://www.codewars.com/kata/triple-shiftian-numbers/)), usually the catch is all the same: get a starting (signature) list of numbers, then proceed creating more with the given rules.

What if you were to get to get two parameters, one with the signature (starting value) and the other with the number you need to sum at each iteration to obtain the next one?

And there you have it, getting 3 parameters:

* a signature of length `length`
* a second parameter is a list/array of indexes of the last `length` elements you need to use to obtain the next item in the sequence (consider you can end up not using a few or summing the same number multiple times)' in other words, if you get a signature of length `5` and `[1,4,2]` as indexes, at each iteration you generate the next number by summing the 2nd, 5th and 3rd element (the ones with indexes `[1,4,2]`) of the last 5 numbers
* a third and final parameter is of course which sequence element you need to return (starting from zero, I don't want to bother you with adding/removing 1s or just coping with the fact that after years on CodeWars we all count as computers do):

```python
custom_fib([1,1],[0,1],2) == 2 #classical fibonacci!
custom_fib([1,1],[0,1],3) == 3 #classical fibonacci!
custom_fib([1,1],[0,1],4) == 5 #classical fibonacci!
custom_fib([3,5,2],[0,1,2],4) == 17 #similar to my Tribonacci
custom_fib([7,3,4,1],[1,1],6) == 2 #can you figure out how it worked ;)?
```

Source: codewars | Difficulty: EASY
Tags: Data structures

Complete search

Starter code: def custom_fib(signature, indexes, n):
	

Solutions:
from collections import deque

def custom_fib(signature, indexes, n):
	fib = deque(signature)
	for _ in range(n):
		fib.append(sum(map(fib.__getitem__, indexes)))
		fib.popleft()
	return fib[0]


def custom_fib(signature, indexes, n):
	for _ in range(n):
		signature = signature[1:] + [sum((signature[i] for i in indexes))]
	return signature[0]


def custom_fib(ini, index, k):
	for n in range(k - len(ini) + 1):
		ini = ini[1:] + [sum((ini[i] for i in index))]
	return ini[[-1, k][k < len(ini)]]


from collections import deque

def custom_fib(sig, ind, n):
	sig = deque(sig)
	while n >= len(sig):
		(s, n) = (sum((sig[x] for x in ind)), n - 1)
		sig.append(s)
		sig.popleft()
	return sig[n]


def custom_fib(seq, indexes, n):
	size = len(seq)
	while len(seq) <= n:
		tail = seq[-size:]
		seq.append(sum((tail[i] for i in indexes)))
	return seq[n]


from collections import deque

def custom_fib(signature, indexes, n):
	s = deque(signature, maxlen=len(signature))
	for i in range(n):
		s.append(sum((s[i] for i in indexes)))
	return s[0]


def custom_fib(signature, indexes, n):
	(values, l) = (list(signature), len(signature))
	while len(values) <= n:
		values.append(sum((values[-l:][i] for i in indexes)))
	return values[n]


custom_fib = f = lambda s, x, n: f(s[1:] + [sum((s[i] for i in x))], x, n - 1) if n else s[n]


custom_fib = lambda s, x, n, l=None: s[n] if len(s) > n else custom_fib((lambda p: s + [sum((p[i] for i in x))])(s[-(l or len(s)):]), x, n, l or len(s))


def custom_fib(signature, indexes, n):
	if n < len(signature):
		return signature[n]
	return custom_fib(signature[1:] + [sum(map(signature.__getitem__, indexes))], indexes, n - 1)