# Clear python solution

• ``````def numberToWords(self, num):
"""
:type num: int
:rtype: str
"""
if num == 0:
return 'Zero'
ddict = {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', 30: 'Thirty', 40: 'Forty', 50: 'Fifty',
60: 'Sixty', 70: 'Seventy', 80: 'Eighty', 90: 'Ninety'}
base = ['', 'Thousand', 'Million', 'Billion']
result, exp = '', 1000
i, chunk = 0, 0
while num:
chunk = num % exp
if chunk:
result = base[i] + ' ' + result
one = chunk % 10
two = (chunk % 100) - one
three = chunk // 100
if two == 10:
result = ddict[two+one] + ' ' + result
else:
if one:
result = ddict[one] + ' ' + result
if two:
result = ddict[two] + ' ' +result
if three:
result = ddict[three] + ' Hundred ' + result
num //= exp
i += 1
return result.rstrip()``````

• recursive version 68ms

``````class Solution(object):
def numberToWords(self, num):
"""
:type num: int
:rtype: str
"""
BILLION = 1000000000
MILLION = 1000000
THOUSAND = 1000
HUNDRED = 100
TENs = ["Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
ONEs = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]

def word_helper(n):
if n >= BILLION:
return " ".join([word_helper(n/BILLION), "Billion", word_helper(n % BILLION)]).strip()
elif n >= MILLION:
return " ".join([word_helper(n/MILLION), "Million", word_helper(n % MILLION)]).strip()
elif n >= THOUSAND:
return " ".join([word_helper(n/THOUSAND), "Thousand", word_helper(n % THOUSAND)]).strip()
elif n >= HUNDRED:
return " ".join([word_helper(n/HUNDRED), "Hundred", word_helper(n % HUNDRED)]).strip()
elif n >= 20:
nn = (n-20)/10
return " ".join([TENs[nn], word_helper(n - (nn+2)*10)]).strip()
else:
return "" if n == 0 else ONEs[n-1]
return "Zero" if num == 0 else word_helper(num).strip()
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.