Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Speech Module by robinw
def checkio(number):
d0={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'}
d1={2: 'twenty', 3:'thirty', 4: 'forty', 5: 'fifty', 6: 'sixty', 7:'seventy', 8: 'eighty', 9: 'ninety'}
if number == 0:
return ""
if number < 20:
return (d0[number])
elif number < 100:
x = divmod(number, 10)
return ((d1[x[0]]+ ' ' +checkio(x[1])).strip())
elif number < 1000:
x = divmod(number, 100)
hundred = " hundred "
return ((d0[x[0]]+hundred+checkio(x[1])).strip()) #strip for removing whitespaces
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 checkio(212).endswith(' '), "Don't forget strip whitespaces at the end of string"
Sept. 25, 2014