首页 > 其他分享 >遇到了一个很棘手的问题...

遇到了一个很棘手的问题...

时间:2022-09-30 08:44:44浏览次数:67  
标签:function ... combiner return 遇到 cs61nay 棘手 lambda

遇到了一个很棘手的问题...

Q3: CS61 - NAY

Difficulty: ⭐⭐⭐

Part A: Implement cs61nay, which takes a two argument function combiner and positive integer n and returns a function.

The returned function then takes n arguments, one at a time, and computes combiner(...(combiner(combiner(arg1, arg2), arg3)...), arg_n). Notice combiner takes in two integers and returns one integer.

For example, the first doctest has the returned function f = cs61nay(lambda x, y: x * y, 3). Now when f is applied to three arguments, like f(2)(3)(4), it multiplies them together, 2*3*4 to get 24.

IMPORTANT: For this problem, the starter code template is just a suggestion. You are welcome to add/delete/modify the starter code template, or even write your own solution that doesn’t use the starter code at all.

HINT: For the n = 1 case, the returned function doesn't use combiner.

这道题初看简单,具体做起来并不容易...

以往做类似的题,我的思路大概是用一个值去乘以另一个函数值,直到边界情况,如经典的计算阶乘

def factorio(x):
    if x == 1:
        return 1;
    else:
        return x * factorio(x - 1)

image-20220929204343463

于是对于这题,我也想使用类似的思路,可是却找不出类似factorial(n - 1)这样的项,可能是因为我们开始有n个参数,接下来就应该有n-1个参数,难以简单的表示成一个函数的调用。

我的一个失败的尝试

def cs61nay(combiner, n):
    """ Return a function that takes n arguments,
    one at a time, and combines them using combiner.
    """
    if n == 1:
        return lambda x: x 
    else:
        return lambda x: lambda y: combiner(x, cs61nay(combiner, n - 1)(y))

n个参数,是这道题最大的特点,也许便使得曾经的思路不再适用——我们不能显式地给出一个返回值。

于是在这里需要用另一种方式思考。

对于

>>> f = cs61nay(lambda x, y: x * y, 3)
>>> f(2)(3)(4) # 2 * 3 * 4
24

我们无法直接实现2 * f(3)(4),也就是类似计算阶乘的思路

这里可以边计算边递归,即从前到后两两合并。这题要求我们按顺序计算,因此某种程度上也需要我们这样做。

首先计算头两个,那我们还得计算呀,这时关键的一步就是把计算结果继续作为参数

也就是把头两个元素x,y替换为combiner(x, y),也可以说是应用结合律首先计算头两个元素

这样的好处是减少了参数个数,也就是减小了问题规模,同时还规避了参数个数未知,无法显示写出表达式等问题

这也是递归,递归地顺序计算

代码:

def cs61nay(combiner, n):
    """ Return a function that takes n arguments,
    one at a time, and combines them using combiner.
    """
    if n == 1:
        return lambda x: x 
    else:
        return lambda x: lambda y: cs61nay(combiner, n - 1)(combiner(x, y))

另外在群里也看到了另一道题(据说是邱奇数),思路也十分多元,其中方法二与本题有异曲同工之妙

Homework 2 | CS 61A Spring 2021

image-20220929214742515

# 方法4
def make_repeater(func, n):
    """Return the function that computes the nth application of func. """
    if n == 0:
        return zero(func)
    return successor(lambda f: make_repeater(f, n - 1))(func)
    

def zero(f):
    return lambda x: x

def successor(n):
    return lambda f: lambda x: f(n(f)(x))

标签:function,...,combiner,return,遇到,cs61nay,棘手,lambda
From: https://www.cnblogs.com/tsrigo/p/16743698.html

相关文章

  • switch... case用法
    switch....case用于判断一个变量与一系列中的某个值是否相等,每个值为一个分支。这也是一个比if...elseif更好的选择。基本语法如下switch(表达式){case值:语句;break......
  • 项目架构中遇到需考虑的问题
    我在项目实践中遇到的困扰了我很久的问题:对于系统架构技术方面,其实项目里面有如此的概念,但是具体还并未完全实现,有待改进(内部解决,非紧急):主要有三个方面:1)缓存问题 a.系统......
  • 教师节,感恩人生路上遇到的每一位老师!
    十年树木,百年树人。今天是2021年9月10日,农历八月初四,我们迎来了第三十七个教师节。教师,是人类灵魂的工程师,更是无比光荣自豪的职业。但“教师”不仅有荣光,背后更多的是诸多......
  • svn使用遇到的问题总结
    1.提示文件冲突  解决办法:1.CODE.txt中保留需要的代码,其余的删掉。2.在冲突目录下,选中文件--右键菜单—TortoiseSVN—Resolved(解决)。会列出冲突的文件列表,如果确认......
  • lsyncd使用中遇到的问题
    问题1:执行命令:/etc/init.d/lsyncdstatus显示:lsyncd已死,但pid文件仍存于是查看日志文件:cat/var/log/lsyncd/lsyncd.log日志里显示:Error:Terminatingsinceout......
  • 遇到一个bug,组件不更新内容
    解决办法当v-if的值发生变化时,组件都会被重新渲染一遍。因此,利用v-if指令的特性,可以达到强制刷新组件的目的。<template><compv-if="update"></comp><button......
  • About....
    65级的,学啥啥都不会的。##谷:529262/SoN3ri##Q:2820760598##舟:某个boss的英文名##steam:dyRR##ATcoder.CF:SoN3ri##屑站:-dyR-##待补充..>说得不对,但是......
  • [iOS]遇到了一个问题:“XXXX”中无法使用Apple Pay ,检查此应用的设置并确定其设计可使
     在钥匙串里查看,发现当时申请的MerchantIDXXXX证书过期1.到apple开发者: https://developer.apple.com/account/#/overview/ 查看 Certificates,Identifiers......
  • 坑爹!Quartz 重复调度问题,你遇到过么?
    作者:Lavender来源:https://segmentfault.com/a/11900000154922601.引子公司前期改用quartz做任务调度,一日的调度量均在两百万次以上。随着调度量的增加,突然开始出现job......
  • 女同桌找我要表情包,还好我会Python,分分钟给她下载几十个G...
    emmm~起因呢,这昨晚女同桌跟我说电脑有点卡,喊我去宿舍给她装个新系统,装系统就装系统吧,结果又说新系统表情包都没保存~我当时就有点生气,真当我是万能的呢?于是我直接就用Py......