From 139a0357ff6e4f20cfb639a9dab018518b5b4391 Mon Sep 17 00:00:00 2001 From: Cory Dean Chung Date: Wed, 16 Aug 2023 01:23:06 -0400 Subject: [PATCH] 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. --- numberwords.py | 15 ++++++++++----- test_numberwords.py | 12 ++++++------ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/numberwords.py b/numberwords.py index 819786c..49abfb3 100644 --- a/numberwords.py +++ b/numberwords.py @@ -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. diff --git a/test_numberwords.py b/test_numberwords.py index e40f218..5f4696b 100644 --- a/test_numberwords.py +++ b/test_numberwords.py @@ -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: