作业1:变量和函数,控制
Homework 1: Variables & Functions, Control

Due by 11:59pm on Thursday, September 3

查看英文原文

说明

下载 hw01.zip.

提交:完成后使用python3 ok --submit来提交代码。你可以在截止日期前多次提交;只有最后一次提交会被评分。请检查你是否成功提交了代码到okpy.org。更多提交作业的说明请参见Lab0

使用Ok:如果你对使用Ok有任何疑问,请参考本指南

阅读材料:以下阅读材料可能对你有帮助

注意:其中一些阅读材料是作业问题所必需的,但直到周一的控制讲座才会涉及。

评分:作业评分基于正确性。每个错误的问题将使总分减少一分。课程大纲中有作业恢复政策。 本次作业满分为2分。

必答题

作业提示视频

本视频为解决本次作业的问题提供一些指导。

Q1: 课程大纲测验

请根据我们课程大纲页面上的政策完成课程大纲测验

Q2: A 加上 B 的绝对值

在以下函数中填空,在不调用abs函数的情况下将a加到b的绝对值上。除了两个空白处以外,你不能修改所提供的代码。

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
    >>> # a check that you didn't change the return statement!
    >>> import inspect, re
    >>> re.findall(r'^\s*(return .*)', inspect.getsource(a_plus_abs_b), re.M)
    ['return f(a, b)']
    """
    if b < 0:
        f = _____
    else:
        f = _____
    return f(a, b)

使用 Ok 来测试你的代码:

python3 ok -q a_plus_abs_b --local

Q3: 三个数中取两个

编写一个函数,该函数接受三个正数作为参数,并返回其中两个最小数的平方和。函数体只能使用一行代码。

def two_of_three(x, y, z):
    """Return a*a + b*b, where a and b are the two smallest members of the
    positive numbers x, y, and z.

    >>> 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
    >>> # check that your code consists of nothing but an expression (this docstring)
    >>> # a return statement
    >>> import inspect, ast
    >>> [type(x).__name__ for x in ast.parse(inspect.getsource(two_of_three)).body[0].body]
    ['Expr', 'Return']
    """
    return _____

提示:考虑使用maxmin函数:

>>> max(1, 2, 3)
3
>>> min(-1, -2, -3)
-3

使用 Ok 来测试你的代码:

python3 ok -q two_of_three --local

Q4: 最大因子

编写一个函数,该函数接受一个大于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 ***"

提示:要确定b是否能整除a, 你可以使用表达式a % b == 0,它的意思是: “a除以b的余数为0。”

使用 Ok 来测试你的代码:

python3 ok -q largest_factor --local

Q5: If 函数 vs 语句

下面是一个函数,该函数与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

尽管上面 doctests 的结果与if语句的结果相同,但实际上在任何情况下它并if语句执行相同的操作。 为了证明这一点,请你编写函数condtrue_funcfalse_func,使得 with_if_statement 打印数字47,但是with_if_function打印4247

def with_if_statement():
    """
    >>> result = with_if_statement()
    47
    >>> print(result)
    None
    """
    if cond():
        return true_func()
    else:
        return false_func()

def with_if_function():
    """
    >>> result = with_if_function()
    42
    47
    >>> print(result)
    None
    """
    return if_function(cond(), true_func(), false_func())

def cond():
    "*** YOUR CODE HERE ***"

def true_func():
    "*** YOUR CODE HERE ***"

def false_func():
    "*** YOUR CODE HERE ***"

提示:如果你很难区分if语句和if_function, 请看一下if 语句的求值规则调用表达式

使用 Ok 来测试你的代码:

python3 ok -q with_if_statement --local
python3 ok -q with_if_function --local

Q6: 冰雹序列

Douglas Hofstadter在普利策奖获奖书籍 Gödel, Escher, Bach 中提出了以下数学猜想。

  1. 选择一个正整数n作为开始。
  2. 如果n是偶数,将它除以2。
  3. 如果n是奇数,将它乘以3再加1。
  4. 继续这个过程直到n变为1。

数字n会上下波动,但最终会变为1(至少对于所有尝试过的数字来说——没有人曾经证明这个序列会终止)。类似地,冰雹在最终落到地面之前会在大气中上下波动。

突发新闻(或者至少在数学中最接近突发新闻的东西)。去年冰雹猜想有了最新进展,表明如果你重复这个过程,几乎所有的数字最终都会变为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
    """
    "*** YOUR CODE HERE ***"

冰雹序列可能会变得非常长!试试27。你找到的最长序列是什么?

使用 Ok 来测试你的代码:

python3 ok -q hailstone --local

提交

确保通过运行以下命令提交此作业:

python3 ok --submit