【CS61A 2024秋】Python入门课,全过程记录P2(Week3开始,更新中2024/11/24)
文章目录
- 关于
- 基本介绍👋
- Week 3
- Mon Environments
- 阅读材料
- Lab 02: Higher-Order Functions, Lambda Expressions
- Q1: WWPD: The Truth Will Prevail
- Q2: WWPD: Higher-Order Functions
- Q3: WWPD: Lambda
关于
个人博客,里面偶尔更新,最近比较忙。发一些总结的帖子和思考。
江湖有缘相见🤝。如果读者想和我交个朋友可以加我好友(见主页or个人博客),共同学习。笔者是学生,课业还是比较繁重的,可能回复不及时。笔者也正在四处寻找一些可以兼职锻炼知识并且补贴一些生活的工作,如果读者需要一些详细的辅导,或者帮助完成一些简易的lab也可以找我,笔者还是学生,自以为才学有限,也没有高价的理由📖。
基本介绍👋
CS61A是加州大学伯克利分校(UC Berkeley)计算机科学专业的入门课程,全名为Structure and Interpretation of Computer Programs。
这是入门的Python课程,因此这篇博客会默认读者没有很多的编程背景,讲的东西会比较基础,有编程经验的读者可以快速浏览。
首先贴上课程网址。
CS61A课程主页
感觉课程主页还是非常精美的,相比MIT6.828的课程主页来说。
这篇博客也根据日程表的日程来编写。
Week 3
Mon Environments
阅读材料
没啥好说的,读者自行阅读。
Lab 02: Higher-Order Functions, Lambda Expressions
Q1: WWPD: The Truth Will Prevail
WWPD是What Would Python Display?的缩写,这个小问题是测试短路(short-circuit)的知识点,布尔表达式在能够判断真假的时候就不会查看后续子句了。
python的布尔表达式返回的是能确定真假的时候的原来的值,None和0都表示False。
D:\Desktop\cs61a\labs\lab02>python ok -q short-circuit -u --local
=====================================================================
Assignment: Lab 2
OK, version v1.18.1
=====================================================================
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unlocking tests
At each "? ", type what you would expect the output to be.
Type exit() to quit
---------------------------------------------------------------------
The Truth Will Prevail > Suite 1 > Case 1
(cases remaining: 4)
What would Python display? If you get stuck, try it out in the Python
interpreter!
>>> True and 13
? 13
-- OK! --
>>> False or 0
? 0
-- OK! --
>>> not 10
? False
-- OK! --
>>> not None
? True
-- OK! --
---------------------------------------------------------------------
The Truth Will Prevail > Suite 2 > Case 1
(cases remaining: 3)
What would Python display? If you get stuck, try it out in the Python
interpreter!
>>> True and 1 / 0 # If this errors, just type Error.
? Error
-- OK! --
>>> True or 1 / 0 # If this errors, just type Error.
? True
-- OK! --
>>> -1 and 1 > 0 # If this errors, just type Error.
? True
-- OK! --
>>> -1 or 5
? -1
-- OK! --
>>> (1 + 1) and 1 # If this errors, just type Error. If this is blank, just type Nothing.
? 1
-- OK! --
---------------------------------------------------------------------
The Truth Will Prevail > Suite 2 > Case 2
(cases remaining: 2)
What would Python display? If you get stuck, try it out in the Python
interpreter!
>>> print(3) or ""
(line 1)? 3
(line 2)? ""
-- OK! --
---------------------------------------------------------------------
The Truth Will Prevail > Suite 3 > Case 1
(cases remaining: 1)
What would Python display? If you get stuck, try it out in the Python
interpreter!
>>> def f(x):
... if x == 0:
... return "zero"
... elif x > 0:
... return "positive"
... else:
... return ""
>>> 0 or f(1)
? "positive"
-- OK! --
>>> f(0) or f(-1)
? "zero"
-- OK! --
>>> f(0) and f(-1)
? ""
-- OK! --
---------------------------------------------------------------------
Q2: WWPD: Higher-Order Functions
这个quiz用来考察,高阶函数用法。
>>> # If Python displays <function...>, type Function, if it errors type Error, if it displays nothing type Nothing
>>> def cake():
... print('beets')
... def pie():
... print('sweets')
... return 'cake'
... return pie
>>> chocolate = cake()
? beets
-- OK! --
这个的输出是beets。cake函数的返回值是pie,这里将cake的返回值赋值给chocolate,并没有打印单个chocolate的数据类型,仅仅运行了cake函数,打印了beets。
>>> chocolate()
(line 1)? sweets
(line 2)? "cake"
-- OK! --
这个调用的chocolate锁绑定的函数,也就是pie,首先打印sweet,由于print是打印到标准输出流因此没有引号,然后显示chocolate的返回值,字符串类型显示的时候会把单引号变成双引号。
>>> more_chocolate, more_cake = chocolate(), cake
? sweets
-- OK! --
>>> more_chocolate
? "cake"
-- OK! --
这里并没有调用cake函数,只是绑定cake的函数名。
>>> # Reminder: cake, more_cake, and chocolate were defined/assigned in the code above!
>>> # It might be helpful to refer to their definitions on the assignment website so you don't have to scroll as much!
>>> def snake(x, y):
... if cake == more_cake:
... return chocolate
... else:
... return x + y
>>> snake(10, 20)
? Function
-- OK! --
>>> snake(10, 20)()
(line 1)? sweets
(line 2)? "cake"
-- OK! --
>>> cake = 'cake'
>>> snake(10, 20)
? 30
-- OK! --
上面定义了,chocolate是pie函数,并且more_cake绑定了cake,所以返回chocolate,也就是函数。然后一个输入等价于pie(),不等号不成立就输出x+y即可。
Q3: WWPD: Lambda
考察Lambda语法。
D:\Desktop\cs61a\labs\lab02>python3 ok -q lambda -u --local
D:\Desktop\cs61a\labs\lab02>python ok -q lambda -u --local
=====================================================================
Assignment: Lab 2
OK, version v1.18.1
=====================================================================
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unlocking tests
At each "? ", type what you would expect the output to be.
Type exit() to quit
---------------------------------------------------------------------
Lambda the Free > Suite 1 > Case 1
(cases remaining: 5)
Q: Which of the following statements describes a difference between a def statement and a lambda expression?
Choose the number of the correct choice:
0) A lambda expression cannot return another function.
1) A lambda expression does not automatically bind the function that it returns to a name.
2) A lambda expression cannot have more than two parameters.
3) A def statement can only have one line in its body.
? 1
-- OK! --
---------------------------------------------------------------------
Lambda the Free > Suite 1 > Case 2
(cases remaining: 4)
Q: How many formal parameters does the following lambda expression have?
lambda a, b: c + d
Choose the number of the correct choice:
0) two
1) Not enough information
2) one
3) three
? 0
-- OK! --
---------------------------------------------------------------------
Lambda the Free > Suite 1 > Case 3
(cases remaining: 3)
Q: When is the return expression of a lambda expression executed?
Choose the number of the correct choice:
0) When you pass the lambda expression into another function.
1) When the function returned by the lambda expression is called.
2) When you assign the lambda expression to a name.
3) When the lambda expression is evaluated.
? 1
-- OK! --
---------------------------------------------------------------------
Lambda the Free > Suite 2 > Case 1
(cases remaining: 2)
What would Python display? If you get stuck, try it out in the Python
interpreter!
>>> # If Python displays <function...>, type Function, if it errors type Error, if it displays nothing type Nothing
>>> lambda x: x # A lambda expression with one parameter x
? Function
-- OK! --
>>> a = lambda x: x # Assigning a lambda function to the name a
>>> a(5)
? 5
-- OK! --
>>> (lambda: 3)() # Using a lambda expression as an operator in a call exp.
? 3
-- OK! --
>>> b = lambda x, y: lambda: x + y # Lambdas can return other lambdas!
>>> c = b(8, 4)
>>> c
? Function
-- OK! --
>>> c()
? 12
-- OK! --
>>> d = lambda f: f(4) # They can have functions as arguments as well
>>> def square(x):
... return x * x
>>> d(square)
? 16
-- OK! --
---------------------------------------------------------------------
Lambda the Free > Suite 2 > Case 2
(cases remaining: 1)
What would Python display? If you get stuck, try it out in the Python
interpreter!
>>> # Try drawing an environment diagram if you get stuck!
>>> higher_order_lambda = lambda f: lambda x: f(x)
>>> g = lambda x: x * x
>>> higher_order_lambda(2)(g) # Which argument belongs to which function call?
? Error
-- OK! --
>>> higher_order_lambda(g)(2)
? 4
-- OK! --
>>> call_thrice = lambda f: lambda x: f(f(f(x)))
>>> call_thrice(lambda y: y + 1)(0)
? 3
-- OK! --
>>> print_lambda = lambda z: print(z) # When is the return expression of a lambda expression executed?
>>> print_lambda
? Function
-- OK! --
>>> one_thousand = print_lambda(1000)
? 1000
-- OK! --
>>> one_thousand # What did the call to print_lambda return? If it displays nothing, write Nothing
? Nothing
-- OK! --
---------------------------------------------------------------------
OK! All cases for Lambda the Free unlocked.
Cannot backup when running ok with --local.