7.17
进度
Week 1 & 2 课件看完了,Lab 完成,剩余 Project Project 1: The Game of Hog | CS 61A Fall 2020。
Lab 2
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(cnt):
def inner_func(base, cnt):
result = base
while cnt >= 3:
result = f1(result)
result = f2(result)
result = f3(result)
cnt -= 3
if cnt == 1:
result = f1(result)
elif cnt == 2:
result = f1(result)
result = f2(result)
return result
return lambda x: inner_func(x, cnt)
return funcLab 1
>>> True and 13
______
>>> False or 0
______
>>> not 10
______
>>> not None
______
>>> True and 1 / 0 and False
______
>>> True or 1 / 0 or False
______
>>> True and 0
______
>>> False or 1
______
>>> 1 and 3 and 6 and 10 and 15
______
>>> -1 and 1 > 0
______
>>> 0 or False or 2 or 1 / 0
______
>>> not 0
______
>>> (1 + 1) and 1
______
>>> 1/0 or True
______
>>> (True or False) and False
_____7.18
PDF-7 Generating Environment Diagram 没有看懂是在做什么,需要听下视频。
剩余 Project-2 Optional Contest: Hog Strategy | CS 61A Fall 2020.
该查看 Part 5 了。
Homework -2
The recursive factorial function can be written as a single expression by using a conditional expression.
>>> fact = lambda n: 1 if n == 1 else mul(n, fact(sub(n, 1)))
>>> fact(5)
120However, this implementation relies on the fact (no pun intended) that fact has a name, to which we refer in the body of fact. To write a recursive function, we have always given it a name using a def or assignment statement so that we can refer to the function within its own body. In this question, your job is to define fact recursively without giving it a name!
Write an expression that computes n factorial using only call expressions, conditional expressions, and lambda expressions (no assignment or def statements). Note in particular that you are not allowed to use make_anonymous_factorial in your return expression. The sub and mul functions from the operator module are the only built-in functions required to solve this problem:
from operator import sub, mul
def make_anonymous_factorial():
"""Return the value of an expression that computes factorial.
>>> make_anonymous_factorial()(5)
120
>>> from construct_check import check
>>> # ban any assignments or recursion
>>> check(HW_SOURCE_FILE, 'make_anonymous_factorial', ['Assign', 'AugAssign', 'FunctionDef', 'Recursion'])
True
"""
return lambda x: (lambda f, x: f(f, x))(lambda f, x: 1 if x == 1 else mul(x, f(f, sub(x, 1))), x)
Lab 4
def max_subseq(n, t):
"""
Return the maximum subsequence of length at most t that can be found in the given number n.
For example, for n = 20125 and t = 3, we have that the subsequences are
2
0
1
2
5
20
21
22
25
01
02
05
12
15
25
201
202
205
212
215
225
012
015
025
125
and of these, the maxumum number is 225, so our answer is 225.
>>> max_subseq(20125, 3)
225
>>> max_subseq(20125, 5)
20125
>>> max_subseq(20125, 6) # note that 20125 == 020125
20125
>>> max_subseq(12345, 3)
345
>>> max_subseq(12345, 0) # 0 is of length 0
0
>>> max_subseq(12345, 1)
5
"""
"*** YOUR CODE HERE ***"Project - 1
def make_test_dice(*outcomes):
index = len(outcomes) - 1
def dice():
nonlocal index
index = (index + 1) % len(outcomes)
return outcomes[index]
return dice
test_dice = make_test_dice(4, 1, 2)
test_dice() # 4
test_dice() # 1
test_dice() # 2### Problem 7 (3 pt) 困难
python3 hog_gui.py
Project -2
### Problem 8 (2 pt) 该做这个了
7.21
6 - Mutable Values
Lab 5: Data Abstraction, Trees | CS 61A Fall 2020
Homework 3 | CS 61A Fall 2020