CS61A学习笔记
HW
hw01
Q5 If Function vs Statement
Let’s try to write a function that does the same thing as an if
statement.
1 |
|
Despite the doctests above, this function actually does not do the same thing as an if
statement in all cases. To prove this fact, write functions cond
, true_func
, and false_func
such that with_if_statement
prints the number 47
, but with_if_function
prints both 42
and 47
.
1 |
|
执行result = with_if_statement(
)和result = with_if_function()
结果不同,是因为在with_if_function()
函数中,return if_function(cond(), true_func(), false_func())
的执行方式是根据if_function
函数的逻辑来确定的。
1 |
|
这个函数是一个普通的Python函数,它接受三个参数:condition
、true_result
和false_result
。然后根据condition
的值来返回其中之一。函数内部的if condition:
语句用于判断condition
的值,如果condition
为真,就返回true_result
,否则返回false_result
。
在这个特定的情况下,cond()
、true_func()
和false_func()
都是函数调用,它们在if_function
函数调用之前就已经被执行了。这是因为在调用if_function(cond(), true_func(), false_func())
时,函数参数是在调用前就计算并传递给if_function
的。所以,cond()
、true_func()
和false_func()
都会在if_function
的内部判断之前执行,而不是根据条件选择执行其中之一。
LAB
lab01
Q7:Double Eights
Write a function that takes in a number and determines if the digits contain two adjacent 8s.
1 |
|
PRO
Pro7test
Hog
1 |
|
输入s0, s1 = play(always_roll(1), always_roll(1), dice=make_test_dice(5), goal=10, say=count(1))
后,输出
1 |
|
这个代码的效果是无限递归地调用
count
函数,并不断地增加参数n
的值,每次都打印出当前的n
和s0
的值。因为没有终止条件,所以这个递归将会无限循环下去,直到出现栈溢出或手动停止程序运行。
需要注意的是,
s1
参数在这个代码中没有被使用,因此对于代码的功能来说是没有影响的。如果想要更清楚地理解代码的目的和效果,需要查看完整的代码或者了解更多关于s1
参数的用法。
因此,第二行n+1=2,但是第二轮是p1掷色子,s0不会增加,故s0仍然为5,第三轮到p0掷色子,s0增加为10
pro9
1 |
|
这段代码是一个函数,其目的是找出在一定数量的试验中,使用不同数量的骰子(从1到10颗)时,哪种数量的骰子能够获得最高的平均得分。
在这段代码中,
make_averaged(roll_dice, trials_count)
返回了一个新的函数,然后立即对这个新函数进行了调用,传递了参数(i, dice)
。这种写法是函数的嵌套调用。让我们分解这个表达式的含义:
make_averaged(roll_dice, trials_count)
:这部分代码调用了函数make_averaged
,并传递了两个参数roll_dice
和trials_count
。make_averaged
函数返回了一个新的函数,该函数用于计算指定函数的平均值,其中roll_dice
是要计算平均值的函数,trials_count
是进行平均值计算时的试验次数。(i, dice)
:这部分是对上述返回的新函数的调用。这个新函数接受两个参数,分别是i
和dice
。在这个上下文中,i
表示骰子的数量,dice
表示模拟掷骰子的函数。这个调用返回了i
个骰子使用dice
模拟的平均得分。所以,整个表达式
make_averaged(roll_dice, trials_count)(i, dice)
的作用是计算使用i
个骰子、模拟掷骰子的函数dice
的平均得分,基于trials_count
次试验。这个平均值是通过嵌套调用两个函数实现的。