大家好,日拱一卒,我是梁唐。
本文始发于大众号:Coder梁
最近上了CS61A的公开课,还做了它们的作业,十分有意思,分享给大家。
作业原文
Github
这是这门课的第一份作业,作业基于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 n寻找因数,假如都不能整除回来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