# Number to words

• ``````class Solution(object):
def numberToWords(self, num):
if num == 0:
return "Zero"
result = self.helperFunction(num%1000)
num = num /1000
if (num > 0 and num%1000 > 0):
result = self.helperFunction(num%1000) + "Thousand " + result
num = num / 1000
if (num > 0 and num % 1000 > 0):
result = self.helperFunction(num % 1000) + "Million " + result
num = num / 1000
if (num > 0 and num % 1000 > 0):
result = self.helperFunction(num % 1000) + "Billion " + result

return result.rstrip()

def helperFunction(self,n):
dict1 = {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"}
dict2 = {2: "Twenty", 3: "Thirty", 4: "Forty", 5: "Fifty", 6: "Sixty", 7: "Seventy", 8: "Eighty", 9: "Ninety"}
if n > 99:
answer += dict1[n/100] + " Hundred "

n = n % 100
if n < 20 and n > 9:
answer += dict1[n] + " "
else:
if n > 19:
answer += dict2[n/10] + " "
n = n % 10
if n% 10 > 0:
answer += dict1[n%10] + " "

``````

• ``````# These only need to be defined once.
# We use lists instead of dictionaries, because it's more efficient (hashing a key still takes cycles).

TRANS = [
'Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine',
'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen',
'Twenty',
]

TENS = [
'', 'Ten', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety',
]

def partToArray(array, number, unit):
"""
Accumulates words corresponding to number in array, with a unit word (if any).
"""
if number:
# hundreds, remainder = divmod(number, 100)
hundreds, remainder = number // 100, number % 100
# tens, units = divmod(remainder, 10)
tens, units = remainder // 10, remainder % 10
if hundreds:
array.append(TRANS[hundreds])
array.append('Hundred')
# Note: We could be using proper English...
# if remainder:
#     array.append('And')
if remainder >= 21:
array.append(TENS[tens])
else:
units = remainder
if units:
# Note: We could be using proper English and use hyphens.
array.append(TRANS[units])
if unit:
array.append(unit)

class Solution(object):
def numberToWords(self, number):
"""
:type num: int
:rtype: str
"""
if number == 0:
return 'Zero'

array = []

# if number < 0:
#    array.append('Minus')
#    number = abs(number)

# divmod sounds better, but profiling shows it's more expensive due to function call.
# billions, remainder = divmod(number, 1000000000)
# millions, remainder = divmod(remainder, 1000000)
# thousands, units = divmod(remainder, 1000)
billions, remainder = number // 1000000000, number % 1000000000
millions, remainder = remainder // 1000000, remainder % 1000000
thousands, units = remainder // 1000, remainder % 1000

partToArray(array, billions, 'Billion')
partToArray(array, millions, 'Million')
partToArray(array, thousands, 'Thousand')
partToArray(array, units, None)
return ' '.join(array)
``````

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