Given a number between 0-99999, the function number_to_english, return the same number pass to argument in letters.
Simple task, but the code is a little bit messy :D
convertion = { 0:'zero', 1:'one', 2:'two', 3:'three', 4:'four', 5:'five', 6:'six', 7:'seven', 8:'eight', 9:'nine', 10:'ten', 11:'eleven', 12:'twelve', 13:'thirteen', 14:'fourteen', 15:'fifteen', 16:'sixteen', 17:'seventeen', 18:'eighteen', 19:'nineteen', 20:'twenty', 30:'thirty', 40:'forty', 50:'fifty', 60:'sixty', 70:'seventy', 80:'eighty', 90:'ninety' } def convert(num): if not 0 <= num <= 99999: return '' thousand, num = divmod(num, 1000) hundred, num = divmod(num, 100) ten, unit = divmod(num, 10) output = [] if thousand: output += [convert(thousand), 'thousand'] if hundred: output += [convertion[hundred], 'hundred'] if ten == 1: output += [convertion[ten*10 + unit]] else: if ten: output += [convertion[ten*10]] if not output or unit: output += [convertion[unit]] return ' '.join(output) number_to_english = convert
def mil(n):num={'0':'zero','1':'one','2':'two','3':'three','4':'four','5':'five','6':'six','7':'seven','8':'eight','9':'nine'}dec={'10':'ten','11':'eleven','12':'twelve','13':'thirteen','14':'fourteen','15':'fifteen','16':'sixteen','17':'seventeen','18':'eighteen','19':'nineteen'}dec_com={'20':'twenty','30':'thirty','40':'forty','50':'fifty','60':'sixty','70':'seventy','80':'eighty','90':'ninety'}mil="thousand"th=[]n_t=[x for x in n]for i in n_t:if len(n_t)==2:if i!='1' and n_t[1]=='0':th.append(dec_com[i+'0'])th.append(mil)breakelif i=='1':th.append(dec[i+n_t[1]])th.append(mil)breakelse:th.append(dec_com[i+'0'])th.append(num[n_t[1]])th.append(mil)breakelse:th.append(num[i])th.append(mil)return thdef cen(n):num={'0':'zero','1':'one','2':'two','3':'three','4':'four','5':'five','6':'six','7':'seven','8':'eight','9':'nine'}dec={'10':'ten','11':'eleven','12':'twelve','13':'thirteen','14':'fourteen','15':'fifteen','16':'sixteen','17':'seventeen','18':'eighteen','19':'nineteen'}dec_com={'20':'twenty','30':'thirty','40':'forty','50':'fifty','60':'sixty','70':'seventy','80':'eighty','90':'ninety'}cen="hundred"c=[]n_d=[x for x in n]for m in n_d:if n_d[0]!='0':c.append(num[m])c.append(cen)if n_d[1]=='0' and n_d[2]=='0':breakelif n_d[1]=='0' and n_d[2]!='0':c.append(num[n_d[2]])breakelif n_d[1]!='1' and n_d[2]=='0':c.append(dec_com[n_d[1]+'0'])breakelif n_d[1]=='1':c.append(dec[n_d[1]+n_d[2]])breakelse:c.append(dec_com[n_d[1]+'0'])c.append(num[n_d[2]])breakelse:if n_d[1]=='0' and n_d[2]=='0':breakelif n_d[1]=='0' and n_d[2]!='0':c.append(num[n_d[2]])breakelif n_d[1]!='1' and n_d[2]=='0':c.append(dec_com[n_d[1]+'0'])breakelif n_d[1]!='1' and n_d[2]!='0':c.append(dec_com[n_d[1]+'0'])c.append(num[n_d[2]])breakelif n_d[1]=='1':c.append(dec[n_d[1]+n_d[2]])breakreturn cdef number_to_english(n):num={0:'zero',1:'one',2:'two',3:'three',4:'four',5:'five',6:'six',7:'seven',8:'eight',9:'nine'}dec={10:'ten',11:'eleven',12:'twelve',13:'thirteen',14:'fourteen',15:'fifteen',16:'sixteen',17:'seventeen',18:'eighteen',19:'nineteen'}dec_com={20:'twenty',30:'thirty',40:'forty',50:'fifty',60:'sixty',70:'seventy',80:'eighty',90:'ninety'}th=[]c=[]m='{0:,}'.format(n)m=m.split(",")try:if n<0 or type(n)==float or n>99999:passelif n<10:c.append(num[n])elif n<20:c.append(dec[n])elif n%10==0 and n<99:c.append(dec_com[n])elif n<99:k=list(str(n))c.append(dec_com[int(k[0]+'0')])c.append(num[int(k[1])])else:c=cen(m[1])th=mil(m[0])except IndexError:if n<0 or type(n)==float or n>99999:passelif n<10:c.append(num[n])elif n<20:c.append(dec[n])elif n%10==0 and n<99:c.append(dec_com[n])elif n<99:k=list(str(n))c.append(dec_com[int(k[0]+'0')])c.append(num[int(k[1])])else:c=cen(m[0])t=[]t.extend(th)t.extend(c)return " ".join(t)- convertion = {
- 0:'zero', 1:'one', 2:'two', 3:'three', 4:'four', 5:'five',
- 6:'six', 7:'seven', 8:'eight', 9:'nine', 10:'ten',
- 11:'eleven', 12:'twelve', 13:'thirteen', 14:'fourteen', 15:'fifteen',
- 16:'sixteen', 17:'seventeen', 18:'eighteen', 19:'nineteen',
- 20:'twenty', 30:'thirty', 40:'forty', 50:'fifty',
- 60:'sixty', 70:'seventy', 80:'eighty', 90:'ninety'
- }
- def convert(num):
- if not 0 <= num <= 99999:
- return ''
- thousand, num = divmod(num, 1000)
- hundred, num = divmod(num, 100)
- ten, unit = divmod(num, 10)
- output = []
- if thousand:
- output += [convert(thousand), 'thousand']
- if hundred:
- output += [convertion[hundred], 'hundred']
- if ten == 1:
- output += [convertion[ten*10 + unit]]
- else:
- if ten:
- output += [convertion[ten*10]]
- if not output or unit:
- output += [convertion[unit]]
- return ' '.join(output)
- number_to_english = convert
Test.describe("Simple test") test.assert_equals(number_to_english(0),'zero') test.assert_equals(number_to_english(9),'nine') test.assert_equals(number_to_english(11),'eleven') test.assert_equals(number_to_english(20),'twenty') test.assert_equals(number_to_english(100),'one hundred') test.assert_equals(number_to_english(12321),'twelve thousand three hundred twenty one') test.assert_equals(number_to_english(99999),'ninety nine thousand nine hundred ninety nine') Test.describe("Not valid case") test.assert_equals(number_to_english(-1),'') test.assert_equals(number_to_english(9999999),'')
- Test.describe("Simple test")
- test.assert_equals(number_to_english(0),'zero')
- test.assert_equals(number_to_english(9),'nine')
- test.assert_equals(number_to_english(11),'eleven')
- test.assert_equals(number_to_english(20),'twenty')
- test.assert_equals(number_to_english(100),'one hundred')
- test.assert_equals(number_to_english(12321),'twelve thousand three hundred twenty one')
- test.assert_equals(number_to_english(99999),'ninety nine thousand nine hundred ninety nine')
- Test.describe("Not valid case")
- test.assert_equals(number_to_english(-1),'')
- test.assert_equals(number_to_english(9999999),'')