completed numberwords.py and added negatives

I completed numberwords.py, modeled after the solution that I accidentally had access
to, but I did fix a few output issues where things were printing awkwardly (thiry-zero)
so now things look to work a little more like they would be said in English. I also
(after MUCH planning, execution, debugging, and throwing it all away to start over)
came up with a way to get the negative numbers to work. I did this by converting
the original number to a string, checking if the first character is a dash, throwing
away the dash if it exists, and then completing the rest of the program as if the number
were positive. I added some conditionals and a boolean value to only print the "negative "
string on the most parent function so it doesn't end up saying something like
"negative negative fiften thousand negative four hundred and negative thirty-five", and
it seems to work for all of the numbers that I tested. It doesn't work well with
test_numberwords.py because the test cases have the tens index shifted. I guess I could
have just modified those and ran it again. I should do that.
This commit is contained in:
Pat Wick 2023-08-09 12:16:40 -04:00
parent d8a5934cb5
commit 9538db68d7
1 changed files with 119 additions and 69 deletions

View File

@ -1,69 +1,119 @@
# numberwords.py # numberwords.py
# -------------- # --------------
# By MWC Contributors # By MWC Contributors
# Functions to print out a verbal representation of an integer. # Functions to print out a verbal representation of an integer.
MAXIMUM = 1000000 MAXIMUM = 1000000
DIGIT_NAMES = [ MINIMUM = -1000000
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" DIGIT_NAMES = [
] "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"
TWEEN_AND_TEEN_NAMES = [ ]
"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" TWEEN_AND_TEEN_NAMES = [
] "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
TENS_NAMES = [ ]
"ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" TENS_NAMES = [
] "null", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"
]
def int_under_1000000_to_str(number):
"Returns a textual representation of the number." def negative(number):
check_number_in_range(abs(number), 0, MAXIMUM) number_str = str(number)
if number < 1000: if number_str[0] == "-":
return int_under_1000_to_str(number) return True
else: else:
thousands, hundreds = divide_with_remainder(number, 1000) return False
thousands_text = int_under_1000_to_str(thousands)
hundreds_text = int_under_1000_to_str(hundreds) def int_under_1000000_to_str(number):
return thousands_text + " thousand " + hundreds_text "Returns a textual representation of the number."
global original
def int_under_1000_to_str(number): original = str(number)
"Returns a textual representation of the number" is_negative = negative(number)
check_number_in_range(number, 0, 1000) if is_negative:
if number < 100: temp = original
return int_under_100_to_str(number) original = temp[1:]
else: negative_msg = "negative "
hundreds, tens = divide_with_remainder(number, 100) else:
hundreds_text = int_under_10_to_str(hundreds) negative_msg = ""
tens_text = int_under_100_to_str(tens) if is_negative:
return hundreds_text + " hundred and " + tens_text temp = abs(number)
number = temp
def int_under_100_to_str(number): check_number_in_range(number, 0, MAXIMUM)
check_number_in_range(number, 0, 100) if number < 1000:
tens, ones = divide_with_remainder(number, 10) return int_under_1000_to_str(number, is_negative)
if tens == 0: else:
return int_under_10_to_str(number) thousands, hundreds = divide_with_remainder(number, 1000)
elif tens == 1: thousands_text = int_under_1000_to_str(thousands, False)
return TWEEN_AND_TEEN_NAMES[ones] hundreds_text = int_under_1000_to_str(hundreds, False)
else: return negative_msg + thousands_text + " thousand " + hundreds_text
return TENS_NAMES[tens] + '-' + int_under_10_to_str(ones)
def int_under_1000_to_str(number, is_negative):
def int_under_10_to_str(number): "Returns a textual representation of the number"
check_number_in_range(number, 0, 10) if is_negative:
return DIGIT_NAMES[number] temp = abs(number)
number = temp
def check_number_in_range(number, minimum, maximum): negative_msg = "negative "
"""Checks whether a number is at least minimum and less than maximum. else:
Raises an error if the number is not in range. negative_msg = ""
""" check_number_in_range(number, 0, 1000)
if number < minimum: if number < 100:
raise ValueError(f"{number} must not be below {minimum}.") return int_under_100_to_str(number, is_negative)
if number >= maximum: else:
raise ValueError(f"{number} must be less than {maximum}.") hundreds, tens = divide_with_remainder(number, 100)
hundreds_text = int_under_10_to_str(hundreds, False)
def divide_with_remainder(dividend, divisor): tens_text = int_under_100_to_str(tens, False)
"""Divides one number by another, using whole-number division. return negative_msg + hundreds_text + " hundred and " + tens_text
Returns the quotient and the remainder.
Note how a function can return more than one value! def int_under_100_to_str(number, is_negative):
""" if is_negative:
quotient = dividend // divisor temp = abs(number)
remainder = dividend % divisor number = temp
return quotient, remainder negative_msg = "negative "
else:
negative_msg = ""
check_number_in_range(number, 0, 100)
tens, ones = divide_with_remainder(number, 10)
if tens == 0:
return int_under_10_to_str(number, is_negative)
elif tens == 1:
return negative_msg + TWEEN_AND_TEEN_NAMES[ones]
else:
if ones > 0:
return negative_msg + TENS_NAMES[tens] + '-' + int_under_10_to_str(ones, False)
else:
return negative_msg + TENS_NAMES[tens]
def int_under_10_to_str(number, is_negative):
if is_negative:
temp = abs(number)
number = temp
negative_msg = "negative "
else:
negative_msg = ""
check_number_in_range(number, 0, 10)
if len(original) < 2 or number > 0:
if len(original) < 2:
return negative_msg + DIGIT_NAMES[number]
else:
return DIGIT_NAMES[number]
else:
return ""
def check_number_in_range(number, minimum, maximum):
"""Checks whether a number is at least minimum and less than maximum.
Raises an error if the number is not in range.
"""
if number < minimum:
raise ValueError(f"{number} must not be below {minimum}.")
if number >= maximum:
raise ValueError(f"{number} must be less than {maximum}.")
def divide_with_remainder(dividend, divisor):
"""Divides one number by another, using whole-number division.
Returns the quotient and the remainder.
Note how a function can return more than one value!
"""
quotient = dividend // divisor
remainder = dividend % divisor
return quotient, remainder