原题:

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、

 

【Kata Daily 190904】Calculating with Functions(函数计算)的更多相关文章

  1. 从函数计算架构看 Serverless 的演进与思考

    作者 | 杨皓然  阿里巴巴高级技术专家 导读:云计算之所以能够成为 DT 时代颠覆性力量,是因为其本质是打破传统架构模式.降低成本并简化体系结构,用全新的思维更好的满足了用户需求.而无服务器计算(S ...

  2. 开发函数计算的正确姿势 —— 使用 ROS 进行资源编排

    前言 首先介绍下在本文出现的几个比较重要的概念: 函数计算(Function Compute): 函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传.函数计算 ...

  3. C++习题 虚函数-计算图形面积

    C++习题 虚函数-计算图形面积 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 122  Solved: 86 [cid=1143&pid=6 ...

  4. 学习如何看懂SQL Server执行计划(二)——函数计算篇

    二.函数计算部分 --------------------标量聚合--------------------/* 标量聚合-主要在聚合函数操作中产生 计算标量:根据行中的现有值计算出一个新值 流聚合:在 ...

  5. 开发函数计算的正确姿势 —— 使用 Fun Local 本地运行与调试

    前言 首先介绍下在本文出现的几个比较重要的概念: 函数计算(Function Compute): 函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传.函数计算 ...

  6. 手把手教您将 libreoffice 移植到函数计算平台

    LibreOffice 是由文档基金会开发的自由及开放源代码的办公室套件.LibreOffice 套件包含文字处理器.电子表格.演示文稿程序.矢量图形编辑器和图表工具.数据库管理程序及创建和编辑数学公 ...

  7. 给出两个单词word1和word2,写一个函数计算出将word1 转换为word2的最少操作次数。

    问题: 给出两个单词word1和word2,写一个函数计算出将word1 转换为word2的最少操作次数. 你总共三种操作方法: 1.插入一个字符 2.删除一个字符 3.替换一个字符 格式: 输入行输 ...

  8. 函数计算 Python 连接 SQL Server 小结

    python 连接数据库通常要安装第三方模块,连接 MS SQL Server 需要安装 pymssql .由于 pymsql 依赖于 FreeTDS,对于先于 2.1.3 版本的 pymssql,需 ...

  9. sql server2005版本中,len函数计算了字符串末尾的空格

    sql server2005版本中,len函数计算了字符串末尾的空格的长度,以下是测试脚本: print @@version declare @v varchar(max) set @v = 'hp, ...

随机推荐

  1. c++中的GetModuleFileName函数的用法以及作用

    参考: 1. http://blog.sina.com.cn/s/blog_b078a1cb0101fw48.html 2. https://www.cnblogs.com/Satu/p/820393 ...

  2. c#类(class)

    类 类的定义是以关键字class开始的,后面跟类的名称,类的主题包含一个花括号里,下面是类定义的一般格式. <access specifier> class class_name { // ...

  3. 第2天 | 12天搞定Python,运行环境(超详细步骤)

    倘若有人告诉你,他在学习Python编程,却没有安装运行环境,那你赶紧叫他滚,并离他远点,因为他在欺骗你的感情. 没有安装运行环境,程序根本无法跑起来,对错不能知根知底,试问是在学编程,还是在跟空气对 ...

  4. git 上传文件到 gitee 码云远程仓库(强制上传)

    1.先git init 会出现一个.git的文件夹,有些人可能是隐藏了,工具哪里打开就行了 2.将当前的数据上传到码云,看清楚奥,是当前.git add ./ 这是代表当前的意思 3.将上传的数据备注 ...

  5. 教你两步快速使用华为HMS沙盒(沙箱)测试

    沙盒(沙箱)测试允许在开发者在接入华为应用内支付IAP联调过程中无需真实支付即可完成端到端的测试. 第一步:添加测试账号 在AppGallery Connect中的"用户与访问"添 ...

  6. Fedora version history --- kernel version

    Fedora version history https://en.wikipedia.org/wiki/Fedora_version_history     Version (Code name)[ ...

  7. Linux Centos7 安装Docker-CE

    先确保yum 是最新版本 执行: sudo yum update 添加docker源地址 sudo yum-config-manager --add-repo https://download.doc ...

  8. MeteoInfoLab脚本示例:风场矢量图

    读取风场U/V变量数据,可以从U/V计算出风速:speed = sqrt(u*u+v*v).quiverm函数用来绘制风场矢量图,参数中包括U/V变量,如果要绘制彩色风场还需要第三个变量,这里是风速s ...

  9. Flask之WTF

    Flask-WTF是什么? 是一个关于表单的扩展库,可以自动生成表单的HTML代码和验证提交的表单数据,并且提供跨站请求伪造(Cross-Site Request Forgery)保护的功能,使用非常 ...

  10. day27 Pyhton 面向对象02 类和对象的命名空间

    一.内容回顾 类:具有相同属性和方法的一类事务 # 描述一类事务轮廓的一个机制 #商品/用户/店铺 对象/实例: 对象(实例)就是类的实例化 # 对象就是类的一个具体的表现 #某一件特定的商品/某个人 ...