-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path273.Integer-to-English-Words.py
63 lines (53 loc) · 1.67 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
# https://leetcode.com/problems/missing-number/
#
# algorithms
# Easy (24.49%)
# Total Accepted: 106,816
# Total Submissions: 436,070
from collections import OrderedDict
class Solution(object):
def numberToWords(self, num):
"""
:type num: int
:rtype: str
"""
if num == 0:
return 'Zero'
one_place = {idx: eng for idx, eng in enumerate(
['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'])}
ten_place = {idx + 1: eng for idx, eng in enumerate(
['Ten', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'])}
ten_place_1 = {
11: 'Eleven',
12: 'Twelve',
13: 'Thirteen',
14: 'Fourteen',
15: 'Fifteen',
16: 'Sixteen',
17: 'Seventeen',
18: 'Eighteen',
19: 'Nineteen',
}
unit = OrderedDict([
(1000000000, 'Billion'),
(1000000, 'Million'),
(1000, 'Thousand'),
(100, 'Hundred'),
])
res = ''
while num >= 100:
for u, eng in unit.iteritems():
if num >= u:
n = num / u
num %= u
if n > 0:
res += ' ' + self.numberToWords(n)
res += ' ' + eng
if num in ten_place_1:
res += ' ' + ten_place_1[num]
return res.strip()
if num >= 10:
res += ' ' + ten_place[num / 10]
if num > 0 and num % 10 != 0:
res += ' ' + one_place[num % 10]
return res.strip()