Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
2 Solutions: 1. using num2words (not allowed, here) 2. a longer one solution in Clear category for Speech Module by H0r4c3
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"
#from num2words import num2words
def checkio(number):
result = num2words(number)
result = result.replace('-', ' ').replace('and', '').replace(' ', ' ')
return result
def checkio(number):
result = []
def three_digits(number):
result.append(FIRST_TEN[(number // 100) - 1])
result.append(HUNDRED)
number = number % 100
return number
def two_digits_20(number):
result.append(OTHER_TENS[(number // 10) - 2])
number = number % 10
return number
def two_digits_10(number):
result.append(SECOND_TEN[number - 10])
number = 0
return number
def one_digit(number):
result.append(FIRST_TEN[number - 1])
return number
if 100 <= number < 1000:
number = three_digits(number)
if 20 <= number <= 99:
number = two_digits_20(number)
elif 10 <= number <= 19:
number = two_digits_10(number)
if 1 <= number <= 9:
number = one_digit(number)
return ' '.join(result)
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"
print('Done! Go and Check it!')
Jan. 9, 2022