Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Speech Module solution in Clear category for Speech Module by resilience
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(number):
hundreds = (number // 100) % 10
tens = (number) % 100
return (hundredsString(hundreds) + tensString(tens)).strip()
def hundredsString(hundreds):
if (0 == hundreds):
return ""
else:
return FIRST_TEN[hundreds-1] + " hundred "
def tensString(tens):
if (0 == tens):
return ""
elif (tens >= 20):
return convertTensDigit(tens) + " " + convertOnesDigit(tens % 10)
elif (tens >= 10 and tens <= 19):
return convertTeens(tens)
else:
return convertOnesDigit(tens % 10)
def convertTensDigit(tens):
tensDigit = tens // 10
if (0 == tensDigit):
return ""
else:
return OTHER_TENS[tensDigit - 2]
def convertTeens(tens):
onesDigit = tens % 10
return SECOND_TEN[onesDigit]
def convertOnesDigit(ones):
if (0 == ones):
return ""
else:
return FIRST_TEN[ones - 1]
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"
Aug. 6, 2015