作业 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、微信在线,随时响应!