-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhomework.py
47 lines (40 loc) · 1.04 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
# -*- coding: utf-8 -*-
from __future__ import print_function
import time
import sys
# 采用递归的方式实现
def Fibonacci_Recursion_tool(n):
if n <= 2:
return 1
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):
pass
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):
l = [1]*n
for i in range(2,n):
l[i] = l[i-1] + l[i-2]
return l
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))