Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Speech Module by eiichi
import itertools
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):
if number is None:
return None
print(number)
number_list = number_to_list_with_padding(number)
FFIRST_AND_SECOND_TEN = [""]
FFIRST_AND_SECOND_TEN.extend(FIRST_TEN)
FFIRST_AND_SECOND_TEN.extend(SECOND_TEN)
print(FFIRST_AND_SECOND_TEN)
HUNDREDS = [""]
HUNDREDS.extend(list(itertools.repeat(HUNDRED, 9)))
print(HUNDREDS)
if 0 <= number_list[1] and number_list[1] <= 1:
num_10p_and_1p_words = FFIRST_AND_SECOND_TEN[number_list[1]*10 + number_list[0]]
elif 2 <= number_list[1] and number_list[1] <= 9:
num_10p_and_1p_words = OTHER_TENS[number_list[1] - 2] + " " + FFIRST_AND_SECOND_TEN[number_list[0]]
num_100p_words = FFIRST_AND_SECOND_TEN[number_list[2]] + " " + HUNDREDS[number_list[2]]
num_words = num_100p_words + " " + num_10p_and_1p_words
print(num_words.strip())
return num_words.strip()
def number_to_list_with_padding(number):
number_list_without_padding = to_list(number)
print(number_list_without_padding)
number_list = list(itertools.repeat(0, 3 - len(number_list_without_padding)))
number_list.extend(number_list_without_padding)
number_list.reverse()
print(number_list)
return number_list
def to_list(number):
if number == 0:
return []
else:
x = number % 10
list = to_list(int((number - x)/10))
list.append(x)
return list
#Some hints
#Don't forget strip whitespaces at the end of string
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
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"
Feb. 17, 2014