-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path273 Integer to English Words.py
93 lines (80 loc) · 2.42 KB
/
273 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 2^31 - 1.
For example,
123 -> "One Hundred Twenty Three"
12345 -> "Twelve Thousand Three Hundred Forty Five"
1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
Author: Rajeev Ranjan
"""
class Solution(object):
def __init__(self):
self.table = {
0: None,
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",
100: "Hundred",
1000: "Thousand",
1000000: "Million",
1000000000: "Billion"
}
def numberToWords(self, num):
"""
Pay attention to the handling of 0's
:type num: int
:rtype: str
"""
if num == 0: return "Zero"
ret = []
self.toWords(num, ret)
ret = filter(lambda x: x, ret) # filter None as zeros
return " ".join(map(str, ret))
def toWords(self, num, ret):
"""
will call partial_parse
significance by significance
"""
SIGS = [1000000000, 1000000, 1000, 100]
for SIG in SIGS:
self.partial_parse(num, SIG, ret)
num %= SIG
TEN = 10
if num/TEN > 1:
ret.append(self.table[(num/TEN)*TEN])
ret.append(self.table[num%TEN])
def partial_parse(self, num, sig, ret):
"""
will call toWords
"""
if num/sig:
prefix = []
self.toWords(num/sig, prefix)
ret.extend(prefix)
ret.append(self.table[sig])
if __name__ == "__main__":
assert Solution().numberToWords(1234567891) == "One Billion Two Hundred Thirty Four Million Five Hundred Sixty " \
"Seven Thousand Eight Hundred Ninety One"