【Python CheckiO 题解】SP

时间:2024-06-18 12:33:32

题目描述

【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]