Compare commits
9 Commits
Author | SHA1 | Date |
---|---|---|
|
cfc31beb8b | |
|
ab1a34ed55 | |
|
865b6cd0b8 | |
|
9e14c962c5 | |
|
c713541952 | |
|
473d44e015 | |
|
d16f9903b5 | |
|
cea08c1fc4 | |
|
e644d2d62d |
|
@ -0,0 +1,11 @@
|
|||
|
||||
|
||||
# -----------------------------------------------------------------
|
||||
# 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?
|
||||
|
|
@ -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
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
# 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"
|
|
@ -1,16 +1,20 @@
|
|||
[tool.poetry]
|
||||
[project]
|
||||
name = "problemset-numberwords"
|
||||
version = "0.1.0"
|
||||
description = ""
|
||||
authors = ["Chris Proctor <chris@chrisproctor.net>"]
|
||||
authors = [
|
||||
{name = "Chris Proctor",email = "chris@chrisproctor.net"}
|
||||
]
|
||||
license = {text = "MIT"}
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.10,<4.0"
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = "^3.10"
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core"]
|
||||
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
||||
build-backend = "poetry.core.masonry.api"
|
||||
|
||||
[tool.poetry.scripts]
|
||||
numwords = "cli:cli"
|
||||
[tool.poetry]
|
||||
package-mode = false
|
||||
|
|
|
@ -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 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()
|
||||
|
|
Loading…
Reference in New Issue