创建一个函数,将任何正数转换为表示单词中数字的字符串。该函数应该适用于 0 到 999999 之间的所有数字。
例子
number2words(0) ==> "zero"
number2words(1) ==> "one"
number2words(9) ==> "nine"
number2words(10) ==> "ten"
number2words(17) ==> "seventeen"
number2words(20) ==> "twenty"
number2words(21) ==> "twenty-one"
number2words(45) ==> "forty-five"
number2words(80) ==> "eighty"
number2words(99) ==> "ninety-nine"
number2words(100) ==> "one hundred"
number2words(301) ==> "three hundred one"
number2words(799) ==> "seven hundred ninety-nine"
number2words(800) ==> "eight hundred"
number2words(950) ==> "nine hundred fifty"
number2words(1000) ==> "one thousand"
number2words(1002) ==> "one thousand two"
number2words(3051) ==> "three thousand fifty-one"
number2words(7200) ==> "seven thousand two hundred"
number2words(7219) ==> "seven thousand two hundred nineteen"
number2words(8330) ==> "eight thousand three hundred thirty"
number2words(99999) ==> "ninety-nine thousand nine hundred ninety-nine"
number2words(888888) ==> "eight hundred eighty-eight thousand eight hundred eighty-eight"
题目难度:一般
题目来源:https://www.codewars.com/kata/52724507b149fa120600031d
def number2words(n: int) -> str:
""" works for numbers between 0 and 999999 """
pass
assert number2words(0) == "zero"
assert number2words(1) == "one"
assert number2words(8) == "eight"
assert number2words(10) == "ten"
assert number2words(19) == "nineteen"
assert number2words(20) == "twenty"
assert number2words(22) == "twenty-two"
assert number2words(100) == "one hundred"
assert number2words(301) == "three hundred one"
assert number2words(793) == "seven hundred ninety-three"
assert number2words(888888) == "eight hundred eighty-eight thousand eight hundred eighty-eight"
qunzi
(群子)
2
def number2words(n: int) -> str:
a=[int(i) for i in str(n)]
dict1={0:"zero",1:"one",2:"two",3:"three",4:"four",5:"six",6:"six",7:"seven",8:"eight",9:"nine"}
dict2={10:"ten",11:"eleven",12:"twelve",13:"thirteen",14:"fourteen",15:"fifteen",16:"sixteen",17:"seventeen",18:"eighteen",19:"nineteen"}
dict3={2:"twenty",3:"thirty",4:"fourty",5:"fifty",6:"sixty",7:"seventy",8:"eighty",9:"ninety",}
if len(a)==1:
return dict1[n]
elif len(a)==2:
if a[0]==1:
return dict2[n]
if a[0]>1 and a[-1]!=0:
return dict3[a[0]]+"-"+dict1[a[1]]
else:
return dict3[a[0]]
elif len(a)==3:
if a[1]==0:
if a[-1]==0:
return dict1[a[0]]+" hundred"
else:
return dict1[a[0]]+" hundred "+dict1[a[-1]]
else:
return dict1[a[0]]+" hundred "+dict3[a[1]]+"-"+dict1[a[-1]]
##最后一个没写
assert number2words(0) == "zero"
assert number2words(99) == "ninety-nine"
assert number2words(8) == "eight"
assert number2words(10) == "ten"
assert number2words(19) == "nineteen"
assert number2words(20) == "twenty"
assert number2words(22) == "twenty-two"
assert number2words(100) == "one hundred"
assert number2words(301) == "three hundred one"
assert number2words(793) == "seven hundred ninety-three"
lekaixin
(ALe阿乐)
3
def number2words(n: int) -> str:
def get_number(list1: list):
res_list = []
x = sum([int(i) if len(i) <= 3 else int(i[:-3]) for i in list1])
for i in list1:
if len(i) > 3:
i = i[:-3]
if len(i) == 3:
res_list.append(word_dict0[i[0]])
res_list.append('hundred')
elif len(i) == 2:
if i == '10':
res_list.append(word_dict1[str(x)[-2:]])
break
else:
res_list.append(word_dict2[i])
elif len(i) == 1:
if i != '0':
res_list.append(word_dict0[i])
else:
if len(str(x)) == 1:
res_list.append(word_dict0[i])
else:
continue
res_str = ' '.join([i for i in res_list if i != ''])
if '0' not in str(x)[-2:]:
index = res_str.rfind(' ')
if index == -1:
return res_str
else:
res_str = res_str[:index] + '-' + res_str[index + 1:]
return res_str
word_dict0 = {'0': 'zero', '1': 'one', '2': 'two', '3': 'three', '4': 'four', '5': 'five', '6': 'six', '7': 'seven',
'8': 'eight', '9': 'nine'}
word_dict1 = {'10': 'ten', '11': 'eleven', '12': 'twelve', '13': 'thirteen', '14': 'fourteen', '15': 'fifteen',
'16': 'sixteen', '17': 'seventeen', '18': 'eighteen', '19': 'nineteen'}
word_dict2 = {'00': '', '20': 'twenty', '30': 'thirty', '40': 'forty', '50': 'fifty', '60': 'sixty',
'70': 'seventy', '80': 'eighty', '90': 'ninety'}
n_list = [str(n)[i] + '0' * (len(str(n)) - i - 1) for i in range(len(str(n)))]
if len(n_list) > 3:
n_list1 = n_list[:-3]
n_list2 = n_list[-3:]
res_str1 = get_number(n_list1)
res_str2 = get_number(n_list2)
res_str = res_str1 + ' thousand ' + res_str2
else:
res_str = get_number(n_list)
return res_str
assert number2words(0) == "zero"
assert number2words(1) == "one"
assert number2words(8) == "eight"
assert number2words(10) == "ten"
assert number2words(19) == "nineteen"
assert number2words(20) == "twenty"
assert number2words(22) == "twenty-two"
assert number2words(100) == "one hundred"
assert number2words(301) == "three hundred one"
assert number2words(793) == "seven hundred ninety-three"
assert number2words(888888) == "eight hundred eighty-eight thousand eight hundred eighty-eight"