-
Notifications
You must be signed in to change notification settings - Fork 1
/
integer-to-english-words.py
71 lines (68 loc) · 1.33 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
n = {
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',
}
ty = {
2:'Twenty',
3:'Thirty',
4:'Forty',
5:'Fifty',
6:'Sixty',
7:'Seventy',
8:'Eighty',
9:'Ninety',
}
def speak(num):
ans = []
if num / 10**9 >= 1:
ans += speak(num/10**9)
ans.append('Billion')
num = num % 10**9
if num / 10**6 >= 1:
ans += speak(num/10**6)
ans.append('Million')
num = num % 10**6
if num / 10**3 >= 1:
ans += speak(num/10**3)
ans.append('Thousand')
num = num % 10**3
if num / 10**2 >= 1:
ans += speak(num/10**2)
ans.append('Hundred')
num = num % 10**2
if num > 20:
ans.append(ty[num/10])
if num % 10 != 0:
ans.append(n[num%10])
elif num == 0:
if len(ans) == 0:
ans.append('Zero')
else:
ans.append(n[num])
return ans
class Solution(object):
def numberToWords(self, num):
"""
:type num: int
:rtype: str
"""
ans = speak(num)
return ' '.join(ans)