Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Speech Module Second solution in Clear category for Speech Module by Poy
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):
#sec way
speech = []
calnum = number
while calnum > 0:
if calnum >=100 :
speech.append(FIRST_TEN[int(str(calnum)[0])-1])
speech.append(HUNDRED)
calnum = calnum - ((int(calnum/100))*100)
elif calnum >= 20:
speech.append(OTHER_TENS[int(str(calnum)[0])-2])
calnum = calnum - ((int(calnum/10))*10)
elif calnum >= 10:
speech.append(SECOND_TEN[int(str(calnum)[1])])
calnum = 0
else :
speech.append(FIRST_TEN[int(str(calnum)[0])-1])
calnum = 0
"""
#first way
speech = []
sort = []
for i in range(len(str(number))):
zero = len(str(number))-i-1
sort.append ( str(number)[i] + "0"*zero )
#print (sort)
for s in range(len(sort)):
if int(sort[s]) >= 100:
speech.append(FIRST_TEN[int(sort[s][0])-1])
speech.append(HUNDRED)
if int(sort[s]) >= 10 and int(sort[s]) <20:
speech.append(SECOND_TEN[int(str(int(sort[s])+int(sort[s+1]))[1])])
break
elif int(sort[s]) >= 20 and int(sort[s]) <100:
speech.append(OTHER_TENS[int(sort[s][0])-2])
if int(sort[s])<10 and int(sort[s]) > 0:
speech.append(FIRST_TEN[int(sort[s][0])-1])
"""
return " ".join(speech)
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"
Dec. 9, 2015
Comments: