python - 为什么这个 codewars 算法不能只使用函数?

标签 python algorithm

我已经尝试过使用可视化工具,但这个问题只返回 none。这是代码战问题:https://www.codewars.com/kata/calculating-with-functions/solutions/python

def zero(f = None): return 0 if not f else f(0)
def one(f = None): return 1 if not f else f(1)
def two(f = None): return 2 if not f else f(2)
def three(f = None): return 3 if not f else f(3)
def four(f = None): return 4 if not f else f(4)
def five(f = None): return 5 if not f else f(5)
def six(f = None): return 6 if not f else f(6)
def seven(f = None): return 7 if not f else f(7)
def eight(f = None): return 8 if not f else f(8)
def nine(f = None): return 9 if not f else f(9)

def plus(y): return lambda x: x+y
def minus(y): return lambda x: x-y
def times(y): return lambda  x: x*y
def divided_by(y): return lambda  x: x/y
four(plus(nine))

最佳答案

你需要调用nine();否则 nine 只是一个函数对象,不能与整数混合进行数字运算。

print(four(plus(nine())))

这个输出:

13

关于python - 为什么这个 codewars 算法不能只使用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51712467/

相关文章:

python - 通过Python + xmpppy 发送消息 : AttributeError in minimal example

python - Tkinter 销毁并退出会卡住 Mac 上的根窗口

algorithm - 线段树中的数据映射和惰性传播

algorithm - 仅使用递增、循环、赋值、零的关系操作

algorithm - 如何使用 mod 运算符包装数字

生成二进制矩阵的算法

algorithm - 在 rabin-karp rolling hash 中选择基数和模素数

python - 如果索引不存在,则为一行

python - 对多索引 DataFrame 上的不同行执行操作

python - 子字符串之间的对齐错误