-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path273.integer-to-english-words.py
More file actions
81 lines (74 loc) · 2.05 KB
/
273.integer-to-english-words.py
File metadata and controls
81 lines (74 loc) · 2.05 KB
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
class Solution:
def numberToWords(self, num: int) -> str:
if num == 0:
return "Zero"
units = [
"",
"One ",
"Two ",
"Three ",
"Four ",
"Five ",
"Six ",
"Seven ",
"Eight ",
"Nine "
]
tens = [
"",
"",
"Twenty ",
"Thirty ",
"Forty ",
"Fifty ",
"Sixty ",
"Seventy ",
"Eighty ",
"Ninety "
]
conv = [
"",
"Thousand ",
"Million ",
"Billion ",
]
ones = [
'Ten',
'Eleven',
'Twelve',
'Thirteen',
'Fourteen',
'Fifteen',
'Sixteen',
'Seventeen',
'Eighteen',
'Nineteen',
]
# if temp == 0:
# return ""
def calc(num):
string = ""
temp = num
temp_str = str(temp)
if len(temp_str) == 1:
string += units[temp%10]
elif len(temp_str) == 2:
if(temp_str[0]) == '1':
string += ones[temp%10]
else:
string += tens[int(temp_str[0])] + units[temp%10]
elif len(str(temp)) == 3:
if(temp_str[1]) == '1':
string += units[int(temp_str[0])] + 'Hundred '+ ones[temp%10]
else:
string += units[int(temp_str[0])] + 'Hundred '+ tens[int(temp_str[1])] + units[temp%10]
return string
ext_string = ""
ext_counter = 0
int_counter = 0
while(num):
if num%1000:
ext_string = calc(num%1000).strip()+ " "+ conv[ext_counter] + ext_string
num = num // 1000
ext_counter += 1
return ext_string.strip()