Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Speech Module by Moff
def get_word_digit(i):
return {0: '', 1: 'one', 2: 'two', 3: 'three', 4: 'four',
5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine'}.get(i)
def get_word_10_19(i):
return {10: 'ten', 11: 'eleven', 12: 'twelve', 13: 'thirteen', 14: 'fourteen',
15: 'fifteen', 16: 'sixteen', 17: 'seventeen', 18: 'eighteen', 19: 'nineteen'}.get(i)
def get_word_tens(i):
return {2: 'twenty', 3: 'thirty', 4: 'forty', 5: 'fifty',
6: 'sixty', 7: 'seventy', 8: 'eighty', 9: 'ninety'}.get(i)
def int_to_words(num):
if num < 10:
return get_word_digit(num)
elif num < 20:
return get_word_10_19(num)
elif num < 100:
tens = get_word_tens(num // 10)
decimals = get_word_digit(num % 10)
if decimals:
return '{} {}'.format(tens, decimals)
else:
return tens
elif num < 1000:
res = '{} hundred'.format(get_word_digit(num // 100))
if num % 100:
res += ' {}'.format(int_to_words(num % 100))
return res
elif num == 1000:
return 'one thousand'
else:
return ''
def checkio(number):
return int_to_words(number)
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"
July 16, 2015