Created
October 31, 2013 20:35
-
-
Save markovAntony/7256666 to your computer and use it in GitHub Desktop.
This program will give the fully-qualified name of a given integer using the American naming convention.
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
""" | |
This program will give the fully-qualified name of a given integer from: | |
{ | |
zero | |
0 | |
} | |
to | |
{ | |
novemtrigintillion | |
1e+120 | |
} | |
@author Andrew Brown | |
@date 10/30/2013 | |
""" | |
#Lexical dictionaries | |
base = {0:'zero',1:'one',2:'two',3:'three',4:'four',5:'five',6:'six' | |
,7:'seven',8:'eight',9:'nine',10:'ten',11:'eleven',12:'twelve'} | |
smallPrefix = {2:'twen',3:'thir',4:'four',5:'fif',6:'six',7:'seven' | |
,8:'eigh',9:'nine'} | |
largeBasePrefix = {1:'m',2:'b',3:'tr',4:'quadr',5:'quint',6:'sext' | |
,7:'sept',8:'oct',9:'non'} | |
largeFirstPrefix = {0:'',1:'un',2:'duo',3:'tre',4:'quattuor',5:'quin',6:'sex' | |
,7:'sept',8:'octo',9:'novem'} | |
largeSecondPrefix = {1:'dec',2:'vigint',3:'trigint'} | |
#Recursive function evaluates the following cases: | |
#base case: 0 - 99 | |
#case 1: 100 - 999 | |
#case 2: 1000 - 999999 | |
#case 3: 1e6 - 1e120 | |
def num_name(x): | |
length = len(str(x)) | |
if(length > 0 and length < 3): | |
return base_digits(x) | |
elif(length >= 3 and length < 4): | |
return (base[int(str(x)[:1])] + ' hundred '\ | |
+ zero_helper(x, length)).strip() | |
elif(length >= 4 and length < 7): | |
return (num_name(str(x)[:length - 3]) + ' thousand '\ | |
+ zero_helper(x, length)).strip() | |
else: | |
lexKey = ((length - 1) / 3) - 1 | |
return (num_name(str(x)[:(length % digit_place_helper(length)) + 1])\ | |
+ ' '\ | |
+ large_syntax_helper(x, lexKey) + 'illion '\ | |
+ zero_helper(x, length)).strip() | |
#Returns 0 - 99 base case string | |
def base_digits(x): | |
x = int(x) | |
if(x < 13): | |
return base[x] | |
elif(x >= 13 and x < 20): | |
return smallPrefix[int(str(x)[1:])] + 'teen' | |
else: | |
return smallPrefix[int(str(x)[:-1])] \ | |
+ ('ty' if base[int(str(x)[1:])] == 'zero'\ | |
else 'ty-') \ | |
+ ('' if base[int(str(x)[1:])] == 'zero'\ | |
else base[int(str(x)[1:])]) | |
#Builds compound prefixes for numbers starting at decillion | |
def large_syntax_helper(x, lexKey): | |
if(lexKey < 10): | |
return largeBasePrefix[lexKey] | |
else: | |
return largeFirstPrefix[int(str(lexKey)[1:])]\ | |
+ largeSecondPrefix[int(str(lexKey)[:-1])] | |
#Returns smallest number of digits in a place grouping | |
#e.g. 7 - 9 will always return 7 to pair with 'million' | |
# 10 - 12 will always return 10 to pair with 'billion' | |
# etc. | |
def digit_place_helper(x): | |
if(x % 3 == 1): | |
return x | |
elif(x % 3 == 2): | |
return x - 1 | |
else: | |
return x - 2 | |
#Returns blank string only if ALL trailing digits after the top-most | |
#digit grouping are 0, otherwise recurse | |
def zero_helper(x, length): | |
return ('' if int(str(x)[(length % digit_place_helper(length)) + 1:]) == 0\ | |
else num_name(int(str(x)[(length % digit_place_helper(length)) + 1:]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment