-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhomework.py
66 lines (56 loc) · 1.42 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -*- coding: utf-8 -*-
# coding:utf-8
from __future__ import print_function
import time
import sys
# 采用递归的方式实现
def Fibonacci_Recursion_tool(n):
if n == 1:
return 1
elif 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):
a = 1
b = 1
n = int(input())
if n<=2 :
print(1)
else:
print (1,1,end=' ')
for i in range(1,n-1):
a,b = b,a+b
print (b)
def Fibonacci_Loop(n):
result_list = []
a, b = 0, 1
i = 0
while i < n:
result_list.append(b)
a, b = b, a + b
i += 1
return result_list
# 采用迭代的方式实现
def Fibonacci_Yield_tool(n):
a, i, b = 0, 0, 1
while i < n:
yield b #yield就是 return 返回一个值,并且记住这个返回的位置,下次迭代就从这个位置后开始。
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))