Compare commits

..

No commits in common. "main" and "solutions" have entirely different histories.

5 changed files with 83 additions and 68 deletions

View File

@ -1,11 +0,0 @@
# -----------------------------------------------------------------
# Write your commit message above this line.
#
# The first line should be a quick description of what you changed.
# Then leave a blank line.
# Then write a few sentences reflecting on one moment from your last
# work session. What were you feeling? If you solved a problem,
# what strategy helped you?

View File

@ -11,27 +11,58 @@ 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):
return "umm..."
"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):
return "umm..."
"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):
return "umm..."
def int_under_20_to_str(number):
return "umm..."
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):
return "umm..."
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.
Returns the quotient and the remainder.
Note how a function can return more than one value!
"""
quotient = dividend // divisor
remainder = dividend % divisor

7
poetry.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Poetry 1.5.1 and should not be changed by hand.
package = []
[metadata]
lock-version = "2.0"
python-versions = "^3.10"
content-hash = "53f2eabc9c26446fbcc00d348c47878e118afc2054778c3c803a0a8028af27d9"

View File

@ -1,20 +1,16 @@
[project]
[tool.poetry]
name = "problemset-numberwords"
version = "0.1.0"
description = ""
authors = [
{name = "Chris Proctor",email = "chris@chrisproctor.net"}
]
license = {text = "MIT"}
authors = ["Chris Proctor <chris@chrisproctor.net>"]
readme = "README.md"
requires-python = ">=3.10,<4.0"
dependencies = [
]
[tool.poetry.dependencies]
python = "^3.10"
[build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
[tool.poetry]
package-mode = false
[tool.poetry.scripts]
numwords = "cli:cli"

View File

@ -3,43 +3,35 @@
# By MWC Contributors
# Run this file to test your implementation of numberwords.py
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,
)
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}'")
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 fifty-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 seventy-two'],
[int_under_1000000_to_str, 10000, 'ten thousand'],
[int_under_1000000_to_str, 588567, 'five hundred and ninety-eight thousand five hundred and sixty-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()