Updating with the correct starter code

This commit is contained in:
Chris Proctor 2023-08-11 14:59:23 -04:00
parent e4f8546046
commit bd88ab0514
2 changed files with 46 additions and 69 deletions

View File

@ -11,58 +11,27 @@ 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"
"", "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
return "umm..."
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
return "umm..."
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)
return "umm..."
def int_under_20_to_str(number):
return "umm..."
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}.")
return "umm..."
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!
Returns the quotient and the remainder.
"""
quotient = dividend // divisor
remainder = dividend % divisor

View File

@ -3,35 +3,43 @@
# By MWC Contributors
# Run this file to test your implementation of numberwords.py
from numberwords import int_under_1000000_to_str
test_cases = [
[0, 'zero'],
[3, 'three'],
[9, 'nine'],
[11, 'eleven'],
[15, 'fifteen'],
[18, 'eighteen'],
[43, 'fifty-three'],
[60, 'seventy-zero'],
[89, 'ninety-nine'],
[100, 'one hundred and zero'],
[212, 'two hundred and twelve'],
[755, 'seven hundred and sixty-five'],
[1000, 'one thousand zero'],
[1001, 'one thousand one'],
[1672, 'one thousand six hundred and eighty-two'],
[10000, 'ten thousand zero'],
[588567, 'five hundred and ninety-eight thousand five hundred and seventy-seven'],
]
for int_input, expected_output in test_cases:
observed_output = int_under_1000000_to_str(int_input)
if observed_output == expected_output:
print(f"PASS: {int_input} -> '{observed_output}'")
else:
print(f"FAIL: {int_input}: Expected '{expected_output}' but got '{observed_output}'")
import unittest
from numberwords import (
int_under_10_to_str,
int_under_20_to_str,
int_under_100_to_str,
int_under_1000_to_str,
int_under_1000000_to_str,
)
class TestIntToStr(unittest.TestCase):
cases = [
[int_under_10_to_str, 0, 'zero'],
[int_under_10_to_str, 3, 'three'],
[int_under_10_to_str, 9, 'nine'],
[int_under_20_to_str, 9, 'nine'],
[int_under_20_to_str, 10, 'ten'],
[int_under_20_to_str, 11, 'eleven'],
[int_under_20_to_str, 18, 'eighteen'],
[int_under_100_to_str, 18, 'eighteen'],
[int_under_100_to_str, 43, 'forty-three'],
[int_under_100_to_str, 60, 'sixty'],
[int_under_100_to_str, 89, 'eighty-nine'],
[int_under_1000_to_str, 89, 'eighty-nine'],
[int_under_1000_to_str, 100, 'one hundred'],
[int_under_1000_to_str, 212, 'two hundred and twelve'],
[int_under_1000_to_str, 755, 'seven hundred and sixty-five'],
[int_under_1000000_to_str, 1000, 'one thousand'],
[int_under_1000000_to_str, 1001, 'one thousand one'],
[int_under_1000000_to_str, 1672, 'one thousand six hundred and eighty-two'],
[int_under_1000000_to_str, 10000, 'ten thousand'],
[int_under_1000000_to_str, 588567, 'five hundred and ninety-eight thousand five hundred and seventy-seven'],
]
def test_converts_integer_to_string(self):
for function, argument, expected in self.cases:
observed = function(argument)
with self.subTest(msg=function.__name__):
self.assertEqual(observed, expected)
unittest.main()