We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
In file CS61A/lab02/lab02_extra.py / , code
CS61A/lab02/lab02_extra.py /
def cycle(f1, f2, f3): """Returns a function that is itself a higher-order function. >>> def add1(x): ... return x + 1 >>> def times2(x): ... return x * 2 >>> def add3(x): ... return x + 3 >>> my_cycle = cycle(add1, times2, add3) >>> identity = my_cycle(0) >>> identity(5) 5 >>> add_one_then_double = my_cycle(2) >>> add_one_then_double(1) 4 >>> do_all_functions = my_cycle(3) >>> do_all_functions(2) 9 >>> do_more_than_a_cycle = my_cycle(4) >>> do_more_than_a_cycle(2) 10 >>> do_two_cycles = my_cycle(6) >>> do_two_cycles(1) 19 """ "*** YOUR CODE HERE ***" def func(n): def fun(x): te = x m = 1 while m <= n: if m % 3 == 1: te = f1(te) elif m % 3 == 2: te = f2(te) else: te = f3(te) m += 1 return te return fun return func
This will lead to a error in python3 (at least in python 3.8 it does so), nonlocal n should be added to first line of fun.
nonlocal n
fun
Reference: Python nonlocal statement
The text was updated successfully, but these errors were encountered:
No branches or pull requests
In file
CS61A/lab02/lab02_extra.py /
, codeThis will lead to a error in python3 (at least in python 3.8 it does so),
nonlocal n
should be added to first line offun
.Reference: Python nonlocal statement
The text was updated successfully, but these errors were encountered: