Description:
This time we want to write calculations using functions and get the results. Let's have a look at some examples:
JavaScript:
seven(times(five())); // must return 35
four(plus(nine())); // must return 13
eight(minus(three())); // must return 5
six(dividedBy(two())); // must return 3
Ruby:
seven(times(five)) # must return 35
four(plus(nine)) # must return 13
eight(minus(three)) # must return 5
six(divided_by(two)) # must return 3
Requirements:
There must be a function for each number from 0 ("zero") to 9 ("nine")
There must be a function for each of the following mathematical operations: plus, minus, times, dividedBy (divided_by in Ruby)
Each calculation consist of exactly one operation and two numbers
The most outer function represents the left operand, the most inner function represents the right operand
This time we want to write calculations using functions and get the results. Let's have a look at some examples:
JavaScript:
seven(times(five())); // must return 35
four(plus(nine())); // must return 13
eight(minus(three())); // must return 5
six(dividedBy(two())); // must return 3
Ruby:
seven(times(five)) # must return 35
four(plus(nine)) # must return 13
eight(minus(three)) # must return 5
six(divided_by(two)) # must return 3
Requirements:
There must be a function for each number from 0 ("zero") to 9 ("nine")
There must be a function for each of the following mathematical operations: plus, minus, times, dividedBy (divided_by in Ruby)
Each calculation consist of exactly one operation and two numbers
The most outer function represents the left operand, the most inner function represents the right operand
Divison should be integer division, e.g eight(dividedBy(three()))/eight(divided_by(three)) should return 2, not 2.666666...
自己
'''
解题思路:
1、使用三元表达式,查看该数是否有参
2、如果无参数的话则返回本身数,反之创建表达式并使用eval执行
'''
def zero(operation=None):return int(eval('0%s' % operation)) if operation else 0
def one(operation=None):return int(eval('1%s' % operation)) if operation else 1
def two(operation=None):return int(eval('2%s' % operation)) if operation else 2
def three(operation=None):return int(eval('3%s' % operation)) if operation else 3
def four(operation=None):return int(eval('4%s' % operation)) if operation else 4
def five(operation=None):return int(eval('5%s' % operation)) if operation else 5
def six(operation=None):return int(eval('6%s' % operation)) if operation else 6
def seven(operation=None):return int(eval('7%s' % operation)) if operation else 7
def eight(operation=None):return int(eval('8%s' % operation)) if operation else 8
def nine(operation=None):return int(eval('9%s' % operation)) if operation else 9
def plus(num):return '+%s' % num
def minus(num):return '-%s' % num
def times(num):return '*%s' % num
def divided_by(num):return '/%s' % num
他人
'''
值得学习的地方:
1、三元表达式,返回操作符的函数,并将当前参数传入
2、操作符函数使用了简洁的lambda表达式
3、lambda的写法还是挺难理解的,这里应该从里往外看,比如:eight(divided_by(four()))
应该先算出four()这个式子,然后再看divided_by(four())->lambda x: x/4 看得出来这个时候lambda还没有被调用
最后eight(operation)的时候,调用了当前的lambda 等效于 执行了 divided_by(4)(8)
'''
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