generated from mwc/problemset_numberwords
Updating with the correct starter code
This commit is contained in:
parent
e4f8546046
commit
bd88ab0514
|
@ -11,58 +11,27 @@ TWEEN_AND_TEEN_NAMES = [
|
||||||
"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
|
"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
|
||||||
]
|
]
|
||||||
TENS_NAMES = [
|
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):
|
def int_under_1000000_to_str(number):
|
||||||
"Returns a textual representation of the number."
|
return "umm..."
|
||||||
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):
|
def int_under_1000_to_str(number):
|
||||||
"Returns a textual representation of the number"
|
return "umm..."
|
||||||
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):
|
def int_under_100_to_str(number):
|
||||||
check_number_in_range(number, 0, 100)
|
return "umm..."
|
||||||
tens, ones = divide_with_remainder(number, 10)
|
|
||||||
if tens == 0:
|
def int_under_20_to_str(number):
|
||||||
return int_under_10_to_str(number)
|
return "umm..."
|
||||||
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):
|
def int_under_10_to_str(number):
|
||||||
check_number_in_range(number, 0, 10)
|
return "umm..."
|
||||||
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):
|
def divide_with_remainder(dividend, divisor):
|
||||||
"""Divides one number by another, using whole-number division.
|
"""Divides one number by another, using whole-number division.
|
||||||
Returns the quotient and the remainder.
|
Returns the quotient and the remainder.
|
||||||
Note how a function can return more than one value!
|
|
||||||
"""
|
"""
|
||||||
quotient = dividend // divisor
|
quotient = dividend // divisor
|
||||||
remainder = dividend % divisor
|
remainder = dividend % divisor
|
||||||
|
|
|
@ -3,35 +3,43 @@
|
||||||
# By MWC Contributors
|
# By MWC Contributors
|
||||||
# Run this file to test your implementation of numberwords.py
|
# Run this file to test your implementation of numberwords.py
|
||||||
|
|
||||||
from numberwords import int_under_1000000_to_str
|
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,
|
||||||
|
)
|
||||||
|
|
||||||
test_cases = [
|
class TestIntToStr(unittest.TestCase):
|
||||||
[0, 'zero'],
|
cases = [
|
||||||
[3, 'three'],
|
[int_under_10_to_str, 0, 'zero'],
|
||||||
[9, 'nine'],
|
[int_under_10_to_str, 3, 'three'],
|
||||||
[11, 'eleven'],
|
[int_under_10_to_str, 9, 'nine'],
|
||||||
[15, 'fifteen'],
|
[int_under_20_to_str, 9, 'nine'],
|
||||||
[18, 'eighteen'],
|
[int_under_20_to_str, 10, 'ten'],
|
||||||
[43, 'fifty-three'],
|
[int_under_20_to_str, 11, 'eleven'],
|
||||||
[60, 'seventy-zero'],
|
[int_under_20_to_str, 18, 'eighteen'],
|
||||||
[89, 'ninety-nine'],
|
[int_under_100_to_str, 18, 'eighteen'],
|
||||||
[100, 'one hundred and zero'],
|
[int_under_100_to_str, 43, 'forty-three'],
|
||||||
[212, 'two hundred and twelve'],
|
[int_under_100_to_str, 60, 'sixty'],
|
||||||
[755, 'seven hundred and sixty-five'],
|
[int_under_100_to_str, 89, 'eighty-nine'],
|
||||||
[1000, 'one thousand zero'],
|
[int_under_1000_to_str, 89, 'eighty-nine'],
|
||||||
[1001, 'one thousand one'],
|
[int_under_1000_to_str, 100, 'one hundred'],
|
||||||
[1672, 'one thousand six hundred and eighty-two'],
|
[int_under_1000_to_str, 212, 'two hundred and twelve'],
|
||||||
[10000, 'ten thousand zero'],
|
[int_under_1000_to_str, 755, 'seven hundred and sixty-five'],
|
||||||
[588567, 'five hundred and ninety-eight thousand five hundred and seventy-seven'],
|
[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'],
|
||||||
]
|
]
|
||||||
|
|
||||||
for int_input, expected_output in test_cases:
|
def test_converts_integer_to_string(self):
|
||||||
observed_output = int_under_1000000_to_str(int_input)
|
for function, argument, expected in self.cases:
|
||||||
if observed_output == expected_output:
|
observed = function(argument)
|
||||||
print(f"PASS: {int_input} -> '{observed_output}'")
|
with self.subTest(msg=function.__name__):
|
||||||
else:
|
self.assertEqual(observed, expected)
|
||||||
print(f"FAIL: {int_input}: Expected '{expected_output}' but got '{observed_output}'")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
unittest.main()
|
||||||
|
|
Loading…
Reference in New Issue