Homework 1: Functions, Control
1206字约4分钟
2024-10-31
Required Questions
Q1: A Plus Abs B
Python 的 operator
模块包含两个参数函数,例如 Python 内置算术运算符的 add
和 sub
。例如, add(2, 3)
的计算结果为 5,就像表达式 2 + 3
一样。
填写以下函数中的空格,用于将 a
添加到 b
的绝对值,而不调用 abs
。除了两个空格之外,您不得修改任何提供的代码。
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
>>> a_plus_abs_b(-1, 4)
3
>>> a_plus_abs_b(-1, -4)
3
"""
if b < 0:
f = _____
else:
f = _____
return f(a, b)
以下内容包含答案
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
>>> a_plus_abs_b(-1, 4)
3
>>> a_plus_abs_b(-1, -4)
3
"""
if b < 0:
f = _____
f = sub
else:
f = _____
f = add
return f(a, b)
使用 Ok 来测试你的代码:
python ok -q a_plus_abs_b
使用 Ok 运行本地语法检查器(检查您没有修改除两个空格之外的任何提供的代码):
python ok -q a_plus_abs_b_syntax_check
Q2: Two of Three
编写一个函数,以三个正数作为参数,返回两个最小数的平方和。函数主体仅使用一行。
def two_of_three(i, j, k):
"""Return m*m + n*n, where m and n are the two smallest members of the
positive numbers i, j, and k.
>>> two_of_three(1, 2, 3)
5
>>> two_of_three(5, 3, 1)
10
>>> two_of_three(10, 2, 8)
68
>>> two_of_three(5, 5, 5)
50
"""
return _____
以下内容包含答案
def two_of_three(i, j, k):
"""Return m*m + n*n, where m and n are the two smallest members of the
positive numbers i, j, and k.
>>> two_of_three(1, 2, 3)
5
>>> two_of_three(5, 3, 1)
10
>>> two_of_three(10, 2, 8)
68
>>> two_of_three(5, 5, 5)
50
"""
return _____
return i * i + j * j + k * k - max(i, j , k) * max(i, j , k)
提示
考虑使用最大值或最小值函数:
>>> max(1, 2, 3)
3
>>> min(-1, -2, -3)
-3
使用 Ok 来测试你的代码:
python ok -q two_of_three
使用 Ok 运行本地语法检查器(检查您没有修改除两个空格之外的任何提供的代码):
python ok -q two_of_three_syntax_check
Q3: Largest Factor
编写一个函数,接受大于 1 的整数 n
,并返回小于 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 ***"
以下内容包含答案
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 ***"
for i in range(1, n // 2 + 1):
if n % i == 0:
res = i
return 1 if res == n else res
提示
要检查 b
是否能整除 a
,请使用表达式 a % b == 0
,可以理解为“ a
除以 b
后的余数为 0”。
使用 Ok 来测试你的代码:
python ok -q largest_factor
Q4: Hailstone
道格拉斯·霍夫施塔特 (Douglas Hofstadter) 的普利策奖获奖著作《哥德尔、埃舍尔、巴赫》提出了以下数学难题。
选择一个正整数
n
作为起点。如果
n
是偶数,则将其除以 2。如果
n
是奇数,则将其乘以 3 并加 1。继续此过程,直到
n
为 1。
数字 n
将上下移动,但最终以 1 结束(至少对于所有曾经尝试过的数字而言——没有人证明过该序列会终止)。类似地,冰雹在大气层中上下移动,最终降落到地球上。
这个 n
值序列通常称为冰雹序列。编写一个函数,该函数接受一个形式参数名为 n
的参数,打印出从 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
>>> b = hailstone(1)
1
>>> b
1
"""
"*** YOUR CODE HERE ***"
以下内容包含答案
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
>>> b = hailstone(1)
1
>>> b
1
"""
"*** YOUR CODE HERE ***"
print(n)
cnt = 1
while n != 1:
if n % 2 == 0:
n //= 2
else:
n = n * 3 + 1
print(n)
cnt += 1
return cnt
冰雹序列可能会很长!试试 27。您能找到最长的是多少?
提示
请注意,如果最初 n == 1
,则该序列长度为一步。
如果您看到 4.0 但只想要 4,请尝试使用向下取整除法 //
而不是常规除法 /
。
使用 Ok 来测试你的代码:
python ok -q hailstone
对冰雹序列感到好奇?看看这篇文章: