We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
d1={1:"One",2:"Two",3:"Three",4:"Four",5:"Five",6:"Six",7:"Seven",8:"Eight",9:"Nine"}d2={10:"Ten",11:"Eleven",12:"Twelve",13:"Thirteen",14:"Fourteen",15:"Fifteen",16:"Sixteen",17:"Seventeen",18:"Eighteen",19:"Nineteen"}d3={2:"Twenty",3:"Thirty",4:"Forty",5:"Fifty",6:"Sixty",7:"Seventy",8:"Eighty",9:"Ninety"}defnumber1(n):# this fonction give the numbre with word with number between 0 nd 100if1<=n<10:returnd1[n]elif10<=n<20:returnd2[n]else:a=n%10b=n//10ifa==0:returnd3[b]else:ch=d3[b]+" "+d1[a]returnchdefnumber2(n):# this fonction give the numbre with word with number between 0 nd 999ifn<100:returnnumber1(n)else:a=n%100b=n//100ifa==0:returnd1[b]+" Hundred"else:ch=d1[b]+" Hundred "+number1(a)returnchd4={1:"Thousand",2:"Million",3:"Billion",4:"Trillion"}defnumber3(n):# this fonction give the numbre with word with number between 0 nd 9999..ifn<1000:returnnumber2(n)else:ch1=str(n)l=[]foriinrange(len(ch1),0,-3):l.append(int(ch1[max(i-3,0):i]))ch2=number2(l[0])foriinrange(1,len(l)):ifl[i]!=0:ch2=number2(l[i])+" "+d4[i]+" "+ch2returnch2for_inrange(int(input())):num=int(input())print(number3(num))
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #17: Number to Words
You are viewing a single comment's thread. Return to all comments →