Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Speech Module by nsmirnoff
first_ten = ["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(num):
first_tens = lambda num: first_ten[num - 1]
second_tens = lambda num: second_ten[num - 10]
tens = lambda num: other_tens[num // 10 - 2]
hundreds = lambda num: first_ten[num // 100 - 1] + ' ' + hundred
if num == 0: return 'zero'
elif 0 < num < 10: return first_tens(num) # 1 - 9
elif 10 <= num < 20: return second_tens(num) # 10 - 19
elif 20 <= num <= 90 and num % 10 == 0: return tens(num) # tens: 20, 30 ...
elif num < 100: # 20 - 99
return ' '.join([tens(num), first_tens(num % 10)])
elif num % 100 == 0: return hundreds(num) # hundreds: 100, 200 ...
else: return ' '.join([hundreds(num), checkio(num % 100)])
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
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"
assert not checkio2(12).endswith(' '), "Don't forget strip whitespaces at the end of string"
Feb. 27, 2015