MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / get

Method get

dynamic_programming/fibonacci.py:11–24  ·  view source on GitHub ↗

Get the Fibonacci number of `index`. If the number does not exist, calculate all missing numbers leading up to the number of `index`. >>> Fibonacci().get(10) [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] >>> Fibonacci().get(5) [0, 1, 1, 2, 3]

(self, index: int)

Source from the content-addressed store, hash-verified

9 self.sequence = [0, 1]
10
11 def get(self, index: int) -> list:
12 """
13 Get the Fibonacci number of `index`. If the number does not exist,
14 calculate all missing numbers leading up to the number of `index`.
15
16 >>> Fibonacci().get(10)
17 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
18 >>> Fibonacci().get(5)
19 [0, 1, 1, 2, 3]
20 """
21 if (difference := index - (len(self.sequence) - 2)) >= 1:
22 for _ in range(difference):
23 self.sequence.append(self.sequence[-1] + self.sequence[-2])
24 return self.sequence[:index]
25
26
27def main() -> None:

Callers 2

mainFunction · 0.95
is_breakableFunction · 0.45

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected