-
Notifications
You must be signed in to change notification settings - Fork 43
/
integer-to-english-words.py
176 lines (151 loc) · 5.15 KB
/
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""
273. Integer to English Words
Hard
Convert a non-negative integer num to its English words representation.
Example 1:
Input: num = 123
Output: "One Hundred Twenty Three"
Example 2:
Input: num = 12345
Output: "Twelve Thousand Three Hundred Forty Five"
Example 3:
Input: num = 1234567
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
Constraints:
0 <= num <= 231 - 1
"""
# V0
# V1
# https://leetcode.com/problems/integer-to-english-words/discuss/70643/Short-Python
class Solution(object):
def numberToWords(self, num):
"""
:type num: int
:rtype: str
"""
A = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", \
"Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", \
"Sixteen", "Seventeen", "Eighteen", "Nineteen"]
B = ["Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
C = ["Billion", "Million", "Thousand"]
def trans(n):
return [] if not n else [A[n // 100], "Hundred"] + trans(n%100) if n >= 100 \
else [A[n]] if n < 20 else [B[(n//10)-2]] + trans(n%10)
res, N = [], [10**9, 10**6, 1000, 1]
for i in range(4):
digit, num = divmod(num, N[i])
if digit: res += trans(digit) + C[i:i+1]
return " ".join(res) or "Zero"
# V1'
class Solution:
def numberToWords(self, num):
tens = [
"",
"Ten",
"Twenty",
"Thirty",
"Forty",
"Fifty",
"Sixty",
"Seventy",
"Eighty",
"Ninety"
]
teens = {
10: 'Ten',
11: 'Eleven',
12: 'Twelve',
13: 'Thirteen',
14: 'Fourteen',
15: 'Fifteen',
16: 'Sixteen',
17: 'Seventeen',
18: 'Eighteen',
19: 'Nineteen',
}
digits = [
"",
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine"
]
def parse(num):
if num >= 1000000000:
return parse(num // 1000000000).strip() + " Billion " + parse(num % 1000000000).strip()
elif num >= 1000000:
return parse(num // 1000000).strip() + " Million " + parse(num % 1000000).strip()
elif num >= 1000:
return parse(num // 1000).strip() + " Thousand " + parse(num % 1000).strip()
elif num >= 100:
return digits[num // 100] + " Hundred " + parse(num % 100).strip()
elif num >= 20:
return tens[num//10] + " " + parse(num % 10).strip()
elif num >= 10 and num < 20:
return teens[num]
else:
return digits[num]
if num == 0:
return "Zero"
return parse(num).strip()
# V1''
# (TLE)
# https://leetcode.com/problems/integer-to-english-words/discuss/213494/Python-solution
class Solution:
def numberToWords(self, num):
"""
:type num: int
:rtype: str
"""
def helper(num):
if num < 10:
return dic1[num]
elif 10 <= num < 20:
return dic3[num]
elif num < 100:
res = []
q, num = divmod(num, 10)
res.append(dic2[q*10])
if num > 0:
res.append(" "+dic1[num])
return "".join(res)
elif num < 1000:
q, num = divmod(num, 100)
if num == 0:
return dic1[q]+" "+"Hundred"
else:
return dic1[q]+" "+"Hundred"+" "+helper(num)
elif num < 1000000:
q, num = divmod(num, 1000)
if num == 0:
return helper(q)+" "+"Thousand"
else:
return helper(q)+" "+"Thousand"+" "+helper(num)
elif num < 1000000000:
q, num = divmod(num, 1000000)
if num == 0:
return helper(q)+" "+"Million"
else:
return helper(q)+" "+"Million"+" "+helper(num)
else:
q, num = divmod(num, 1000000000)
if num == 0:
return helper(q)+" "+"Billion"
else:
return helper(q)+" "+"Billion"+" "+helper(num)
if num == 0:
return "Zero"
dic1 = {9:"Nine", 8:"Eight", 7:"Seven", 6:"Six",
5:"Five", 4:"Four", 3:"Three", 2:"Two", 1:"One"}
dic2 = {90:"Ninety", 80:"Eighty", 70:"Seventy", 60:"Sixty",
50:"Fifty", 40:"Forty", 30:"Thirty", 20:"Twenty"}
dic3 = {19:"Nineteen", 18:"Eighteen", 17:"Seventeen",
16:"Sixteen", 15:"Fifteen", 14:"Fourteen", 13:"Thirteen",
12:"Twelve", 11:"Eleven", 10:"Ten"}
return helper(num)
# V2