【Kata Daily 2】Calculating with Functions

原题:

This time we want to write calculations using functions and get the results. Let's have a look at some examples:

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 and Python)
  • 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. For example, this should return 2, not 2.666666...:
eight(divided_by(three()))

-----------------------------------------------------------------------------------------------------------------------------
题目的要求就是利用函数的方式进行加减乘除,如:
seven(times(five())) ------->相当于:7*5=35

解题过程

这道理不会做 o(╥﹏╥)o。


以下是一位网友的解决思路:
def zero(a='0'):
    if a != '0':
        if a[0]=='+':
            return 0+int(a[1])
        if a[0]=='-':
            return 0-int(a[1])
        if a[0]=='*':
            return 0*int(a[1])
        if a[0]=='/':
            return int(0/int(a[1]))
    else:
        return '0'
def one(a='1'):
    if a != '1':
        if a[0]=='+':
            return 1+int(a[1])
        if a[0]=='-':
            return 1-int(a[1])
        if a[0]=='*':
            return 1*int(a[1])
        if a[0]=='/':
            return int(1/int(a[1]))
    else:
        return '1'
def two(a='2'):
    if a != '2':
        if a[0]=='+':
            return 2+int(a[1])
        if a[0]=='-':
            return 2-int(a[1])
        if a[0]=='*':
            return 2*int(a[1])
        if a[0]=='/':
            return int(2/int(a[1]))
    else:
        return '2'
def three(a='3'):
    if a != '3':
        if a[0]=='+':
            return 3+int(a[1])
        if a[0]=='-':
            return 3-int(a[1])
        if a[0]=='*':
            return 3*int(a[1])
        if a[0]=='/':
            return int(3/int(a[1]))
    else:
        return '3'
def four(a='4'):
    if a != '4':
        if a[0]=='+':
            return 4+int(a[1])
        if a[0]=='-':
            return 4-int(a[1])
        if a[0]=='*':
            return 4*int(a[1])
        if a[0]=='/':
            return int(4/int(a[1]))
    else:
        return '4'
def five(a='5'):
    if a != '5':
        if a[0]=='+':
            return 5+int(a[1])
        if a[0]=='-':
            return 5-int(a[1])
        if a[0]=='*':
            return 5*int(a[1])
        if a[0]=='/':
            return int(5/int(a[1]))
    else:
        return '5'
def six(a='6'):
    if a != '6':
        if a[0]=='+':
            return 6+int(a[1])
        if a[0]=='-':
            return 6-int(a[1])
        if a[0]=='*':
            return 6*int(a[1])
        if a[0]=='/':
            return int(6/int(a[1]))
    else:
        return '6'
def seven(a='7'):
    if a != '7':
        if a[0]=='+':
            return 7+int(a[1])
        if a[0]=='-':
            return 7-int(a[1])
        if a[0]=='*':
            return 7*int(a[1])
        if a[0]=='/':
            return int(7/int(a[1]))
    else:
        return '7'
def eight(a='8'):
    if a != '8':
        if a[0]=='+':
            return 8+int(a[1])
        if a[0]=='-':
            return 8-int(a[1])
        if a[0]=='*':
            return 8*int(a[1])
        if a[0]=='/':
            return int(8/int(a[1]))
    else:
        return '8'
def nine(a='9'):
    if a != '9':
        if a[0]=='+':
            return 9+int(a[1])
        if a[0]=='-':
            return 9-int(a[1])
        if a[0]=='*':
            return 9*int(a[1])
        if a[0]=='/':
            return int(9/int(a[1]))
    else:
        return '9'
 
def plus(a):
    return '+'+a
def minus(a):
    return '-'+a
def times(a):
    return '*'+a
def divided_by(a):
    return '/'+a

这位网友的思路很好理解:

  数字函数的传值默认为本身,且默认传回自身的值。如果参数值为其他,则根据参数值判断算法类型符合哪种算法,最后进行相加减。  算法函数只要是将算法的符号和值进行字符串合并,用于在数字函数中进行判断。如:seven(times(five()))

  第一步:先执行five()函数,由于没有参数值,因此使用默认的参数值,返回自身,即:5。

  第二步:处理times(5)函数,将5和算法符号进行合并,即:符号'*'和5合并,返回字符串"*5"

  第三步:处理seven("*5")函数,由于数字函数有参数,因此执行else里面的内容,根据a[0]即字符串的第一个字符来判断符合哪种算法,再将值(即a[1])进行运算,即:5*7=35

优质解答:

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: int(x/y)

我们来探讨一下这种算法的思路:

  数字函数中,先判断是否为None,为None则返回自身。如果不为None则执行函数f。算术函数返回一个匿名函数。还是选择例子:seven(times(five()))

  第一步:先处理five()函数,即返回自身5。times(5)

  第二步:处理times(5),结果为返回一个匿名函数,函数内部的y值为5。

  第三步:处理f函数,函数f为算术函数处理结果,即返回的匿名函数。f(7)相当于执行匿名函数:lambda 7:7*5处理结果为35。

知识点:

  1、使用了函数默认参数值:

    def one(a=1):  即默认参数a默认传值为1,可自行定义a的参数值。

  2、使用了匿名函数lambda

    lambda 的格式为:lambda x, y: x + y。x和y是需要传入的形参。x+y是函数的处理结果。

  3、使用了闭包

    算术函数的返回值是一个函数,即将先一步处理的结果(y=5)和匿名函数一起保存了下来。而seven(f),就是将这个包赋给了f,f变成了含有y值的匿名函数。

  4、

 

猜你喜欢

转载自www.cnblogs.com/bcaixl/p/11460452.html