日拱一卒,一起来学伯克利CS61A,先做几道有趣的Python热身题
起来,伯克利,CS61A,几道,有趣,Python,热身
2025-03-25 08:59:26 时间
作者 | 梁唐
大家好,日拱一卒,我是梁唐。
最近上了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基础17-面向对象
- Python 学生信息管理系统——文章中源码100%真实有效—–如何将类、初始化属性、模块、循环判断、静态方法等一系列知识点结合起来做一个项目「建议收藏」
- python 四大基础数据结构及操作
- mt4 python_一个使用Python自动化交易外汇MT4脚本实现「建议收藏」
- 用python给女朋友表白_python绘制太阳花
- java浅拷贝和深拷贝的区别_python的浅拷贝和深拷贝
- Python计算中位数_用频率直方图求中位数
- 简单的Python脚本,实现ssh登录配置路由器
- Python基础15-日志模块logging
- 手把手教你用Python破解邻家小妹wifi密码
- Python 随机(Random)模块的不可预测之美
- Python 模板渲染库 yaml 和 jinja2 的实战经验分享
- xgboost分类算法_python分类统计
- javascript typescript_typescript python
- Python实现教务信息管理系统
- 工具推荐|利用python-cdo高效处理气象数据
- 用Python爬了微信好友,原来他们是这样的人...
- python re.compile() 详解——Python正则表达式「建议收藏」
- python分子化学模拟_#分子模拟#MDTraj分子模拟python包(一)