首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用约简技术实现Python中的高阶函数

用约简技术实现Python中的高阶函数
EN

Code Review用户
提问于 2014-07-11 11:12:12
回答 2查看 903关注 0票数 1

对于下面的查询,请显示和积都是一个更一般函数的实例,称为累计,并具有以下签名: def累计(组合器、开始、n、术语):“返回序列中第一个n项组合的结果。”“*这里的代码以相同的参数项作为参数,n作为求和和积,连同一个组合器函数(由两个参数组成),该函数指定如何将当前项与前面项的累积组合,以及指定用于开始积累的基值的起始值。实现累积,并说明如何将求和和积定义为简单的累积调用: def summation_using_accumulate(n,term):“”是使用累加的求和实现。>>> summation_using_accumulate(4,square) 30“”*您在这里的代码*“def product_using_accumulate(n,term):”是使用累积的产品的实现。>>> product_using_accumulate(4,square) 576“”*您的代码在这里*“

以下是解决办法:

代码语言:javascript
复制
    from operator import mul, add


def accumulate(combiner, start, n, f):
    """Return the result of combining the first n terms in a sequence."""

    total = start           #Result of summation gets stored here
    i = 1                   #Initial value of sequence
    while i <= n:
        total = combiner(total, f(i))
        i = i + 1
    return total


def summation_using_accumulate(n, f):
    """An implementation of summation using accumulate.

    >>> summation_using_accumulate(4, square)
    30
    """
    return accumulate(add, 0, n, f)



def product_using_accumulate(n, f):
    """An implementation of product using accumulate.

    >>> product_using_accumulate(4, square)
    576
    """
    return accumulate(mul, 1, n, f)


def square(x):
    return mul(x, x)

print("product_using_accumulate: ",product_using_accumulate(4, square))
print("summation_using_accumulate: ",summation_using_accumulate(4, square))
print(accumulate(add, 0, 4, square)) 
print(accumulate(mul, 1, 4, square))

我已经测试过这段代码,看上去不错。

我的问题:

  1. 解决方案在任何方面看上去都不正确吗?
  2. 对命名惯例有任何反馈意见吗?
  3. 对编码风格有任何反馈吗?
EN

回答 2

Code Review用户

回答已采纳

发布于 2014-07-11 13:12:41

以下是我认为你们被引导到的实施方案:

代码语言:javascript
复制
##from functools import reduce # if Python 3.x
from operator import add, mul

def accumulate(combiner, start, n, f):
    """Return the result of combining the first n terms in a sequence."""
##    return reduce(combiner, (f(i+1) for i in range(n)), start) # <- built-in version
    total = start
    for i in range(n):
        total = combiner(total, f(i+1))
    return total 

def summation_using_accumulate(n, f):
    """An implementation of summation using accumulate.

    >>> summation_using_accumulate(4, square)
    30
    """
    return accumulate(add, 0, n, f)

def product_using_accumulate(n, f):
    """An implementation of product using accumulate.

    >>> product_using_accumulate(4, square)
    576
    """
    return accumulate(mul, 1, n, f)

def square(x):
    return mul(x, x)

所以你才能

说明如何将求和和积定义为简单的累积调用。

也就是说,只需这样做,就能取得所需的结果:

代码语言:javascript
复制
>>> product_using_accumulate(4, square)
576
>>> summation_using_accumulate(4, square)
30

另外,请注意forrange的使用,这两种方法在while循环中手动递增值更容易,也更不容易出错。

所以你的具体问题的答案是:

  1. 不,accumulateproduct_using_accumulatesummation_using_accumulate都错了,但你现在已经修好了;
  2. 不;现在您已经删除了Currentterm (应该是current_term -根据样式指南,变量名是lowercase_with_underscores);以及
  3. 是的,您需要更多的空间,例如return accumulate(add,0,n,f)应该是return accumulate(add, 0, n, f)
票数 4
EN

Code Review用户

发布于 2014-07-11 11:28:29

  1. 如果作业问题以“显示那个”开头,我希望答案包含一个证据(或至少一个论点),而不仅仅是一个实现。
  2. 你误解了问题陈述。它说start“指定要使用什么值来开始积累”,但是在您的实现中,start是序列中贡献积累的第一个项的索引。这两个测试用例都有start=1f(1)=1,因此它们无法检测您的错误。
  3. summation_using_accumulateproduct_using_accumulate的实现实际上并不使用accumulate
票数 5
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/56744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档