python-checkio/ElectronicStation/speechmodule.py
2019-12-18 17:30:47 +01:00

40 lines
1.4 KiB
Python

def checkio(number):
toStr = ""
hasSpecials = False
numbers = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
deci = {1: "ten", 2: "twenty", 3: "thirty", 4: "forty", 5: "fifty", 6: "sixty", 7: "seventy", 8: "eighty",
9: "ninety"}
specials = {11: "eleven", 12: "twelve", 13: "thirteen", 14: "fourteen", 15: "fifteen", 16: "sixteen",
17: "seventeen", 18: "eighteen", 19: "nineteen"}
isHundred = number / 100
isDeci = (number % 100) / 10
isNum = ((number % 100) % 10)
if isHundred >= 1:
toStr += numbers[isHundred] + " hundred"
if isDeci == 1 and (isNum != 0):
if toStr:
toStr += " "
toStr += specials[isDeci * 10 + isNum]
hasSpecials = True
elif isDeci > 1:
if toStr:
toStr += " "
toStr += str(deci[int(isDeci)])
if hasSpecials == False and isNum != 0:
if toStr:
toStr += " "
toStr += numbers[isNum]
return toStr
if __name__ == '__main__':
assert checkio(4) == 'four', "First"
assert checkio(133) == 'one hundred thirty three', "Second"
assert checkio(12) == 'twelve', "Third"
assert checkio(101) == 'one hundred one', "Fifth"
assert checkio(212) == 'two hundred twelve', "Sixth"
assert checkio(40) == 'forty', "Seventh, forty - it is correct"
print('All ok')