-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy path0273-integer-to-english-words.py
30 lines (26 loc) · 1.13 KB
/
0273-integer-to-english-words.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
29
30
class Solution:
def numberToWords(self, num: int) -> str:
if num == 0:
return "Zero"
lessTwentyMap = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]
tensMap = ["", "", "Twenty", "Thirty", "Forty",
"Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
thousandsMap = ["", "Thousand", "Million", "Billion"]
def helper(num: int):
if num == 0:
return ""
elif num < 20:
return lessTwentyMap[num] + " "
elif num < 100:
return tensMap[num // 10] + " " + helper(num % 10)
else:
return lessTwentyMap[num // 100] + " Hundred " + helper(num % 100)
result = ""
for i, unit in enumerate(thousandsMap):
if num % 1000 != 0:
result = helper(num % 1000) + unit + " " + result
num //= 1000
return result.strip()
num = 1234567
print(Solution().numberToWords(num))