Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Speech Module by elka-s
'''FIRST_TEN = ["one", "two", "three", "four", "five", "six", "seven",
"eight", "nine"]
SECOND_TEN = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen"]
OTHER_TENS = ["twenty", "thirty", "forty", "fifty", "sixty", "seventy",
"eighty", "ninety"]
HUNDRED = "hundred"
'''
def checkio(num):
ans = []
#100 - 900
huns = num // 100
#20 - 90
ties = (num % 100) // 10
#1-9
ones = num % 10
teens = 0
if ties == 1:
teens = ties * 10 + ones
ones = 0
ones_speech = {1 : 'one', 2 : 'two', 3 : 'three', 4 : 'four', 5 : 'five', 6 : 'six', 7 : 'seven', 8 : 'eight', 9 : 'nine'}
ties_speech = {2: 'twenty', 3 : 'thirty', 4 : 'forty', 5 : 'fifty', 6 : 'sixty', 7 : 'seventy', 8 : 'eighty', 9 : 'ninety'}
huns_speech = {1 : 'one hundred', 2 : 'two hundred', 3 : 'three hundred', 4 : 'four hundred', 5 : 'five hundred', 6 : 'six hundred', 7 : 'seven hundred', 8 : 'eight hundred', 9 : 'nine hundred'}
teens_speech = {10 : 'ten', 11 : 'eleven', 12 : 'twelve', 13 : 'thirteen', 14 : 'fourteen', 15 : 'fifteen', 16 : 'sixteen', 17 : 'seventeen', 18 : 'eighteen', 19 : 'nineteen'}
if huns in huns_speech:
ans.append(huns_speech[huns])
if teens in teens_speech:
ans.append(teens_speech[teens])
if ties in ties_speech:
ans.append(ties_speech[ties])
if ones in ones_speech:
ans.append(ones_speech[ones])
ans = ' '.join(ans)
return ans
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"
June 5, 2015
Comments: