Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Speech Module by Renelvon
FIRST_TEN = ["zero", "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine"]
SECOND_TEN = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen"]
OTHER_TENS = ["twenty", "thirty", "forty", "fifty", "sixty", "seventy",
"eighty", "ninety"]
HUNDRED = "hundred"
def checkio(number):
hundred_s, decade_s, unit_s = '', '', ''
h, d = 0, 0
if number > 99:
h, number = divmod(number, 100)
hundred_s = FIRST_TEN[h] + ' ' + HUNDRED + ' '
if number > 19:
d, number = divmod(number, 10)
decade_s = OTHER_TENS[d - 2] + ' '
elif number > 9:
decade_s = SECOND_TEN[number - 10] + ' '
d, number = 1, 0
if number > 0 or d == 0 and h == 0:
unit_s = FIRST_TEN[number]
final_string = "%s%s%s" %(hundred_s, decade_s, unit_s)
return final_string.rstrip()
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio(4) == 'four', "1st example"
assert checkio(133) == 'one hundred thirty three', "2nd example"
assert checkio(12) == 'twelve', "3rd example"
assert checkio(101) == 'one hundred one', "4th example"
assert checkio(212) == 'two hundred twelve', "5th example"
assert checkio(40) == 'forty', "6th example"
Feb. 13, 2014
Comments: