I did a bunch wihout using the dictionary and just using if statements and returning the variables,

but it seemed like it was taking too long / too many lines so then I switched to using a dicitionary
of place values. I think it turned out okay, I was finally able to get at all of the spaces and ands
(at least, the test ran OK, I suppose there could be more oversights that weren't revealed in the tests).
This commit is contained in:
root 2024-12-07 17:34:08 -05:00
parent 7c5907d640
commit 2a8a1c29b8
2 changed files with 122 additions and 10 deletions

View File

@ -15,19 +15,116 @@ TENS_NAMES = [
] ]
def int_under_1000000_to_str(number): def int_under_1000000_to_str(number):
return "umm..." number_by_places = {"100k": "", "10k": "", "1k": "", "100": "", "10": "", "1": ""}
quotient100000, remainder10000 = divide_with_remainder(number, 100000) #123456 = 1, 23456
quotient10000, remainder1000 = divide_with_remainder(remainder10000, 10000) #23456 = 2, 3456
quotient1000, remainder100 = divide_with_remainder(remainder1000, 1000) #3456 = 3, 456
quotient100, remainder10 = divide_with_remainder(remainder100, 100) #456 = 4, 56
quotient10, remainder1 = divide_with_remainder(remainder10, 10) #56 = 5, 6
if number < 1000:
int_under_1000_to_str(number)
return int_under_1000_to_str(number)
if number >= 1000:
#100000s place
if quotient100000 != 0:
number_by_places["100k"] = DIGIT_NAMES[quotient100000] + " hundred"
#10000s place
if quotient10000 == 1:
if quotient100000 != 0:
number_by_places["10k"] = " and " + TWEEN_AND_TEEN_NAMES[quotient1000] + " thousand"
if quotient100000 == 0:
number_by_places["10k"] = TWEEN_AND_TEEN_NAMES[quotient1000] + " thousand"
if quotient10000 != 1:
if quotient10000 != 0:
if quotient100000 != 0:
number_by_places["10k"] = " and " + TENS_NAMES[quotient10000+1]
if quotient100000 == 0:
number_by_places["10k"] = TENS_NAMES[quotient10000+1]
#1000s place
if quotient1000 != 0:
if quotient100 == 0 and quotient10 == 0:
if quotient10000 == 0:
number_by_places["1k"] = DIGIT_NAMES[quotient1000]+" thousand"
if quotient10000 != 0:
number_by_places["1k"] = "-" + DIGIT_NAMES[quotient1000]+" thousand"
if quotient100 != 0:
if quotient10000 == 0:
number_by_places["1k"] = DIGIT_NAMES[quotient1000]+" thousand "
if quotient10000 != 0:
number_by_places["1k"] = "-" + DIGIT_NAMES[quotient1000]+" thousand "
if quotient10 != 0:
if quotient10000 == 0:
number_by_places["1k"] = DIGIT_NAMES[quotient1000]+" thousand "
if quotient10000 != 0:
number_by_places["1k"] = "-" + DIGIT_NAMES[quotient1000]+" thousand "
if remainder1 != 0:
if quotient10000 == 0:
number_by_places["1k"] = DIGIT_NAMES[quotient1000]+" thousand "
if quotient10000 != 0:
number_by_places["1k"] = "-" + DIGIT_NAMES[quotient1000]+" thousand "
#hundreds place
if quotient100 != 0:
number_by_places["100"] = DIGIT_NAMES[quotient100] + " hundred"
#tens place
if quotient10 == 1:
number_by_places["10"] = " and " + TWEEN_AND_TEEN_NAMES[remainder1]
if quotient10 > 1:
number_by_places["10"] = " and " + TENS_NAMES[quotient10]
#ones place
if remainder1 != 0:
if quotient10 > 1:
number_by_places["1"] = "-" + DIGIT_NAMES[remainder1]
if quotient10 == 1:
number_by_places["1"] = ""
if quotient10 == 0:
number_by_places["1"] = DIGIT_NAMES[remainder1]
return number_by_places["100k"]+number_by_places["10k"]+number_by_places["1k"]+number_by_places["100"]+number_by_places["10"]+number_by_places["1"]
def int_under_1000_to_str(number): def int_under_1000_to_str(number):
return "umm..." number_by_places = {"100k": "", "10k": "", "1k": "", "100": "", "10": "", "1": ""}
quotient100, remainder10 = divide_with_remainder(number, 100) #456 = 4, 56
quotient10, remainder1 = divide_with_remainder(remainder10, 10) #56 = 5, 6
if number >= 100:
if quotient100 != 0:
number_by_places["100"] = DIGIT_NAMES[quotient100] + " hundred"
if quotient10 == 1:
number_by_places["10"] = " and " + TWEEN_AND_TEEN_NAMES[remainder1]
if quotient10 > 1:
number_by_places["10"] = " and " + TENS_NAMES[quotient10]
if remainder1 != 0 and quotient10 != 1:
number_by_places["1"] = "-" + DIGIT_NAMES[remainder1]
return number_by_places["100k"]+number_by_places["10k"]+number_by_places["1k"]+number_by_places["100"]+number_by_places["10"]+number_by_places["1"]
if number < 100:
int_under_100_to_str(number)
return int_under_100_to_str(number)
def int_under_100_to_str(number): def int_under_100_to_str(number):
return "umm..." number_by_places = {"100k": "", "10k": "", "1k": "", "100": "", "10": "", "1": ""}
quotient10, remainder1 = divide_with_remainder(number, 10) #56 = 5, 6
if number >= 20:
if quotient10 > 0 and quotient10 != 1:
number_by_places["10"] = TENS_NAMES[quotient10]
if remainder1 != 0 and quotient10 != 1:
number_by_places["1"] = "-" + DIGIT_NAMES[remainder1]
return number_by_places["100k"]+number_by_places["10k"]+number_by_places["1k"]+number_by_places["100"]+number_by_places["10"]+number_by_places["1"]
if number < 20:
int_under_20_to_str(number)
return int_under_20_to_str(number)
def int_under_20_to_str(number): def int_under_20_to_str(number):
return "umm..." number_by_places = {"100k": "", "10k": "", "1k": "", "100": "", "10": "", "1": ""}
quotient10, remainder1 = divide_with_remainder(number, 10)
if number >= 10:
number_by_places["10"] = TWEEN_AND_TEEN_NAMES[remainder1]
return number_by_places["100k"]+number_by_places["10k"]+number_by_places["1k"]+number_by_places["100"]+number_by_places["10"]+number_by_places["1"]
if number < 10:
number_by_places["1"] = DIGIT_NAMES[number]
return number_by_places["100k"]+number_by_places["10k"]+number_by_places["1k"]+number_by_places["100"]+number_by_places["10"]+number_by_places["1"]
def int_under_10_to_str(number): def int_under_10_to_str(number):
return "umm..." number_by_places = {"100k": "", "10k": "", "1k": "", "100": "", "10": "", "1": ""}
number_by_places["1"] = DIGIT_NAMES[number]
return number_by_places["100k"]+number_by_places["10k"]+number_by_places["1k"]+number_by_places["100"]+number_by_places["10"]+number_by_places["1"]
def divide_with_remainder(dividend, divisor): def divide_with_remainder(dividend, divisor):
"""Divides one number by another, using whole-number division. """Divides one number by another, using whole-number division.
@ -36,3 +133,5 @@ def divide_with_remainder(dividend, divisor):
quotient = dividend // divisor quotient = dividend // divisor
remainder = dividend % divisor remainder = dividend % divisor
return quotient, remainder return quotient, remainder
int_under_1000000_to_str(10000)

View File

@ -37,19 +37,32 @@ return digit_names[number]
## Integers under 20 ## Integers under 20
If the integer is under 10, then use the procedure described above. If the integer is under 10, then use the procedure described above.
Otherwise, ... (this is where you take over!) Otherwise, ... (this is where you take over!)
make a dictionary with 100k, 10k, 1k, 100, 10, 1 places
add names to 1s, 10s place in dictionary
## Integers under 100 ## Integers under 100
if number < 20:
integers under 20
if number >= 20:
add names to 1s, 10s places in dictionary
## Integers under 1000 ## Integers under 1000
if number < 100:
integers under 100
if number >= 100:
add names to 1s, 10s, 100s places in dictionary
## Integers under 1000000 ## Integers under 1000000
if number < 1000:
integers under 1000
if number >= 1000:
add names to 1s, 10s, 100s, 1k, 10k, 100k place in dictionary
## Negative integers down to -1 million ## Negative integers down to -1 million
We won't deal with negative integers in this problem set, We won't deal with negative integers in this problem set,
but how would you deal with a negative integer, using the but how would you deal with a negative integer, using the
functions above? functions above?
if number < 0:
add "negative" to dictionary (add a sign entry to the front)