CS61A Homework 2: Higher Order Functions 高阶函数

302 阅读3分钟

这一节的Homework不难,而且四道题,删成两道题目了。第二道题目的话,没有想出来。后来想了一下,其实挺简单的;错误示范我也放在这里面了

Q1: Product

这一题,其实就是对于参数n采取什么策略的意思;题目不难

def product(n, term):
    """Return the product of the first n terms in a sequence.
​
    n: a positive integer
    term:  a function that takes one argument to produce the term
​
    >>> product(3, identity)  # 1 * 2 * 3
    6
    >>> product(5, identity)  # 1 * 2 * 3 * 4 * 5
    120
    >>> product(3, square)    # 1^2 * 2^2 * 3^2
    36
    >>> product(5, square)    # 1^2 * 2^2 * 3^2 * 4^2 * 5^2
    14400
    >>> product(3, increment) # (1+1) * (2+1) * (3+1)
    24
    >>> product(3, triple)    # 1*3 * 2*3 * 3*3
    162
    """
    "*** YOUR CODE HERE ***"
    # 傻了,啥玩意*0不都是等于0 嘛
    result = 1
    while n:
        result *= term(n)
        n -= 1
​
    return result

Q2: Accumulate

这一题是前面一题的进一步,我们把方法抽象出来形成一个函数,然后通过这个函数,来返回具体数据;

这一题,第一个需要考虑,merge的执行流程.我们需要考虑循环的时候,merge函数的最终结果;

👱‍♀ 举个例子;

accumulate(add,0,5,identify)
​
    我们可以知道,应该是 0 + (1+2+3+4+5) 我们需要循环执行获取1-5的最终结果.所以需要一个循环;这个循环采用的是identify方法,也就是 while n: identify(5) 
    但是我们需要将前一个结果和后一个结果相加,那么就导致一个现象,我们需要用到add方法了.所以我们add方法需要包含在循环里面,所以应该是while n: add(0,identify(5))
    还需要考虑一点,就是我们不能使用+= 而使用赋值运算符,因为add 也就是merger函数,是包含了前面的结果的;
def accumulate(merger, start, n, term):
    """Return the result of merging the first n terms in a sequence and start.
    The terms to be merged are term(1), term(2), ..., term(n). merger is a
    two-argument commutative function.
​
    >>> accumulate(add, 0, 5, identity)  # 0 + 1 + 2 + 3 + 4 + 5
    15
    >>> accumulate(add, 11, 5, identity) # 11 + 1 + 2 + 3 + 4 + 5
    26
    >>> accumulate(add, 11, 0, identity) # 11
    11
    >>> accumulate(add, 11, 3, square)   # 11 + 1^2 + 2^2 + 3^2
    25
    >>> accumulate(mul, 2, 3, square)    # 2 * 1^2 * 2^2 * 3^2
    72
    >>> # 2 + (1^2 + 1) + (2^2 + 1) + (3^2 + 1)
    >>> accumulate(lambda x, y: x + y + 1, 2, 3, square)
    19
    >>> # ((2 * 1^2 * 2) * 2^2 * 2) * 3^2 * 2
    >>> accumulate(lambda x, y: 2 * x * y, 2, 3, square)
    576
    >>> accumulate(lambda x, y: (x + y) % 17, 19, 20, square)
    16
    """
    "*** YOUR CODE HERE ***"
    sum_total = start
    while n:
        sum_total = merger(sum_total, term(n))
        n -= 1
    return sum_total

沿用这个就行了,但是Ok对于报错页面返回有问题;直接贴结果了

Q3 product_using_accumulate

def product_using_accumulate(n, term):
    """Returns the product: term(1) * ... * term(n), using accumulate.
​
    >>> product_using_accumulate(4, square)
    576
    >>> product_using_accumulate(6, triple)
    524880
    >>> # You aren't expected to understand the code of this test.
    >>> # Check that the bodies of the functions are just return statements.
    >>> # If this errors, make sure you have removed the "***YOUR CODE HERE***".
    >>> import inspect, ast
    >>> [type(x).__name__ for x in ast.parse(inspect.getsource(product_using_accumulate)).body[0].body]
    ['Expr', 'Return']
    """
    "*** YOUR CODE HERE ***"
    return accumulate(mul, 1, n, term)

Q4 summation_using_accumulate

def summation_using_accumulate(n, term):
    """Returns the sum: term(0) + ... + term(n), using accumulate.
​
    >>> summation_using_accumulate(5, square)
    55
    >>> summation_using_accumulate(5, triple)
    45
    >>> # You aren't expected to understand the code of this test.
    >>> # Check that the bodies of the functions are just return statements.
    >>> # If this errors, make sure you have removed the "***YOUR CODE HERE***".
    >>> import inspect, ast
    >>> [type(x).__name__ for x in ast.parse(inspect.getsource(summation_using_accumulate)).body[0].body]
    ['Expr', 'Return']
    """
    "*** YOUR CODE HERE ***"
    return accumulate(add, 0, n, term)

最后

贴截图,结束