Skip to content

Problem 2: Two of Three (100pts)

Problem

Write a function that takes three positive numbers and returns the sum of the squares of the two largest numbers. Use only a single line for the body of the function.

Warning:

You should not use data structures like lists and the sorted function in this problem.

Violating this requirement may result in 0 score in online judge testing.

编写一个函数,接受三个正数作为参数,并返回其中两个最大数平方和。函数主体只能有一行代码。

Warning

在本问题中,您不应使用列表等数据结构或 sorted 函数。

违反此要求可能导致 OJ 测试得分为 0。

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

    >>> 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
    >>> check_single_return(two_of_three)
    >>> check_no_square_brackets(two_of_three)
    """
    return _____

Hints

Hint: Consider using the max or min function:

>>> max(1, 2, 3)
3
>>> min(-1, -2, -3)
-3
  • 考虑使用 maxmin 函数

Solutions