日拱一卒,伯克利CS61A,作业1,几道有趣的Python热身题
大家好,日拱一卒,我是梁唐。
本文始发于公众号:Coder梁
最近上了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开始遍历到n\sqrt nn寻找因数,如果都不能整除返回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
转载自:https://juejin.cn/post/7090047648962953252