# Python Solution using dictionary

• Make two dictionaries as shown below.
Sweep n from left to right, and put converted words into a list.

``````class Solution(object):
def __init__(self):
self.tens = {0:'', 1:'', 2:'Hundred', 3:'Thousand', 6:'Million', 9:'Billion'}
self.nums = {\
0: "", 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"}

def numberToWords(self, num):
"""
:type num: int
:rtype: str
"""
i, res = 0, []
while 10**(i+1) <= num: i += 1
# assert (10**i) <= num <= (10**(i+1))-1

while i > -1:
if i in self.tens:
if num < 1000:
res += self.hundsToLists(num, i)
i = -1
else:
res += self.hundsToLists(num//(10**i), i)
res += self.tens[i],
num, i = num%(10**i), i-1
else: i -= 1
return ' '.join([r for r in res if r != '']) or "Zero"

def hundsToLists(self, n, od):
lst = []
if n >= 100:
lst += [self.nums[(n//100)], self.tens[2]]          # put hundreds first if needed
n -= (n//100)*100
if n in self.nums: lst += self.nums[n],                 # simply look-up
else: lst += [self.nums[(n//10)*10], self.nums[n%10]]   # decipher ten and one
return lst
``````

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