日拱一卒,一起来学伯克利CS61A,先做几道有趣的Python热身题
Python 起来 有趣 伯克利 几道 CS61A
2023-06-13 09:11:29 时间
作者 | 梁唐
大家好,日拱一卒,我是梁唐。
最近上了CS61A的公开课,还做了它们的作业,非常有意思,分享给大家。
这是这门课的第一份作业,作业基于Python,需要了解一点基础的Python语法,非常有意思。即使没有听过课也没有关系,因为课上其实没讲太多和作业有关的内容,感兴趣的同学不妨试着自己做做。
我们先来看题目,解答放在最后。
Q1: A Plus Abs B
给定两个数a和b,要求实现a + abs(b)
,但不允许调用abs
函数,其中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)
Q2:Two of Three
给定三个数,要求返回三个数中最大的两个数的平方和,并且只能填写一行代码。
def two_of_three(a, b, c):
"""Return x*x + y*y, where x and y are the two largest members of the
positive numbers a, b, and c.
>>> two_of_three(1, 2, 3)
13
>>> two_of_three(5, 3, 1)
34
>>> two_of_three(10, 2, 8)
164
>>> two_of_three(5, 5, 5)
50
"""
return _____
Q3:Largest Factor
给定一个整数n,要求返回除了n本身以外最大的因数。
def largest_factor(n):
"""Return the largest factor of n that is smaller than n.
>>> largest_factor(15) # factors are 1, 3, 5
5
>>> largest_factor(80) # factors are 1, 2, 4, 5, 8, 10, 16, 20, 40
40
>>> largest_factor(13) # factor is 1 since 13 is prime
1
"""
"*** YOUR CODE HERE ***"
Q4:If Function vs Statement
这题很有意思,首先给定一段代码实现类似if语句的功能。
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
需要实现三个函数c
,t
,f
:
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
函数返回1,而with_if_function
拥有不同的结果。
Q5: Hailstone
实现函数,给定整数n,反复执行如下过程:
- 如果n是偶数,则除以2
- 如果n是奇数,则乘3再加上1
- 如果n等于1,退出
要求函数当中打印n变化的过程,并且返回一共变化的次数
def hailstone(n):
"""Print the hailstone sequence starting at n and return its
length.
>>> a = hailstone(10)
10
5
16
8
4
2
1
>>> a
7
"""
"*** YOUR CODE HERE ***"
解答
Q1
根据b的符号,决定f是加法或者减法
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 = sub
else:
f = add
return f(a, b)
Q2
这题我想了很久,逻辑很简单,但是要一行代码实现不容易。
后来发现要找出这三个元素当中较大的两个元素不容易,但可以先把三个数的平方和都相加,最后再减去最小数的平方和。
def two_of_three(a, b, c):
"""Return x*x + y*y, where x and y are the two largest members of the
positive numbers a, b, and c.
>>> two_of_three(1, 2, 3)
13
>>> two_of_three(5, 3, 1)
34
>>> two_of_three(10, 2, 8)
164
>>> two_of_three(5, 5, 5)
50
"""
return a * a + b * b + c * c - min(a, b, c) * min(a, b, c)
Q3
我这里从2开始遍历到
寻找因数,如果都不能整除返回1
def largest_factor(n):
"""Return the largest factor of n that is smaller than n.
>>> largest_factor(15) # factors are 1, 3, 5
5
>>> largest_factor(80) # factors are 1, 2, 4, 5, 8, 10, 16, 20, 40
40
>>> largest_factor(13) # factor is 1 since 13 is prime
1
"""
"*** YOUR CODE HERE ***"
import math
for i in range(2, math.ceil(math.sqrt(n))):
if n % i == 0:
return n // i
return 1
Q4
这两种if实现方式的区别在于return if_function(c(), t(), f())
语句不论c()
返回的结果是True
还是False
,t
和f
函数都会执行,因为只有执行了才能有值传入函数。而with_if_statement
则不然,利用这点实现即可
def c():
"*** YOUR CODE HERE ***"
return False
def t():
"*** YOUR CODE HERE ***"
print("it's running")
def f():
"*** YOUR CODE HERE ***"
return 1
Q5
根据描述实现逻辑即可,没有难度
def hailstone(n):
"""Print the hailstone sequence starting at n and return its
length.
>>> a = hailstone(10)
10
5
16
8
4
2
1
>>> a
7
"""
"*** YOUR CODE HERE ***"
step = 1
while True:
print(n)
if n == 1:
break
if n % 2 == 0:
n = n // 2
else:
n = n * 3 + 1
step += 1
return step
我也把我的代码传到了GitHub上
相关文章
- python中矩阵的转置怎么写_Python 矩阵转置的几种方法小结
- python十进制转换_Python 进制转换
- 浙江新增python编程_9月起,浙江省八年级新增Python编程课,未来编程是处理大数据的手段…「建议收藏」
- python常用面试题_Python+Selenium 常见面试题整理[通俗易懂]
- python整除和取余写法_Python的整除和取余[通俗易懂]
- python安装不了whl文件_Python安装whl文件过程图解
- python中dtype的使用规范_Python numpy.dtype() 使用实例
- 怎么用python打开csv文件_Python文本处理之csv-csv文件怎么打开[通俗易懂]
- python生兔子问题(递归算法)_java实现斐波那契数列
- 【说站】python创建链表的两种形式
- 【说站】python集合的底层实现
- 【测试开发】python系列教程: 基础语法
- Python的基础知识_python的基本知识点
- Python自动化之Python循环语句
- python的实例_类_静态方法—特殊函数—状态码
- 从尾到头打印链表(python题解)
- 【测试开发】python系列教程:Python数据类型转换
- 【测试开发】python系列教程:Python 运算符
- Python玩转九九乘法表
- 单细胞python复现|| 01-文献解读:肺癌转移中的再生谱系和免疫介导的杀伤
- python 抓取错误
- Python线程-线程的互斥
- 一步一步学会在Linux上运行Python程序(linux运行python程序)
- Linux创建Python文件的步骤(linux新建python文件)
- 搞定!Linux下快速设置Python环境变量(linux设置python环境变量)
- 学习Python精通SQL Server操作技巧(python操作sqlserver)
- python闭包的作用详解编程语言
- Python连接MySQL数据库:初学者指南(python入mysql)
- Linux下从系统卸载Python(linux下卸载python)
- Linux Python:开启互联网新时代(linux python)
- 使用python分析gitlog日志示例
- Python中的map、reduce和filter浅析