Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Speech Module by macfreek
# migrated from python 2.7
names = {
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",
13: "thirteen",
14: "fourteen",
15: "fifteen",
16: "sixteen",
17: "seventeen",
18: "eighteen",
19: "nineteen",
20: "twenty",
30: "thirty",
40: "forty",
50: "fifty",
60: "sixty",
70: "seventy",
80: "eighty",
90: "ninety",
}
exponents = {
2: "hundred",
3: "thousand",
6: "million",
9: "billion", # (short scale). Long scale: thousand million or milliard
12: "trillion", # (short scale). Long scale: billion
15: "quadrillion", # (short scale). Long scale: thousand billion or billiard
18: "quintillion", # (short scale). Long scale: trillion
21: "sextillion", # (short scale). Long scale: thousand trillion or trilliard
100: "googol", # short scale: duotrigintillion; long scale: ten thousand sexdecillion
}
def checkio(num):
'string representation of num, simplified (no punctiation)'
if num in names:
return names[num]
if num < 0:
return "minus "+checkio(-num)
if num < 100: # 21 ... 99
tens, ones = divmod(num, 10)
return names[10*tens] + " " + names[ones]
if num < 1000: # 101 ... 999
print("**")
hundreds, ones = divmod(num, 100)
if ones == 0:
return names[hundreds] + " " + exponents[2]
else:
return names[hundreds] + " " + exponents[2] + " " + checkio(ones)
if num == 1000:
return names[1] + " " + exponents[3]
for exp in [3,6,9,12,15,18,21]:
if num < (10 ** (exp+3)):
base, ones = divmod(num, (10 ** exp))
if ones == 0:
return checkio(base) + " " + exponents[exp]
elif ones < 100:
return checkio(base) + " " + exponents[exp] + " " + checkio(ones)
else:
return checkio(base) + " " + exponents[exp] + " " + checkio(ones)
digits = [int(digit) for digit in str(num)]
return " ".join([names[digit] for digit in digits])
raise Exception
def numToBritish(num):
"""Number to English text, now with propert British punctiation.
Taken from my solution for Project Euler #17."""
if num in names:
return names[num]
if num < 0:
return "minus "+numToBritish(-num)
if num < 100: # 21 ... 99
tens, ones = divmod(num, 10)
return names[10*tens] + "-" + names[ones]
if num < 1000: # 101 ... 999
hundreds, ones = divmod(num, 100)
if ones == 0:
return names[hundreds] + " " + exponents[2]
else:
return names[hundreds] + " " + exponents[2] + " and " + numToBritish(ones)
if num == 1000:
return names[1] + " " + exponents[3]
for exp in [3,6,9,12,15,18,21]:
if num < (10 ** (exp+3)):
base, ones = divmod(num, (10 ** exp))
if ones == 0:
return numToBritish(base) + " " + exponents[exp]
elif ones < 100:
return numToBritish(base) + " " + exponents[exp] + " and " + numToBritish(ones)
else:
return numToBritish(base) + " " + exponents[exp] + ", " + numToBritish(ones)
digits = [int(digit) for digit in str(num)]
return " ".join([names[digit] for digit in digits])
raise Exception
if __name__ == '__main__':
assert checkio(4) == 'four', "First"
assert checkio(133) == 'one hundred thirty three', "Second"
assert checkio(12)=='twelve', "Third"
assert checkio(101)=='one hundred one', "Fifth"
assert checkio(212)=='two hundred twelve', "Sixth"
assert checkio(999)=='nine hundred ninety-nine', "Sixth"
assert checkio(40)=='forty', "Seventh, forty - it is correct"
print('All ok')
Dec. 10, 2012