【Python CheckiO 题解】SP
题目描述
【Speech Module】:输入一个数字,将其转换成英文表达形式,字符串中的所有单词必须以一个空格字符分隔。
【输入】:一个数字(int)
【输出】:代表数字的英文字符串(str)
【前提】:0 < number < 1000
【范例】:
checkio(4)=='four'
checkio(143)=='one hundred forty three'
checkio(12)=='twelve'
checkio(101)=='one hundred one'
checkio(212)=='two hundred twelve'
checkio(40)=='forty'
解题思路
将输入的数字分为四种情况:
0 ≤ 数字 < 10:直接返回 FIRST_TEN[number - 1] 即可;
10 ≤ 数字 < 20:直接返回 SECOND_TEN[number - 10] 即可;
20 ≤ 数字 < 100:如果是 10 的倍数(number % 10 == 0),直接返回 OTHER_TENS[number // 10 - 2] 即可,如果不是 10 的倍数(number % 10 != 0),那么就要将其十位和个位连起来,即:OTHER_TENS[number // 10 - 2] + ' ' + FIRST_TEN[number % 10 - 1];
100 ≤ 数字 < 999:与第三种情况类似,实现判断是不是 100 的倍数,如果是则直接返回 FIRST_TEN[number // 100 - 1] + ' ' + HUNDRED,如果不是,则判断其除以 100 的余数是不是 10 的倍数,也就是后两位是不是 10 的倍数,即重复第三步即可。
代码实现
FIRST_TEN = ["one", "two", "three", "four", "five", "six", "seven",
"eight", "nine"]
SECOND_TEN = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen"]
OTHER_TENS = ["twenty", "thirty", "forty", "fifty", "sixty", "seventy",
"eighty", "ninety"]
HUNDRED = "hundred"
def checkio(number):
if number >= 0 and number < 10:
english = FIRST_TEN[number - 1]
return english
elif number >= 10 and number < 20:
english = SECOND_TEN[number - 10]
return english
elif number >= 20 and number < 100:
if number % 10 == 0:
english = OTHER_TENS[number // 10 - 2]
return english
else:
english = OTHER_TENS[number // 10 - 2] + ' ' + FIRST_TEN[number % 10 - 1]
return english
else:
if number % 100 == 0:
english = FIRST_TEN[number // 100 - 1] + ' ' + HUNDRED
return english
else:
english = FIRST_TEN[number // 100 - 1] + ' ' + HUNDRED + ' ' + checkio(number % 100)
return english
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio(4) == 'four', "1st example"
assert checkio(133) == 'one hundred thirty three', "2nd example"
assert checkio(12) == 'twelve', "3rd example"
assert checkio(101) == 'one hundred one', "4th example"
assert checkio(212) == 'two hundred twelve', "5th example"
assert checkio(40) == 'forty', "6th example"
assert not checkio(212).endswith(' '), "Don't forget strip whitespaces at the end of string"
print('Done! Go and Check it!')
大神解答
大神解答 NO.1
def checkio(number):
"""Convert Arabic numerals to English
"""
num1 = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
num11 = [
"ten",
"eleven ",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen",
]
num2 = [
"",
"",
"twenty ",
"thirty ",
"forty ",
"fifty ",
"sixty ",
"seventy ",
"eighty ",
"ninety ",
]
num3 = [
"",
"one hundred ",
"two hundred ",
"three hundred ",
"four hundred ",
"five hundred ",
"six hundred ",
"seven hundred ",
"eight hundred ",
"nine hundred ",
]
num4 = ["", "one thousand"]
if number < 10 or number > 19:
a = str(number)
if number < 10:
a = "000" + a
elif number < 100:
a = "00" + a
elif number < 1000:
a = "0" + a
if a[2] == "1":
spel = num4[int(a[0])] + num3[int(a[1])] + num11[int(a[3])]
else:
spel = num4[int(a[0])] + num3[int(a[1])] + num2[int(a[2])] + num1[int(a[3])]
else:
spel = num11[(number - 10)]
if spel[-1] == " ":
spel = spel[0:-1]
return spel
大神解答 NO.2
FIRST_TEN = ["", "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine"]
SECOND_TEN = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen"]
OTHER_TENS = ["twenty", "thirty", "forty", "fifty", "sixty", "seventy",
"eighty", "ninety"]
def checkio(number):
result = ''
if number >= 100:
result += f'{FIRST_TEN[int(number/100)]} hundred '
number %= 100
if number >= 20:
result += f'{OTHER_TENS[int(number/10)-2]} {FIRST_TEN[number % 10]}'
elif number >= 10:
result += SECOND_TEN[number % 10]
else:
result += FIRST_TEN[number]
return result.rstrip()
外汇返佣 NO.3
FIRST_TEN = ["one", "two", "three", "four", "five", "six", "seven",
"eight", "nine"]
SECOND_TEN = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen"]
OTHER_TENS = ["twenty", "thirty", "forty", "fifty", "sixty", "seventy",
"eighty", "ninety"]
HUNDRED = "hundred"
def checkio(number):
d = {f'x{i}': number%10**i//10**(i-1) for i in range(1, 4)}
res = ''
if d['x3']:
res += ' {} {}'.format(FIRST_TEN[d['x3'] - 1], HUNDRED)
if d['x2'] == 1:
res += ' {}'.format(SECOND_TEN[d['x1']])
elif d['x2']:
res += ' {}'.format(OTHER_TENS[d['x2'] - 2])
if d['x1'] and not (d['x2'] == 1):
res += ' {}'.format(FIRST_TEN[d['x1'] - 1])
return res.strip()
大神解答 NO.4
FIRST_TEN = ["one", "two", "three", "four", "five", "six", "seven",
"eight", "nine"]
SECOND_TEN = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen"]
OTHER_TENS = ["twenty", "thirty", "forty", "fifty", "sixty", "seventy",
"eighty", "ninety"]
HUNDRED = "hundred"
def checkio(number):
ret =''
h = number//100
t = (number - h*100)//10
f = number - h*100 - t*10
if h > 0:
ret += FIRST_TEN[h-1] + ' ' + HUNDRED + ' '
if t == 0 and f > 0:
ret += FIRST_TEN[f-1] + ' '
elif t == 1:
ret += SECOND_TEN[f] + ' '
elif t>1:
ret += OTHER_TENS[t-2] + ' '
if f>0:
ret += FIRST_TEN[f-1] + ' '
return ret[:-1]
【Python CheckiO 题解】SP的更多相关文章
- 一人一python挑战题解
题目id: 1 just print a+b give you two var a and b, print the value of a+b, just do it!! print a+b 题目id ...
- python chanllenge题解
网址:chanllenge 修改url最后的html的前缀为答案,就可以过关. 页面上很多只有一幅图片,实际上题目描述全在页面源码中. 然后推荐一个在线代码运行的网站 ideone 查看所有源码:ht ...
- 2019-06-03 校内python模拟题解(所有非原题)
一起来女装吧 本题改编自USACO(USA Computing Olympiad) 1.1节的第一题 (感谢lsy同学对本题题面的贡献) 直接计算就好了 chr:将ASCII码转成字符 ord:字符对 ...
- LeetCode python实现题解(持续更新)
目录 LeetCode Python实现算法简介 0001 两数之和 0002 两数相加 0003 无重复字符的最长子串 0004 寻找两个有序数组的中位数 0005 最长回文子串 0006 Z字型变 ...
- The Python Challenge 题解
仔细阅读,图画下面的提示(网页的 title 也是重要的提示信息,至少告诉你考察的对象是什么) 1. 238 >> 2**38 274877906944L 根据提示,在 URL 地址处,0 ...
- 利用python代码获取文件特定的内容,并保存为文档
说明:有段时间需要读取上百个文件的单点能(sp),就写了下面的代码(计算化学狗努力转行中^-^) import os.path import re # 1 遍历指定目录,显示目录下的所有文件名 def ...
- 【ARTS】01_02_左耳听风-20181119~1125
Algorithm 做一个 leetcode 的算法题 Unique Email Addresses https://leetcode.com/problems/unique-email-addres ...
- Linux 应用笔记
Linux 应用笔记 Linux 应用笔记 小书匠 Raspberry Pi 常用命令 CentOs Raspberry Ubuntu python 实用教程 Vim 权限问题 内存分配 shell ...
- Maya API编程快速入门
一.Maya API编程简介 Autodesk® Maya® is an open product. This means that anyone outside of Autodesk can ch ...
随机推荐
- 微信小程序中显示html富文本的方法
微信小程序中显示html富文本的方法 使用方法:git地址:https://github.com/icindy/wxParse 一.下载wxParse文件 二.在要引入的页面的js文件中,引入文件 j ...
- manacher 和 扩展KMP
manacher 和 扩展KMP 事实上,这两个东西是一样的. 考虑 manacher 的过程 我们实时维护最远扩展的位置 \(mx\) 以及这个回文串的回文中心 \(l\) ,那么显然当然位置如果没 ...
- Apache搭建http网站服务器入门教程
Apache搭建http网站服务器入门教程 准备工具 一台带有Linux系统的主机,这里使用CentOS 7.1 64位系统 一个备案过的域名,这里使用www.hellopage.cn 一台可以访问网 ...
- php简易分词
http://www.xunsearch.com/ 示例 http://www.xunsearch.com/scws/demo/v48.php
- KMP算法中next数组的构建
记得初学$kmp$的时候 老师让大家把它直接背下来 然而不理解的话 不仅调试起来比较慢 很多题目也难往$kmp$上想 ----------------------------------------- ...
- 用 Flask 来写个轻博客 (28) — 使用 Flask-Assets 压缩 CSS/JS 提升网页加载速度
Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 Flask-Assets 将 Flask-Assets 应用 ...
- Laex/Delphi-OpenCV
https://github.com/Laex/Delphi-OpenCV 66 Star119 Fork75 Laex/Delphi-OpenCV CodeIssues 3Pull requests ...
- asp.net的处理机制(.ashx/.aspx)
浅谈自己对asp.net 处理机制的图解 图解的内容有点多(包含asp.net 的处理机制和页面生命周期的重要事件,建议小伙伴把图片下载查看可好?) asp.net处理机制解说 当浏览器发送一条请求给 ...
- APP测试功能点大全
APP测试要点 APP测试的时候,建议让开发打好包APK和IPA安装包,测试人员自己安装应用,进行测试.在测试过程中需要注意的测试点如下: 1.安装和卸载 ●应用是否可以在IOS不同系统版本或 ...
- VM虚拟机启动centos出现内部错误
内部错误 解决办法 1.关闭虚拟机后,单击VM,右键,以管理员身份运行. 2.找到电脑的 管理— Vmware workstation server ,默认状态下是手动,把他改为自动重启就可以啦.