Files
fzu-product/2023旧版内容/3.编程思维体系构建/3.6.4.1阶段一:熟悉语句.md

1.6 KiB
Raw Blame History

阶段一:熟悉语句

::: warning 🐱 在进行本章之前,请你谨记一个原则:基本所有的功能都被人提前实现好了

你需要关心的仅仅是逻辑该如何设立

在做本章任务前,请熟悉 python 的函数,循环和判断语句即可 :::

P1请仅使用一行语句求出三个数的最小平方和

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)
    >>> # and 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 _____

提示:可以使用 min() 函数哦

P2下降阶乘

def falling(n, k):
    """Compute the falling factorial of n to depth k.

    >>> falling(6, 3)  # 6 * 5 * 4
    120
    >>> falling(4, 3)  # 4 * 3 * 2
    24
    >>> falling(4, 1)  # 4
    4
    >>> falling(4, 0)
    1
    """
    "*** YOUR CODE HERE ***"

P3判断一个函数是否有两个或者两个连续的 8

def double_eights(n):
    """Return true if n has two eights in a row.
    
    >>> double_eights(8)
    False
    >>> double_eights(88)
    True
    >>> double_eights(2882)
    True
    >>> double_eights(880088)
    True
    >>> double_eights(12345)
    False
    >>> double_eights(80808080)
    False
    """
    "*** YOUR CODE HERE ***"