-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnonlocal.py
52 lines (48 loc) · 896 Bytes
/
nonlocal.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
def make_fib():
"""Returns a function that returns the next Fibonacci number
every time it is called.
>>> fib = make_fib()
>>> fib()
0
>>> fib()
1
>>> fib()
1
>>> fib()
2
>>> fib()
3
"""
"*** YOUR CODE HERE ***"
curr, nex1 = 0, 1
def fib():
nonlocal curr, nex1
result = curr
curr, nex1 = nex1, nex1 + curr
return result
return fib
def make_test_dice(seq):
"""Makes deterministic dice.
>>> dice = make_test_dice([2, 6, 1])
>>> dice()
2
>>> dice()
6
>>> dice()
1
>>> dice()
2
>>> other = make_test_dice([1])
>>> other()
1
>>> dice()
6
"""
"*** YOUR CODE HERE ***"
position = 0
def dice():
nonlocal position
curr = position % len(seq)
position += 1
return seq[curr]
return dice