Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Speech Module by senth
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):
(hun, tens) = divmod(number, 100)
(tens, ones) = divmod(tens, 10)
#Initiating the variables
hun_str, tens_str, ones_str = '', '', ''
#Find hundreds if any
if hun:
print(hun)
hun_str = FIRST_TEN[hun - 1] + ' ' + HUNDRED
#Find second tens if any
if tens and tens == 1:
tens_str = SECOND_TEN[ones]
#Find other tens if any
if tens and tens >= 2:
tens_str = OTHER_TENS[tens - 2]
#Find ones (only when it's not second tens) if any
if ones and tens != 1:
ones_str = FIRST_TEN[ones - 1]
#Join them as string with spaces
number_as_string = ' '.join((hun_str, tens_str, ones_str))
# Replace 2 or more spaces in-between the string with just one space
from re import sub
number_as_string = sub(' {2,}', ' ', number_as_string)
#return the string after stripping of whitespaces from the beginning or end of line
return number_as_string.strip()
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"
Feb. 3, 2016