Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Speech Module by HonzaKral
# we need all digits
digits = ('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine')
# for teens and tens just store the irregular ones
tens = {2: 'twenty', 3: 'thirty', 4: 'forty', 5: 'fifty', 8: 'eighty'}
teens = {0: 'ten', 1: 'eleven', 2: 'twelve', 3: 'thirteen', 4: 'fourteen', 5: 'fifteen', 8: 'eighteen'}
def checkio(number):
# hundreds
h = number // 100
number -= h * 100
# tens
t = number // 10
number -= t * 10
# singles are left
o = number
out = ''
# hundreds are regular
if h:
out = '%s hundred ' % digits[h]
# check for teen numbers
if t == 1:
return '%s%s' % (out, teens.get(o, '%steen' % digits[o]))
elif t:
out += tens.get(t, '%sty' % digits[t]) + ' '
if o:
out += digits[o]
return out.strip()
if __name__ == '__main__':
assert checkio(4) == 'four', "First"
assert checkio(133) == 'one hundred thirty three', "Second"
assert checkio(12)=='twelve', "Third"
assert checkio(101)=='one hundred one', "Fifth"
assert checkio(212)=='two hundred twelve', 'Sixth'
print('All ok')
Nov. 12, 2012
Comments: