1
+ class Solution (object ):
2
+ def numberToWords (self , num ):
3
+ """
4
+ :type num: int
5
+ :rtype: str
6
+ """
7
+ def helper (num ):
8
+ n = int (num )
9
+ num = str (n )
10
+ if n < 100 :
11
+ return subhelper (num )
12
+ else :
13
+ return ["One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine" ][int (num [0 ]) - 1 ] + " Hundred " + subhelper (num [1 :]) if num [1 :] != "00" else ["One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine" ][int (num [0 ]) - 1 ] + " Hundred"
14
+
15
+ def subhelper (num ):
16
+ n = int (num )
17
+ l1 = ["Zero" , "One" , "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" , "Nine" ]
18
+ l2 = ["Ten" , "Eleven" , "Twelve" , "Thirteen" , "Fourteen" , "Fifteen" , "Sixteen" , "Seventeen" , "Eighteen" , "Nineteen" ]
19
+ l3 = ["Twenty" , "Thirty" , "Forty" , "Fifty" , "Sixty" , "Seventy" , "Eighty" , "Ninety" ]
20
+ if n < 10 :
21
+ return l1 [int (num )]
22
+ if 10 <= n < 20 :
23
+ return l2 [n - 10 ]
24
+ if 20 <= n < 100 :
25
+ return l3 [int (num [0 ]) - 2 ] + " " + l1 [int (num [1 ])] if num [1 ] != "0" else l3 [int (num [0 ]) - 2 ]
26
+ res = ""
27
+ if num >= 1000000000 :
28
+ res = helper (str (num )[0 ]) + " Billion"
29
+ if str (num )[1 :4 ] != "000" :
30
+ res += " " + helper (str (num )[1 :4 ]) + " Million"
31
+ if str (num )[4 :7 ] != "000" :
32
+ res += " " + helper (str (num )[4 :7 ]) + " Thousand"
33
+ if str (num )[7 :] != "000" :
34
+ res += " " + helper (str (num )[7 :])
35
+ elif num >= 1000000 :
36
+ res = helper (str (num )[:- 6 ]) + " Million"
37
+ if str (num )[- 6 :- 3 ] != "000" :
38
+ res += " " + helper (str (num )[- 6 :- 3 ]) + " Thousand"
39
+ if str (num )[- 3 :] != "000" :
40
+ res += " " + helper (str (num )[- 3 :])
41
+ elif num >= 1000 :
42
+ res = helper (str (num )[:- 3 ]) + " Thousand"
43
+ if str (num )[- 3 :] != "000" :
44
+ res += " " + helper (str (num )[- 3 :])
45
+ else :
46
+ return helper (str (num ))
47
+
48
+ return res
0 commit comments