← Back to challenges

Instances of the Fibonacci Sequence

PythonHardalgorithmsarraysnumbersloops

Instructions

Create a function that takes a number as an argument and returns n instances of the Fibonacci sequence as a list.

Fibonacci numbers: F(n) = F(n-1) + F(n-2) with F(0) = 0 and F(1) = 1. So the easy explanation is: The next element is the sum of the two previous elements.

If you want to read more about this sequence, take a look at the On-Line Encyclopedia of Integer Sequences. Fibonacci numbers are strongly related to the golden ratio.

Examples

fib_seq(4) ➞ [0, 1, 1, 2]

fib_seq(7) ➞ [0, 1, 1, 2, 3, 5, 8]

fib_seq(0) ➞ []

Notes

  • If 0 is given, return an empty list.
  • If no argument is given, return None.
  • The input will never be a negative integer.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.