I finished the problem set!!

I did change the test_numberwords.py since the words for the tens
place didn't actually match the number, e.g. 43 was originally
written as "fifty-three" there. It was interesting to see the test
cases. I tried to test some of my own numbers as I was writing, but
I missed the case of numbers ending in zero. 60 becomes
"sixty-zero", which matches what test_numberwords.py was looking for,
but in real life people would be puzzled by the extra "-zero." This
higlights for me an important point about making sure cases to be
tested cover all the sort of edge cases.
This commit is contained in:
Cory Dean Chung 2023-08-16 01:23:06 -04:00
parent 2959bfc4e9
commit 139a0357ff
2 changed files with 16 additions and 11 deletions

View File

@ -15,12 +15,17 @@ TENS_NAMES = [
]
def int_under_1000000_to_str(number):
pass
check_number_in_range(number, 0, 1000000)
if number < 1000:
return int_under_1000_to_str(number)
return int_under_1000_to_str(number // 1000) + " thousand " + int_under_1000_to_str(number % 1000)
def int_under_1000_to_str(number):
pass
check_number_in_range(number, 0, 1000)
if number < 100:
return int_under_100_to_str(number)
return DIGIT_NAMES[number // 100] + " hundred and " + int_under_100_to_str(number % 100)
def int_under_100_to_str(number):
check_number_in_range(number, 0, 100)
@ -39,9 +44,9 @@ def check_number_in_range(number, minimum, maximum):
Raises an error if the number is not in range.
"""
if number < minimum:
raise ValueError("{number} must not be below {minimum}.")
raise ValueError(f"{number} must not be below {minimum}.")
if number >= maximum:
raise ValueError("{number} must be less than {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.

View File

@ -12,17 +12,17 @@ test_cases = [
[11, 'eleven'],
[15, 'fifteen'],
[18, 'eighteen'],
[43, 'fifty-three'],
[60, 'seventy-zero'],
[89, 'ninety-nine'],
[43, 'forty-three'],
[60, 'sixty-zero'],
[89, 'eighty-nine'],
[100, 'one hundred and zero'],
[212, 'two hundred and twelve'],
[755, 'seven hundred and sixty-five'],
[755, 'seven hundred and fifty-five'],
[1000, 'one thousand zero'],
[1001, 'one thousand one'],
[1672, 'one thousand six hundred and eighty-two'],
[1672, 'one thousand six hundred and seventy-two'],
[10000, 'ten thousand zero'],
[588567, 'five hundred and ninety-eight thousand five hundred and seventy-seven'],
[588567, 'five hundred and eighty-eight thousand five hundred and sixty-seven'],
]
for int_input, expected_output in test_cases: