CS61:Homework 2: Higher Order Functions and Recursion(伯克利 Python 作业讲解)

作业 2:高阶函数和递归

你好,我是悦创。

所需文件:https://download.csdn.net/download/qq_33254766/20091964

必答题

重要功能

几个 doctests 引用了这些函数:

from operator import add, mul, sub

square = lambda x: x * x

identity = lambda x: x

triple = lambda x: 3 * x

increment = lambda x: x + 1

高阶函数

Q1:制作中继器

实现函数,make_repeater 以便 make_repeater(func, n)(x) 返回 func(func(...func(x)...)),其中 func 应用 n 次数。也就是说, make_repeater(func, n) 返回另一个可以应用于另一个参数的函数。例如, make_repeater(square, 3)(42) 评估为 square(square(square(42)))

解析题目:
在这里插入图片描述
在这里插入图片描述

Q1:代码
from operator import add, mul, sub

square = lambda x: x * x

identity = lambda x: x

triple = lambda x: 3 * x

increment = lambda x: x + 1

def make_repeater(func, n):
	"""Return the function that computes the nth application of func.
	返回计算 func 的第 n 个应用程序的函数。

	# >>> add_three = make_repeater(increment, 3)
	>>> add_three = make_repeater(increment, 3)(5)
	>>> add_three(5)
	(increment(3)-1) + x ---> (3)-1 =  3-1 = n-1
	8
	>>> make_repeater(triple, 5)(1) # 3 * 3 * 3 * 3 * 3 * 1
	triple(5) * 1 ----> func(n) * 1 ---> 从这里知道是执行 lambda (func)
	243
	>>> make_repeater(square, 2)(5) # square(square(5))
	n = 2 ---> 出现两次 square
	625
	>>> make_repeater(square, 4)(5) # square(square(square(square(5))))
	n = 4 ---> 出现四次 square ---> square 出现的原因是执行 make_repeater 才出现的。

	152587890625
	>>> make_repeater(square, 0)(5) # Yes, it makes sense to apply the function zero times!
	5
	"""
	"*** YOUR CODE HERE ***"
	def final(number):
		if n == 0:
			return number
		else:
			return func(make_repeater(func, n-1)(number))
	return final

if __name__ == "__main__":
	r = make_repeater(increment, 3)(5)
	print(r)
from operator import add, mul, sub

square = lambda x: x * x

identity = lambda x: x

triple = lambda x: 3 * x

increment = lambda x: x + 1

def make_repeater(func, n):
	def final(number):
		if n == 0:
			return number
		else:
			func(make_repeater(func, n-1)(number))
	return final

# 1. 调用函数
# 2. 返回值
# 3. 值 >>> 加括号 (100()) --- Python 能加括号 >>> 函数
# final(1010)



# from operator import add, mul, sub

# square = lambda x: x * x

# identity = lambda x: x

# triple = lambda x: 3 * x

# increment = lambda x: x + 1

# def make_repeater(func, n):
# 	def final(number):
# 		if n == 0:
# 			return number
# 		else:
# 			return func(make_repeater(func, n-1)(number))
# 	return final

# if __name__ == "__main__":
# 	r = make_repeater(increment, 3)(5)
# 	print(r)
视频讲解:

Python中继器实现题目讲解

练习题目
"""
实现函数,aiyc_func 以便 aiyc_func(link, name)(age) 	输出示例:
"""

def aiyc_func(link, name):
	pass

"""
>>>aiyc_func("www.aiyc.top", "aiyuechuang")(19)
>>>姓名:aiyuechuang, 年龄:19, Website:www.aiyc.top

>>>aiyc_func("www.blovey.art", "LiYueRan")(0)
>>>年龄输入异常
"""
答案
"""
实现函数,aiyc_func 以便 aiyc_func(link, name)(age)返回 输出示例:
"""

def aiyc_func(link, name):
	def age(age):
		if age == 0:
			return "年龄输入异常!!!"
		else:
			tup = (link, name, age)
			return f"姓名:{tup[1]}, 年龄:{tup[2]}, Website:{tup[0]}"
			# print(f"姓名:{name}, 年龄:{age}, Website:{link}")
	return age

if __name__ == '__main__':
	r = aiyc_func("www.aiyc.top", "aiyuechuang")(19)
	print(r)
	# print(f"姓名:{r[1]}, 年龄:{r[2]}, Website:{r[0]}")
"""
format
>>>aiyc_func("www.aiyc.top", "aiyuechuang")(19)
>>>姓名:aiyuechuang, 年龄:19, Website:www.aiyc.top

>>>aiyc_func("www.blovey.art", "LiYueRan")(0)
>>>年龄输入异常
"""

# def aiyc_func(link, name):
# 	def age(age):
# 		if age == 0:
# 			return "年龄输入异常"
# 		else:
# 			return (link, name, age)
# 	return age

# if __name__ == '__main__':
# 	r = aiyc_func("www.aiyc.top", "aiyuechuang")(19)
# 	print(r)
# 	print(f"姓名:{r[1]}, 年龄:{r[2]}, Website:{r[0]}")

AI悦创·推出辅导班啦,包括「Python 语言辅导班、C++辅导班、算法/数据结构辅导班」,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。QQ、微信在线,随时响应!

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI悦创|编程1v1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值