Skip to content

Instantly share code, notes, and snippets.

@sreevidyavutukuru
Created July 13, 2017 23:32
Show Gist options
  • Save sreevidyavutukuru/162c5dae97ad00e49e9ced1641e948f0 to your computer and use it in GitHub Desktop.
Save sreevidyavutukuru/162c5dae97ad00e49e9ced1641e948f0 to your computer and use it in GitHub Desktop.
'''Given an integer between 0 and 999,999, print an English phrase that describes the integer (eg, “One Thousand, Two Hundred and Thirty Four”).'''
dict_units = {0:'',1:'one',2:'two',3:'three',4:'four',5:'five',
6:'six',7:'seven',8:'eight',9:'nine'}
dict_tens = {0:'',10: 'ten',20:'twenty',30:'thirty',40:'fourty',50:'fifty',60:'sixty',70:'seventy',80:'eighty',90:'ninty'}
steps = {0: '', 1:'thousand', 2 : 'million', 3: 'billion'}
def exp(n):
strn = str(n)
if len(strn) == 3 :
str1 = dict_units[int(strn[0])] + ' hundred '+ dict_tens[int(strn[1])*10] +' ' + dict_units[int(strn[2])]
elif len(strn) == 2 :
str1 = dict_tens[int(strn[0])*10] +' ' + dict_units[int(strn[1])]
elif len(strn) == 1:
str1 = dict_units[int(strn[0])]
else:
str1=''
return str1
def num_desc(n):
result = ""
step = 0
while n != 0:
if n%1000 != 0:
result = exp(n%1000) + ' ' + steps[step] + ' '+ result
n = n/1000
step += 1
return result
print num_desc(398275)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment