Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Speech Module by lukasz.bogaczynski
suffix = [
["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"],
["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen","sixteen", "seventeen", "eighteen", "nineteen"],
["twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"],
["thousand", "hundred"]
]
def checkio(number):
result = []
len_of_number = len(str(number))
for index, digit in enumerate(str(number)):
if digit == "0":
len_of_number = len_of_number -1
continue
if len_of_number < 0:
break
if len_of_number == 1 and digit == '0':
break
if len_of_number > 3:
result.append(suffix[0][int(digit) - 1])
result.append(suffix[3][4 - len_of_number])
elif len_of_number > 2:
result.append(suffix[0][int(digit) - 1])
result.append(suffix[3][4 - len_of_number])
elif len_of_number > 1:
if digit == "0":
len_of_number = len_of_number - 2
continue
if 0 < int(digit) < 2:
result.append(suffix[1][int(str(number)[index + 1])])
len_of_number = len_of_number - 3
continue
else:
result.append(suffix[2][int(digit) - 2])
else:
result.append(suffix[0][int(digit) - 1])
len_of_number = len_of_number - 1
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"
Oct. 21, 2017