checkio-speech module

来源:互联网 发布:青岛知行国际派遣公司 编辑:程序博客网 时间:2024/06/05 10:07

史蒂芬的语音模块坏了。该模块是负责他的数字发音的。他必须点击输入所有数字,因此当有大数字就要花费他很长的时间来输入。为他写一个新的语音模块帮助机器人正常说话并且增加他的数字的处理速度。字符串中的所有单词必须以一个空格字符分隔。请小心使用空格 – 如果你把两个空格当做一个,那是很难看到的。

输入: 作为整数(int)的一个数字。

输出: 代表数字的字符串。(str)

范例:

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 < number < 1000

FIRST_TEN = ["zero", "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):    sayit = []    if number >= 100:        sayit.append(FIRST_TEN[number//100])        sayit.append(HUNDRED)        number %= 100    if number >= 20:        sayit.append(OTHER_TENS[number//10-2])        number %= 10    if number >= 10:        sayit.append(SECOND_TEN[number-10])    elif number > 0 or words == []:        sayit.append(FIRST_TEN[number])    return ' '.join(sayit)#These "asserts" using only for self-checking and not necessary for auto-testingif __name__ == '__main__':    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 checkio(400) == 'four hundred', "7th example"    assert not checkio(212).endswith(' '), "Don't forget strip whitespaces at the end of string"

把最后要打印的字符串用一个列表存储。判断是否大于100,是的话就append百位数字加hundred。然后把number%100取余数,在判断十位和个位。如果大于20在OTHER_TENS里面。否则找另外一个列表。

0 0
原创粉丝点击