Generator expressions
exercises a
def demo():
for i in range(4):
yield i
g=demo()
g1=(i for i in g)
g2=(i for i in g1)
print(list(g1))
print(list(g2))
Two exercises
def add(n,i):
return n+i
def test():
for i in range(4):
yield i
g=test()
for n in [1,10]:
g=(add(n,i) for i in g)
print(list(g))
recursive function
Exercises:
1, 20-year-old Xiao Ming; students behind big 2-year-old behind the students behind the back of the students is greater than 2 years old; seek first six students ages
def age(x):
if x == 0:
return 20
x -= 1
return age(x) + 2
res = age(3)
print(res)
2, Xiao Ming bought a packet of seeds, number of seeds to eat half of the day every day, eat on the 8th day left five found, with recursion he bought a total number of seeds
def sum(x):
if x == 0:
return 5
x -= 1
return sum(x) * 2
res = sum(8)
print(res)