前不久在知乎上看到CS61A 和CS61B spring18 开课的消息。上去看了一眼,发现真的不错,所有proj hw都可以本地测试!也就是说除了没有课程成绩和官方讨论区和TA解答外,这个课完全可以上!(还需要翻墙看油管)简直棒啊。于是打算寒假刷一波61A 61B 在此也立下flag

61A hw01

Q1: A Plus Abs B

Fill in the blanks in the following function definition for adding a to the absolute value of b, without calling abs.

from operator import add, sub

def a_plus_abs_b(a, b):
"""Return a+abs(b), but without calling abs. >>> a_plus_abs_b(2, 3)
5
>>> a_plus_abs_b(2, -3)
5
"""
if b < 0:
f = _____
else:
f = _____
return f(a, b)

Use Ok to test your code:

python3 ok -q a_plus_abs_b

  第一题就出问题简直好气。实际上python很灵活,我学了C以为赋值只能是值赋值,要么就函数指针,结果这个居然可以f = add  和 f = sub!惊呆了

Q4: If Function vs Statement

 
def if_function(condition, true_result, false_result):
"""Return true_result if condition is a true value, and
false_result otherwise. >>> if_function(True, 2, 3)
2
>>> if_function(False, 2, 3)
3
>>> if_function(3==2, 3+2, 3-2)
1
>>> if_function(3>2, 3+2, 3-2)
5
"""
if condition:
return true_result
else:
return false_result
def with_if_statement():
"""
>>> with_if_statement()
1
"""
if c():
return t()
else:
return f() def with_if_function():
return if_function(c(), t(), f()) def c():
"*** YOUR CODE HERE ***" def t():
"*** YOUR CODE HERE ***" def f():
"*** YOUR CODE HERE ***"

在上面填写代码,让with_if_statement return 1但是with_if_function return 不是1;

这里主要考察两个知识点:1.if-else语句有一句后面不会执行 2.函数调用值传递必须在外面算完值才会传递进去

有了这两条,大家也知道该怎么做了吧

 

CS61A hw01的更多相关文章

  1. [CS61A] Lecture 1&2&3. Introduction&Functions&Control

    [CS61A] Lecture 1&2&3. Introduction&Functions&Control 前言 CS61A是加州大学伯克利分校一门计算机专业课程,用于 ...

  2. CS61A Homework: Church Numerals

    Church Numerals Nagging 南大的 SICP 实际上是 Berkeley CS61A 的 clone ,所以我有幸做到了这个 Homework02. 此外要感谢选课系统,让我一个工 ...

  3. [CS61A] Lecture 5&6&7. Environments & Design & Functions Examples & Homework 2: Higher Order Functions

    [CS61A] Lecture 5&6&7. Environments & Design & Functions Examples & Homework 2: ...

  4. [CS61A] Lecture 4. Higher-Order Functions & Project 1: The Game of Hog

    [CS61A] Lecture 4. Higher-Order Functions & Project 1: The Game of Hog Lecture Lecture 4. Higher ...

  5. CS61A Lecture3 Note

    本次lec主讲控制流 本文档只列一些py控制流与C不同的地方  print的功能不同 可以print出来None这种东西 重点讲了函数运行机制,我的理解是这样的,在调用函数之前,def会产生一个glo ...

  6. 【转】Python资源 转自 dylanninin.com

    http://dylanninin.com/blog/2013/11/23/python_resource.html Python是在工作期间零零碎碎学习起来的.当时正值部门申购图书,鉴于Python ...

  7. 使用Json Template在Azure China创建ARM类型的虚拟机

    前面几篇文章介绍过Azure的两种VM的模式,包括ASM和ARM.并且介绍了如何用Azure CLI和PowerShell创建虚拟机.本文将介绍如何采用Json的Template来创建基于ARM的VM ...

  8. Java 方法的应用

    Java方法可以理解为C#中的函数,都是把复杂的问题简单化,按模块,按功能区分,分别完成各个部分在调用这些方法完成整个功能. 方法的综合练习,猜数字的实现: 代码要求: 生成不重复的4位数字(只有1- ...

  9. Python 的经典入门书籍

    实python非常适合初学者入门,上手很容易.我就是完全通过网上资源学了python的.最大的是3点经验:1.找一本浅显易懂,例程比较好的教程,从头到尾看下去.不要看很多本,专注于一本.把里面的例程都 ...

随机推荐

  1. Ubuntu iso下载地址(14、16、18)

    Ubuntu镜像,快速下载 ubuntu 14.04: http://mirrors.aliyun.com/ubuntu-releases/14.04/ubuntu 16.04: http://mir ...

  2. 【c# 数据库】 多表链接

    1.inner join string sql = "select * from studentsinfo inner join teacher on teacher.姓名 = studen ...

  3. AOP-事物管理

    1. Spring AOP 模块为基于 Spring 的应用程序中的对象提供了事务管理服务

  4. Kubernetes 1.3.1 快速单机部署

    Kubernetes发展到今天, 在官网上已经有非常多的成熟部署方案, 但是由于墙的原因, 最简单的MiniKube都无法进行, 参考了以下两篇文章后, 终于安装成功. k8s-1.13版本测试环境搭 ...

  5. Devexpress使用之:GridControl控件

    Devexpress使用之:GridControl控件 Devexpress系列控件功能很强大,使用起来也不太容易,我也是边摸索边使用,如果有时间我会把常用控件的使用方法整理出来的. using Sy ...

  6. Kafka 性能测试报告

    Producer command: kafka-producer-perf-test --topic _perf-test --num-records 10000000 --record-size 1 ...

  7. identityserver4 代码系列

    链接:https://pan.baidu.com/s/1ePLwUxGpIPObwA8nnfDT9w 提取码:gr0x

  8. WebService CXF知识总结

    2018-10-23 <wsdl:service name="Iptv3aBasicService"> 客户端client信息,CXF会生成一个名为Iptv3ABasi ...

  9. css3 --linear-gradient-渐变色

    //由上至下变色 background:-moz-linear-gradient( top,#f9b347,#f4ad40,#f9b347);  background:-webkit-gradient ...

  10. ORB-SLAM2的安装和运行流程

    一.ORB-SLAM2安装 1.在https://github.com/raulmur/ORB_SLAM2上git clone到当前文件夹内,若想下载到指定文件夹内,就需要cd进入指定文件内,然后再g ...