# Lab 06
# Q1: Make Adder Increasing
def make_adder_inc(n):
def myadd(num):
nonlocal n
t = n + num
n += 1
return t
return myadd
1
2
3
4
5
6
7
2
3
4
5
6
7
# Q2: Next Fibonacci
def make_fib():
n = 0
m = 1
index = -1
def fib():
nonlocal n,m,index
index += 1
if index == 0:
return n
if index == 1:
return m
res = m + n
n = m
m = res
return res
return fib
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Q3: Scale
def scale(it, multiplier):
for item in it:
yield item * multiplier
1
2
3
2
3
# Q4: Hailstone
def hailstone(n):
yield n
if n != 1:
if n & 1:
yield from hailstone(3 * n + 1)
else:
yield from hailstone(n//2)
1
2
3
4
5
6
7
2
3
4
5
6
7