Last active
September 20, 2016 13:50
-
-
Save jpwagner/7f8f33ad7de6535fea7c9632ac12a3be to your computer and use it in GitHub Desktop.
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
word_map = { | |
"1": { | |
"0": "", | |
"1": "one", | |
"2": "two", | |
"3": "three", | |
"4": "four", | |
"5": "five", | |
"6": "six", | |
"7": "seven", | |
"8": "eight", | |
"9": "nine" | |
}, | |
"2": { | |
"1": { | |
"0": "ten", | |
"1": "eleven", | |
"2": "twelve", | |
"3": "thirteen", | |
"4": "fourteen", | |
"5": "fifteen", | |
"6": "sixteen", | |
"7": "seventeen", | |
"8": "eighteen", | |
"9": "nineteen" | |
}, | |
"2": "twenty", | |
"3": "thirty", | |
"4": "forty", | |
"5": "fifty", | |
"6": "sixty", | |
"7": "seventy", | |
"8": "eighty", | |
"9": "ninety" | |
}, | |
"3": "hundred", | |
"4": "thousand", | |
"7": "million", | |
"10": "billion", | |
"13": "trillion", | |
"16": "quadrillion", | |
"19": "quintillion", | |
"22": "sextillion", | |
"25": "septillion", | |
"28": "octillion", | |
"31": "nonillion", | |
"34": "decillion" | |
} | |
def expansion(n,b): | |
q = n | |
k = 0 | |
a = [] | |
i = len(str(n)) | |
while q != 0: | |
a.append(q % b) | |
q = q / b | |
k += 1 | |
return a | |
def expand_base_10(n): | |
return expansion(n,10) | |
def say_number(n): | |
tens = expand_base_10(n) | |
return say_number_recur(tens, len(str(n))) | |
def say_number_one(tens, ret_val=''): | |
if tens[0] != 0: | |
ret_val += word_map["1"][str(tens[0])] | |
return ret_val | |
return '' | |
def say_number_two(tens, ret_val=''): | |
if tens[1] == 0: | |
return say_number_one(tens[0:len(tens)-1], ret_val) | |
if tens[1] == 1: | |
ret_val += word_map["2"]["1"][str(tens[0])] | |
return ret_val | |
ret_val += word_map["2"][str(tens[1])] + say_number_one(tens[0:len(tens)-1], ' ') | |
return ret_val | |
def say_number_three(tens, ret_val=''): | |
if tens[2] == 0: | |
return say_number_two(tens[0:len(tens)-1], ret_val) | |
ret_val += say_number_one(tens[len(tens)-1:len(tens)]) + ' ' + word_map["3"] + say_number_two(tens[0:len(tens)-1], ' ') | |
return ret_val | |
def say_number_recur(tens, num, ret_val=''): | |
if num == 1: | |
return say_number_one(tens, ret_val) | |
if num == 2: | |
return say_number_two(tens, ret_val) | |
if num == 3: | |
return say_number_three(tens, ret_val) | |
if tens[num-1] == 0: | |
return say_number_recur(tens[0:len(tens)-1], num - 1, ret_val) | |
ret_val += say_number_recur(tens[len(tens)-(num%3 or 3):len(tens)], num%3 or 3) + ' ' + word_map[str(num - (num%3 or 3) + 1)] + say_number_recur(tens[0:len(tens)-(num%3 or 3)], num - (num%3 or 3), ' ') | |
return ret_val | |
def pad(num, length): | |
strnum = str(num) | |
while (len(strnum) < length): | |
strnum = '0' + strnum | |
return strnum | |
# configurable limit | |
limit = 139 | |
# longest = 0 | |
# longlength = 0 | |
# for i in range(1, 1000): | |
# length = len(say_number(i)) | |
# if length > limit: | |
# print i | |
# print say_number(i) | |
# print length | |
# break | |
# if length > longlength: | |
# longest = i | |
# longlength = length | |
# # longest 3-digit is 373 | |
num = 373 | |
while len(say_number(num)) <= limit: | |
num = int(str(num) + str(longest)) | |
if len(say_number(num)) > limit: | |
break | |
for i in range(1,999999): | |
newnum = int(str(pad(i,6)) + str(num)[6:]) | |
length = len(say_number(newnum)) | |
if length > limit: | |
print newnum | |
print say_number(newnum) | |
print length | |
break | |
# # limit = 139 | |
# 1111373373373 | |
# one trillion one hundred eleven billion three hundred seventy three million three hundred seventy three thousand three hundred seventy three | |
# 140 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment