forked from thundergolfer/interview-with-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacci.py
More file actions
51 lines (35 loc) · 1.27 KB
/
fibonacci.py
File metadata and controls
51 lines (35 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""solutions to the fibonacci problem"""
def fibonacci_iterative(limit):
"""fibonacci sequence using an iterative approach."""
a, b = 0, 1
for i in xrange(limit):
a, b = b, a + b
return a
def fibonacci_recursive(limit):
"""fibonacci sequence using a recusive approach."""
if limit <= 1:
return limit
return fibonacci_recursive(limit - 1) + fibonacci_recursive(limit - 2)
def fibonacci_reduce(limit):
"""fibonacci sequence using reduce (shortest option)."""
return reduce(lambda x, y: x + [x[y] + x[y - 1]], range(1, limit), [0, 1])[-1]
def fibonacci_comprehension(limit):
"""fibonacci sequence using a list comprehension."""
sequence = [0, 1]
[sequence.append(sequence[i] + sequence[i-1]) for i in range(1, limit)]
return sequence[-1]
def fibonacci_generator():
""" fibonacci sequence using a generator."""
a, b = 0, 1
yield a
yield b
while True:
a, b = b, b + a
yield b
if __name__ == '__main__:'
# Use generator version
for i,value in zip(range(15),fibonacci_generator()):
print(value)
# Cant use this way. You will just get 0's
for _ in range(15):
print(next(fibonacci_generator())) # we just keep hitting the first "yeild a"