# Demo entry 2721334

test code

Submitted by anonymous on Sep 22, 2015 at 00:38
Language: Python. Code size: 1.3 kB.

```# -*- coding: utf-8 -*-

def fibonacci_iter(n):
"""Calculate Fibonacci number (iterative version).

:param n: positive whole number.
"""
if n < 0:
raise ValueError("N argument should be a positive whole number.")

previous, next = 0, 1
for _ in xrange(n):
previous, next = next, previous + next
return previous

def fibonacci_recursive(n):
"""Calculate Fibonacci number (recursive version).

:param n: positive whole number.
"""

if n < 0:
raise ValueError("N argument should be a positive whole number.")
if n == 0:
return 0
if n == 1:
return 1
else:
return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2)

# few basic tests, which make sure that we make all correctly
assert fibonacci_iter(0) == fibonacci_recursive(0) == 0, "Should be 0"
assert fibonacci_iter(1) == fibonacci_recursive(1) == 1, "Should be 1"
assert fibonacci_iter(2) == fibonacci_recursive(2) == 1, "Should be 1"
assert fibonacci_iter(3) == fibonacci_recursive(3) == 2, "Should be 2"
assert fibonacci_iter(4) == fibonacci_recursive(4) == 3, "Should be 3"
assert fibonacci_iter(5) == fibonacci_recursive(5) == 5, "Should be 5"
assert fibonacci_iter(6) == fibonacci_recursive(6) == 8, "Should be 8"
```

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.