Saturday, September 30, 2023
HomeSoftware EngineeringHow you can Write Out Numbers in Python

# How you can Write Out Numbers in Python

## The problem#

Create a perform that transforms any constructive quantity to a string representing the quantity in phrases. The perform ought to work for all numbers between 0 and 999999.

Examples:

``````number2words(0)  ==>  "zero"
number2words(1)  ==>  "one"
number2words(9)  ==>  "9"
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)  ==>  "100"
number2words(301)  ==>  "300 one"
number2words(799)  ==>  "seven hundred ninety-nine"
number2words(800)  ==>  "eight hundred"
number2words(950)  ==>  "9 hundred fifty"
number2words(1000)  ==>  "one thousand"
number2words(1002)  ==>  "one thousand two"
number2words(3051)  ==>  "three thousand fifty-one"
number2words(7200)  ==>  "seven thousand 2 hundred"
number2words(7219)  ==>  "seven thousand 2 hundred nineteen"
number2words(8330)  ==>  "eight thousand 300 thirty"
number2words(99999)  ==>  "ninety-nine thousand 9 hundred ninety-nine"
number2words(888888)  ==>  "eight hundred eighty-eight thousand eight hundred eighty-eight"
``````

## The answer in Python code#

Choice 1:

``````phrases = "zero one two three 4 5 six seven eight 9" +
" ten eleven twelve 13 fourteen fifteen sixteen seventeen eighteen nineteen twenty" +
" thirty forty fifty sixty seventy eighty ninety"
phrases = phrases.break up(" ")

def number2words(n):
if n < 20:
return phrases[n]
elif n < 100:
return phrases[18 + n // 10] + ('' if n % 10 == 0 else '-' + phrases[n % 10])
elif n < 1000:
return number2words(n // 100) + " hundred" + (' ' + number2words(n % 100) if n % 100 > 0 else '')
elif n < 1000000:
return number2words(n // 1000) + " thousand" + (' ' + number2words(n % 1000) if n % 1000 > 0 else '')
``````

Choice 2:

``````d={0:'zero',1:'one',2:'two',3:'three',4:'4',5:'5',
6:'six',7:'seven',8:'eight',9:'9',10:'ten',
11:'eleven',12:'twelve',13:'13',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',
100:'hundred',1000:'thousand'}
def number2words(n):
""" works for numbers between 0 and 999999 """
if 0<=n<=20:return d[n]
if 20<n<=99 and npercent10:return d[10*(n//10)]+'-'+d[n%10]
if 20<n<99:return d[10*(n//10)]
if n<1000 and npercent100==0:return d[n//100]+' '+d
if 100<n<=999:return d[n//100]+' '+d+' '+number2words(npercent100)
if npercent1000==0:return d[n//1000]+' '+d
return number2words(n//1000)+' '+d+' '+number2words(npercent1000)
``````

Choice 3:

``````d = {1: 'one', 2: 'two', 3: 'three', 4: '4', 5: '5',
6: 'six', 7: 'seven', 8: 'eight', 9: '9', 10: 'ten',
11: 'eleven', 12: 'twelve', 13: '13', 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', 0:''}

def number2words(n):
s = (htu(n // 1000) + ' thousand ' if n // 1000 else '') + htu(n % 1000)
return ' '.be a part of(s.break up()) if s else 'zero'

def htu(n):
h, tu, u = n//100, n % 100, n % 10
t = (d[tu] if tu in d else d[tu//10*10] + '-' + d[u]).strip('-')
return d[h] + ' hundred ' + t if h else t
``````

## Check instances to validate our resolution#

``````from resolution import number2words
import check

@check.describe("fastened assessments")
def _():
@check.it("One digit Exams")
def _():
check.assert_equals(number2words(0), "zero")
check.assert_equals(number2words(1), "one")
check.assert_equals(number2words(8), "eight")

@check.it("Tens Exams")
def _():
check.assert_equals(number2words(10), "ten")
check.assert_equals(number2words(19), "nineteen")
check.assert_equals(number2words(20), "twenty")
check.assert_equals(number2words(22), "twenty-two")
check.assert_equals(number2words(54), "fifty-four")
check.assert_equals(number2words(80), "eighty")
check.assert_equals(number2words(98), "ninety-eight")

@check.it("A whole bunch Exams")
def _():
check.assert_equals(number2words(100),"100")
check.assert_equals(number2words(301),"300 one")
check.assert_equals(number2words(793),"seven hundred ninety-three")
check.assert_equals(number2words(800),"eight hundred")
check.assert_equals(number2words(650),"600 fifty")

@check.it("Hundreds Exams")
def _():
check.assert_equals(number2words(1000), "one thousand")
check.assert_equals(number2words(1003), "one thousand three")
``````
RELATED ARTICLES