-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfib.py
135 lines (95 loc) · 3.03 KB
/
fib.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/python
'''
Fibonacci solutions
Multiple solutions to generating a fibonacci sequence.
Investigation following brain fart during interview.
No use of google/docs/stack.
TODO: Tests should return sets rather than print so they can be profiled.
ADVICE: just use fib_get() if importing externally.
'''
def fib_gen():
''' Generator solution to fib problem. '''
# Seed initial values of first,second with 1,1
first = 1
second = 1
# yield initial 1,1 of sequence
yield first
yield second
# loop ad infinitum generating next fib value and updating first,second
while True:
result = first + second
first, second = second, result
yield result
def fib_gen_solution(num):
''' Trial generator solution. '''
fg = fib_gen()
for i in range(1, num):
print next(fg)
def fib_rec(it_target, first=1, second=1, it_cnt=2):
''' Recursive solution to fib problem. '''
# it_cnt is seeded to 2 to account for the two seeded 1 values.
if it_target in range(1, 3):
return 1
tmp = first + second
it_cnt = it_cnt + 1
if it_cnt == it_target:
return tmp
else:
first, second = second, tmp
return fib_rec(it_target, first, second, it_cnt)
def fib_rec_solution(num):
''' Trial recursive solution. '''
for n in range(1, num):
print fib_rec(n)
def fib_imp(iterations):
''' Imperativly get fib sequence. '''
sequence = [1,1]
# edge cases
if iterations is 1:
return [1]
if iterations is 2:
return sequence
for i in range(2, iterations-1):
nxt = sequence[i-1] + sequence[i-2]
sequence.append(nxt)
return sequence
def fib_imp_solution(num):
''' Trial imperative solution. '''
for x in fib_imp(num):
print x
class FibIterator:
''' Class based iterator solution to fibonnacci issue. '''
def __init__(self):
''' Seed sequence with 1,1 and set count to 0. '''
self._n_minus_1 = 1
self._n_minus_2 = 1
self._cnt = 0
def __iter__(self):
''' Return iterator object. '''
return self
def next(self):
''' update n-1 and n-2 values as well as count of iterations. '''
# Not too elegant
self._cnt = self._cnt + 1
if self._cnt in [1,2]:
return 1
# Calculate next value, update previous 2 and return
nxt = self._n_minus_2 + self._n_minus_1
self._n_minus_2, self._n_minus_1 = self._n_minus_1, nxt
return nxt
def fib_class_solution(num):
''' Trial class based iterator solution. '''
fi = FibIterator()
for x in range(0, num):
print next(fi)
if __name__ == '__main__':
''' Entry point. Uncomment to test a solution. '''
TST_RANGE = 10
print '\r\n\tGENERATOR RESULTS.'
fib_gen_solution(TST_RANGE)
print '\r\n\tRECURSIVE RESULTS.'
fib_rec_solution(TST_RANGE)
print '\r\n\tIMPERATIVE RESULTS.'
fib_imp_solution(TST_RANGE)
print '\r\n\tITERATOR RESULTS.'
fib_class_solution(TST_RANGE)