Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
One-line POC (Proof Of Concept) solution in Creative category for Speech Module by JeromeJ
def checkio(number):
# I wanted to take a special approach approach with this function and make a POC of the less ligne as possible
# Turning them into dict so that we can use its .get() method and never have some IndexError
spe = dict(enumerate(('', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen')))
decades = dict(enumerate(('', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety')))
# [::-1] so that the key for the unit is always 0
# [-2:] ignore the hundred if necessary
return (lambda r: r if r else 'zero')(' '.join(filter(lambda p: p, [(spe.get(d.get(2)), 'hundred'*bool(d.get(2)), decades.get(d.get(1)), spe.get(d.get(0)+10*(10<=int(str(number)[-2:])<20))) for d in (dict(enumerate(map(int, str(number)[::-1]))),)][0])))
# Note: spe and decades could also have been included in the one line but I prefer it this way. Note2: I'm sure this could be shortened again :p
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"
assert checkio(40)=='forty', "Seventh, forty - it is correct"
print('All ok')
Nov. 9, 2012
Comments: