-
Notifications
You must be signed in to change notification settings - Fork 16
/
homework.py
50 lines (41 loc) · 1.07 KB
/
homework.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
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
import time
# 采用递归的方式实现
def Fibonacci_Recursion_tool(n):
if n == 1 or n == 2:
return 1
else:
return Fibonacci_Recursion_tool(n-1) + Fibonacci_Recursion_tool(n-2)
def Fibonacci_Recursion(n):
result_list = []
for i in range(1, n + 1): result_list.append(Fibonacci_Recursion_tool(i))
return result_list
# 循环计算每项的值
#def Fibonacci_Loop_tool(n):
def Fibonacci_Loop(n):
result_list = []
a, b = 0, 1
while n > 0:
result_list.append(b)
a, b = b, a + b
n -= 1
return result_list
# 采用迭代的方式实现
def Fibonacci_Yield_tool(n):
a, i, b = 0, 0, 1
while i < n:
yield b
a, b = b, a + b
i += 1
def Fibonacci_Yield(n):
return list(Fibonacci_Yield_tool(n))
# 性能比较
def Test_Fibonacci(n):
F_R = Fibonacci_Recursion(n)
F_L = Fibonacci_Loop(n)
F_Y = Fibonacci_Yield(n)
return F_R, F_L, F_Y
a, b, c = Test_Fibonacci(10)
sys.stdout.write(str(a)+str(b)+str(c))