CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,本博客主要记录自己用 Python 在闯关时的做题思路和实现代码,同时也学习学习其他大神写的代码。
CheckiO 官网:https://checkio.org/
我的 CheckiO 主页:https://py.checkio.org/user/TRHX/
CheckiO 题解系列专栏:https://itrhx.blog.csdn.net/category_9536424.html
CheckiO 所有题解源代码:https://github.com/TRHX/Python-CheckiO-Exercise
题目描述
【Speech Module】:输入一个数字,将其转换成英文表达形式,字符串中的所有单词必须以一个空格字符分隔。
【链接】:https://py.checkio.org/mission/speechmodule/
【输入】:一个数字(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 0 <= number < 10:english = FIRST_TEN[number - 1]return englishelif 10 <= number < 20:english = SECOND_TEN[number - 10]return englishelif 20 <= number < 100:if number % 10 == 0:english = OTHER_TENS[number // 10 - 2]return englishelse:english = OTHER_TENS[number // 10 - 2] + ' ' + FIRST_TEN[number % 10 - 1]return englishelse:if number % 100 == 0:english = FIRST_TEN[number // 100 - 1] + ' ' + HUNDREDreturn englishelse:english = FIRST_TEN[number // 100 - 1] + ' ' + HUNDRED + ' ' + checkio(number % 100)return englishif __name__ == '__main__':# These "asserts" using only for self-checking and not necessary for auto-testingassert 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
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):n = number // 100t = [FIRST_TEN[n-1], HUNDRED] if n > 0 else []n = (number // 10) % 10t += [OTHER_TENS[n-2]] if n > 1 else []n = number % (10 if n > 1 else 20)t += [(FIRST_TEN+SECOND_TEN)[n-1]] if n > 0 else []return ' '.join(t)
大神解答 NO.2
# migrated from python 2.7
def checkio(number):l=["","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty"]d=dict(enumerate(l))d.update({30:"thirty",40:"forty",50:"fifty",60:"sixty",70:"seventy",80:"eighty",90:"ninety"})h=number//100if h:return (d[h]+" hundred "+checkio(number%100)).strip()if number in d:return d[number]return d[number//10*10]+" "+d[number%10]
大神解答 NO.3
def checkio(n, d=dict(enumerate(" one two three four five six seven eight nine ten eleven twelve".split(" ")))):def i(s, j=iter("o en ree ir ve f t ".split(" "))):for k in j: s = __import__("re").sub(k + "$", next(j), s)return sreturn(d[n//100]+" hundred "*(n>99)+d.get(n%100,n%100<20and i(d[n%10])+"teen"or i(d[n//10%10]).replace("u","")+"ty "+d[n%10])).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//100t = (number - h*100)//10f = number - h*100 - t*10if 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]