-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfigureToWords.py
28 lines (24 loc) · 939 Bytes
/
figureToWords.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
singleDigit = ['zero', 'one', 'two', 'three',
'four', 'five', 'six', 'seven', 'eight', 'nine']
doubleDigit = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen',
'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
tenMultiple = ['', '', 'twenty', 'thirty', 'forty',
'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
tenPower = 'hundred'
def fig2words(n: int) -> str:
length = len(str(n))
s = ''
if length == 1:
return singleDigit[n].capitalize()
if length == 3:
s += singleDigit[n//100]+' '+tenPower
n = n % 100
if n == 0:
return s.capitalize()
else:
s += ' and '
if n > 9 and n < 20:
return (s+doubleDigit[n % 10]).capitalize()
s += tenMultiple[n//10]
return s.capitalize() if n % 10 == 0 else (s+' '+singleDigit[n % 10]).capitalize()
print(fig2words(50))