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
# --------------
# By MWC Contributors
# Functions to print out a verbal representation of an integer.
MAXIMUM = 1000000
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"
]
TENS_NAMES = [
"ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"
]
def int_under_1000000_to_str(number):
"Returns a textual representation of the number."
check_number_in_range(abs(number), 0, MAXIMUM)
if number < 1000:
return int_under_1000_to_str(number)
else:
thousands, hundreds = divide_with_remainder(number, 1000)
thousands_text = int_under_1000_to_str(thousands)
hundreds_text = int_under_1000_to_str(hundreds)
return thousands_text + " thousand " + hundreds_text
def int_under_1000_to_str(number):
"Returns a textual representation of the number"
check_number_in_range(number, 0, 1000)
if number < 100:
return int_under_100_to_str(number)
else:
hundreds, tens = divide_with_remainder(number, 100)
hundreds_text = int_under_10_to_str(hundreds)
tens_text = int_under_100_to_str(tens)
return hundreds_text + " hundred and " + tens_text
def int_under_100_to_str(number):
check_number_in_range(number, 0, 100)
tens, ones = divide_with_remainder(number, 10)
if tens == 0:
return int_under_10_to_str(number)
elif tens == 1:
return TWEEN_AND_TEEN_NAMES[ones]
else:
return TENS_NAMES[tens] + '-' + int_under_10_to_str(ones)
def int_under_10_to_str(number):
check_number_in_range(number, 0, 10)
return DIGIT_NAMES[number]
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
# numberwords.py
# --------------
# By MWC Contributors
# Functions to print out a verbal representation of an integer.
MAXIMUM = 1000000
MINIMUM = -1000000
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"
]
TENS_NAMES = [
"null", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"
]
def negative(number):
number_str = str(number)
if number_str[0] == "-":
return True
else:
return False
def int_under_1000000_to_str(number):
"Returns a textual representation of the number."
global original
original = str(number)
is_negative = negative(number)
if is_negative:
temp = original
original = temp[1:]
negative_msg = "negative "
else:
negative_msg = ""
if is_negative:
temp = abs(number)
number = temp
check_number_in_range(number, 0, MAXIMUM)
if number < 1000:
return int_under_1000_to_str(number, is_negative)
else:
thousands, hundreds = divide_with_remainder(number, 1000)
thousands_text = int_under_1000_to_str(thousands, False)
hundreds_text = int_under_1000_to_str(hundreds, False)
return negative_msg + thousands_text + " thousand " + hundreds_text
def int_under_1000_to_str(number, is_negative):
"Returns a textual representation of the number"
if is_negative:
temp = abs(number)
number = temp
negative_msg = "negative "
else:
negative_msg = ""
check_number_in_range(number, 0, 1000)
if number < 100:
return int_under_100_to_str(number, is_negative)
else:
hundreds, tens = divide_with_remainder(number, 100)
hundreds_text = int_under_10_to_str(hundreds, False)
tens_text = int_under_100_to_str(tens, False)
return negative_msg + hundreds_text + " hundred and " + tens_text
def int_under_100_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, 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