-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathgenerator.py
More file actions
49 lines (42 loc) · 798 Bytes
/
generator.py
File metadata and controls
49 lines (42 loc) · 798 Bytes
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
# -*- coding:utf-8 -*-
from collections import Iterable
from collections import Iterator
g = (x* x for x in range(100))
print g
for n in g:
print n
#Fibonacci
def fib(max):
n,a,b = 0,0,1
while n<max:
print b
a,b=b,a+b
n = n+1
return 'done'
#fib(6)
#yield
def fib(max):
n,a,b = 0,0,1
while n<max:
yield b
a,b=b,a+b
n = n+1
a = fib(6)
for x in a:
print(x)
#杨辉三角
def triangles(max):
L=[1]
n = 0
while 1:
yield L
L = [L[x] + L[x +1] for x in range(len(L)-1)]
L.insert(0,1)
L.append(1)
n = n+1
if(n > max):
break
for x in triangles(20):
print x
print isinstance(iter('abc'), Iterator)
print isinstance((x for x in range(10)), Iterator)