Fibonacci sequence ( Python exercise )

Let’s continue with the simple Python exercises I’ve been messing around lately. This is a classical question at Dev job interviews, the Fibonacci sequence code. The idea behind this is to come up with code, that sums up the previous 2 member values for a member in the sequence, simply expressed like 1,2,3,5,8,13…

Below are my personal takes on this problem.

1: The nice and performing solution

```#get fibonacci
import sys

def main(arg):
seq_len = arg
seq_len_iterator = 2
var1 = 1
var2 = 2
fibonacci = ([var1, var2])

while seq_len_iterator < seq_len:

var3 = var1 + var2
fibonacci.append(var3)

i = len(fibonacci)
var1 = fibonacci[i-2]
var2 = fibonacci[i-1]

seq_len_iterator = seq_len_iterator + 1

print(f'Fibonacci sequence for {seq_len} sequence members goes like: {fibonacci}')

if __name__ == '__main__':
try:
arg = int(sys.argv)
main(arg)
except:
print(f'Invalid input, must be integer!')```

Execute with the needed sequence member count argument like for instance :

`python.exe C:/codility/fibonacci/__main__.py 10`

2: The alternative “nested-iterations” solution ( Not performing over ~30 sequence members count, durations exponentially grow, however its another example of a valid solution and can be useful if you need to warm oneself during long winter cold nights somewhere outside 🙂 )

```#get fibonacci
import sys

def main(arg):
seq_len = arg
seq_len_iterator = 2
iterator = 1
var1 = 1
var2 = 2
fibonacci = ([var1, var2])

while seq_len_iterator < seq_len:
if iterator == var1 + var2:
fibonacci.append(iterator)
var1 = var2
var2 = iterator
iterator = iterator + 1
seq_len_iterator = seq_len_iterator + 1
#print(f'Fibonacci member found in try #: {iterator}')
else:
iterator = iterator + 1

print(f'Fibonacci sequence for {seq_len} sequence members goes like: {fibonacci}')

if __name__ == '__main__':
try:
arg = int(sys.argv)
main(arg)
except:
print(f'Invalid input, must be integer!')```

Execute with the needed sequence member count argument like for instance :

`python.exe C:/codility/fibonacci/__main__.py 10`