Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Speech Module - Brute force version solution in Uncategorized category for Speech Module by Antoni_Wojcik
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(num: int) -> str:
if len(str(num)) < 2:
n = num % 10
return FIRST_TEN[n-1]
elif len(str(num)) > 1 and len(str(num)) < 3:
if num >= 20:
if int(str(num)[1]) == 0:
n = num // 10
return OTHER_TENS[n-2]
ten = str(num)[0]
last = str(num)[1]
ten = OTHER_TENS[int(ten)-2]
last = FIRST_TEN[int(last)-1]
return ten + " " + last
else:
n = num % 10
return SECOND_TEN[n]
elif len(str(num)) > 2 and len(str(num)) < 4:
hun = num // 100
n = int((str(num)[1:]))
if n < 20:
if int(str(num)[1]) == 0 and int(str(num)[2]) == 0:
return FIRST_TEN[hun-1] + " " + HUNDRED
if int(str(num)[1]) == 0 and int(str(num)[2]) != 0:
return FIRST_TEN[hun-1] + " " + HUNDRED + " " + FIRST_TEN[int(str(num)[2])-1]
n = n % 10
ten = SECOND_TEN[n]
return FIRST_TEN[hun-1] + " " + HUNDRED + " " + ten
else:
if int(str(num)[1]) != 0 and int(str(num)[2]) == 0:
return FIRST_TEN[hun-1] + " " + HUNDRED + " " + OTHER_TENS[int(str(num)[1])-2]
ten = str(num)[1]
last = str(num)[2]
ten = OTHER_TENS[int(ten)-2]
last = FIRST_TEN[int(last)-1]
return FIRST_TEN[hun-1] + " " + HUNDRED + " " + ten + " " + last
print("Example:")
print(checkio(940))
assert checkio(1) == "one"
assert checkio(2) == "two"
assert checkio(3) == "three"
assert checkio(4) == "four"
assert checkio(5) == "five"
assert checkio(6) == "six"
assert checkio(9) == "nine"
assert checkio(10) == "ten"
assert checkio(11) == "eleven"
assert checkio(12) == "twelve"
assert checkio(13) == "thirteen"
assert checkio(14) == "fourteen"
assert checkio(15) == "fifteen"
assert checkio(16) == "sixteen"
assert checkio(17) == "seventeen"
assert checkio(18) == "eighteen"
assert checkio(19) == "nineteen"
assert checkio(999) == "nine hundred ninety nine"
assert checkio(784) == "seven hundred eighty four"
assert checkio(777) == "seven hundred seventy seven"
assert checkio(88) == "eighty eight"
assert checkio(44) == "forty four"
assert checkio(20) == "twenty"
assert checkio(30) == "thirty"
assert checkio(40) == "forty"
assert checkio(50) == "fifty"
assert checkio(80) == "eighty"
assert checkio(90) == "ninety"
assert checkio(100) == "one hundred"
assert checkio(200) == "two hundred"
assert checkio(300) == "three hundred"
assert checkio(600) == "six hundred"
assert checkio(700) == "seven hundred"
assert checkio(900) == "nine hundred"
assert checkio(21) == "twenty one"
assert checkio(312) == "three hundred twelve"
assert checkio(302) == "three hundred two"
assert checkio(509) == "five hundred nine"
assert checkio(753) == "seven hundred fifty three"
assert checkio(940) == "nine hundred forty"
assert checkio(999) == "nine hundred ninety nine"
print("The mission is done! Click 'Check Solution' to earn rewards!")
Dec. 1, 2022
Comments: