Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Speech Module by amigozz
array119 = {'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', '0':''}
array2090 = {'2':'twenty', '3':'thirty', '4': 'forty', '5':'fifty', '6':'sixty', '7':'seventy', '8':'eighty', '9':'ninety', '0':''}
def checkio(num):
string = str(num)
if num < 20:
return array119[string]
elif ( (num > 19) & (num < 100) ):
return ( array2090[string[0]] + ' ' + array119[string[1]] ).rstrip()
elif( (num > 99) & (string[1] == '1') ):
return (array119[string[0]] + ' hundred ' + array119[string[1] + string[2]]).rstrip()
elif((num > 99) & (string[1] == '0')):
return (array119[string[0]] + ' hundred ' + array2090[string[1]] + array119[string[2]]).rstrip()
else:
return (array119[string[0]] + ' hundred ' + array2090[string[1]] + ' ' + array119[string[2]]).rstrip()
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"
Dec. 29, 2014