Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
It takes a "while" (lol) solution in Clear category for Speech Module by BootzenKatzen
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"
# doing a simple while loop and removing the first number as we go
# tried to reduce the amount of int(str(num)) typing I had to do by creating additional functions
# the basics of each step is to check what place we're at, 100s, 10s, 1s
# then use the digit pulled from that place to index the text version from the lists above
# and add it to our final string
# then it removes the place we were just looking at, and goes to the next - from 100s to 10s, or 10s to 1s
# on the 10s and 1s, it also checks to see if there's already number information to see if it needs to add a space
def checkio(num: int) -> str:
name = ''
while num:
if len(str(num)) == 3:
name += FIRST_TEN[get_part(num, 0) - 1] + " " + HUNDRED
num = down(num)
if len(str(num)) == 2:
if get_part(num, 0) > 1:
if name != "":
name += " "
name += OTHER_TENS[get_part(num, 0) - 2]
num = down(num)
elif get_part(num, 0) == 1:
if name != "":
name += " "
name += SECOND_TEN[get_part(num, 1)]
num = None
if len(str(num)) == 1:
if get_part(num, 0) == 0:
num = None
else:
if name != "":
name += " "
name += FIRST_TEN[get_part(num, 0) - 1]
num = None
return name
def get_part(num, place):
"""
This function is to return the digit in the desired number place
Turns it into a string to be able to index, then returns that indexed digit as an integer
"""
return int(str(num)[place])
def down(num):
"""
This function is just to quickly remove the first digit of our number to continue the while loop
Turns it into a string for indexing, then returns the shortened integer
"""
return int(str(num)[1::])
April 25, 2024
Comments: