【Kata Daily 190904】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_byin 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, not2.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(函数计算)的更多相关文章
- 从函数计算架构看 Serverless 的演进与思考
作者 | 杨皓然 阿里巴巴高级技术专家 导读:云计算之所以能够成为 DT 时代颠覆性力量,是因为其本质是打破传统架构模式.降低成本并简化体系结构,用全新的思维更好的满足了用户需求.而无服务器计算(S ...
- 开发函数计算的正确姿势 —— 使用 ROS 进行资源编排
前言 首先介绍下在本文出现的几个比较重要的概念: 函数计算(Function Compute): 函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传.函数计算 ...
- C++习题 虚函数-计算图形面积
C++习题 虚函数-计算图形面积 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 122 Solved: 86 [cid=1143&pid=6 ...
- 学习如何看懂SQL Server执行计划(二)——函数计算篇
二.函数计算部分 --------------------标量聚合--------------------/* 标量聚合-主要在聚合函数操作中产生 计算标量:根据行中的现有值计算出一个新值 流聚合:在 ...
- 开发函数计算的正确姿势 —— 使用 Fun Local 本地运行与调试
前言 首先介绍下在本文出现的几个比较重要的概念: 函数计算(Function Compute): 函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传.函数计算 ...
- 手把手教您将 libreoffice 移植到函数计算平台
LibreOffice 是由文档基金会开发的自由及开放源代码的办公室套件.LibreOffice 套件包含文字处理器.电子表格.演示文稿程序.矢量图形编辑器和图表工具.数据库管理程序及创建和编辑数学公 ...
- 给出两个单词word1和word2,写一个函数计算出将word1 转换为word2的最少操作次数。
问题: 给出两个单词word1和word2,写一个函数计算出将word1 转换为word2的最少操作次数. 你总共三种操作方法: 1.插入一个字符 2.删除一个字符 3.替换一个字符 格式: 输入行输 ...
- 函数计算 Python 连接 SQL Server 小结
python 连接数据库通常要安装第三方模块,连接 MS SQL Server 需要安装 pymssql .由于 pymsql 依赖于 FreeTDS,对于先于 2.1.3 版本的 pymssql,需 ...
- sql server2005版本中,len函数计算了字符串末尾的空格
sql server2005版本中,len函数计算了字符串末尾的空格的长度,以下是测试脚本: print @@version declare @v varchar(max) set @v = 'hp, ...
随机推荐
- puts()和gets()函数
puts()和gets()函数 1. puts()函数 puts()函数用来向标准输出设备(屏幕)写字符串并换行, 其调用格式为: puts(s); 其中s为字符串变量(字符串数组名或字符串指针). ...
- 【题解】[SHOI2007]善意的投票 / [JLOI2010]冠军调查
Link \(\text{Solution:}\) 我们令源点和汇点分别为睡觉和不睡觉这两种互斥的决策点.把小朋友看成点,问题转化为最小割. 每一个小朋友对自己的意愿指向的汇点/源点.容量为\(1.\ ...
- Docker镜像仓库Harbor部署
一.Harbor组件 组件 功能 harbor-adminserver 配置管理中心 harbor-db Mysql数据库 harbor-jobservice 负责镜像复制 harbor-log 记录 ...
- PHP 下载七牛云的sdk
1,语法 composer require qiniu/php-sdk 2,出现以下图片内容就是下载七牛云的sdk成功
- html ul li 自定义宽
1. ul里面的样式 2. ul li 里面的样式
- 实验 4:Open vSwitch 实验——Mininet 中使用 OVS 命令
一.实验目的 Mininet 安装之后,会连带安装 Open vSwitch,可以直接通过 Python 脚本调用Open vSwitch 命令,从而直接控制 Open vSwitch,通过实验了解调 ...
- activiti 流程部署 保存流程图到数据库 保存二进制图片 存储失败
activiti 流程部署 保存流程图到数据库 保存二进制图片 存储失败 具体错误如下 具体 junit测试 结果 :提示如下: 解决方法: 数据库版本不同 无法保存二进制文件到数据库表中!5.5. ...
- Spring mvc文件上传实现
Spring mvc文件上传实现 jsp页面客户端表单编写 三个要素: 1.表单项type="file" 2.表单的提交方式:post 3.表单的enctype属性是多部分表单形式 ...
- FDDB人脸检测数据集 生成ROC曲线
看了好多博客,踩了很多坑,终于把FDDB数据集的ROC曲线绘制出来了.记录一下. 环境:ubuntu18.04 1.数据集准备 去FDDB官网:http://vis-www.cs.umass.edu/ ...
- 反射(Reflection)
Java学习笔记--反射(Reflection) 关于反射 能够分析类能力的程序称之为反射(Reflection) 反射机制可以用来: 在运行时分析类的能力 在运行时检查对象,例如:编写一个适合所有类 ...