Last active
February 16, 2020 18:33
-
-
Save nkpro2000sr/4c84f030a5b4eae79b9794c1f9e969e8 to your computer and use it in GitHub Desktop.
number to word #just_for_fun_:-) comment your efficient code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ones = ['<0>',"one","two","three","four","five","six","seven","eight","nine"] | |
tens = ['','<Teenify>',"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"] | |
def Teenify(num): | |
num[1]='' | |
if num[0]=="<0>":num[0]="ten" | |
elif num[0]=="one":num[0]="eleven" | |
elif num[0]=="two":num[0]="twelve" | |
elif num[0]=="three":num[0]="thirteen" | |
elif num[0]=="five":num[0]="fifteen" | |
else :num[0]=num[0]+"teen" | |
return num | |
def n2wd(n, num_system = 1): | |
""" | |
$n is input number (integer) | |
-> $n in words (string) | |
$num_system = 1 => Indian Number System | |
$num_system = 2 => International Number System | |
""" | |
if num_system == 1 : | |
digits = [ones, tens, "hundred", "thousand", "10k", "lakh", "10l"] # you can add more | |
elif num_system == 2 : | |
digits = [ones, tens, "hundred", "thousand", "10k", "100k", "million"] # you can add more | |
if n == 0 : return "zero" | |
else : num = [] | |
while n > 0 : | |
num.append(n%10) | |
n//=10 | |
index = 0 | |
while index<len(num): | |
if index < 2 : # once and tens | |
n = digits[index][num[index]] | |
if n == '<Teenify>': num = Teenify(num) | |
else : num[index] = n | |
elif index == 2 : # hundreds | |
if num[index] == 0 : num[index] = '' | |
else: | |
num[index] = ones[num[index]]+" hundred" | |
if num[0]!='<0>' : num[index]+=" and" # command this line if you don't need ' and' | |
else : num[0]='' | |
else : | |
num2 = num[index:index+num_system+1] | |
num[index] = n2wd(sum([n*10**d for d,n in enumerate(num2)])) | |
if len(num) > index+1 : num[index+1:index+num_system+1] = ['']*num_system | |
if num[index] != 'zero' : num[index]+=' '+digits[index] | |
else : num[index] = '' | |
index += num_system | |
index += 1 | |
return ' '.join(reversed(list(filter(len,num)))).replace('<0>','') | |
num_system = int(input("CHOOSE:\n1. Indian Number System\n2. International Number System\n"));print() | |
while 5 : print(n2wd(int(input('>')), num_system)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
code_from_quora