Initial commit

This commit is contained in:
nate 2025-05-20 00:21:29 +00:00
commit 8aa2560b66
7 changed files with 186 additions and 0 deletions

11
.commit_template Normal file
View File

@ -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?

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
__pycache__/*
*.swp
*.swo

38
numberwords.py Normal file
View File

@ -0,0 +1,38 @@
# numberwords.py
# --------------
# By MWC Contributors
# Functions to print out a verbal representation of an integer.
MAXIMUM = 1000000
DIGIT_NAMES = [
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"
]
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"
]
def int_under_1000000_to_str(number):
return "umm..."
def int_under_1000_to_str(number):
return "umm..."
def int_under_100_to_str(number):
return "umm..."
def int_under_20_to_str(number):
return "umm..."
def int_under_10_to_str(number):
return "umm..."
def divide_with_remainder(dividend, divisor):
"""Divides one number by another, using whole-number division.
Returns the quotient and the remainder.
"""
quotient = dividend // divisor
remainder = dividend % divisor
return quotient, remainder

14
nw.py Normal file
View File

@ -0,0 +1,14 @@
# nw.py
# ------
# Implements a simple number-to-text command-line interface.
# Ex: python nw.py 145
from argparse import ArgumentParser
from numberwords import int_under_1000000_to_str
parser = ArgumentParser("Print out a number as it is spoken in English.")
parser.add_argument("number", type=int)
args = parser.parse_args()
text = int_under_1000000_to_str(args.number)
print(text)

55
planning.md Normal file
View File

@ -0,0 +1,55 @@
# Planning Number Words
Before you start programming, do some planning here on how you will break down
this problem. Here's a hint: if you start by writing functions for smaller numbers,
you will find that these functions help you with the larger numbers. For each of
the cases below, explain how you would turn a number into a string. Feel free to
write in sentences or in pseudocode (pseudocode is a sort of "casual programming"
where you're almost writing in code, being pretty specific without worrying about
syntax. For each case below, assume the integer is zero or more--don't worry about
negative integers.
## Integers under 10
(This one is done for you!)
For an integer less than ten, you need to know the name of each digit, and look it
up. You could use a big if/else statement like:
```
if number == 0:
return "zero"
elif number == 1:
return "one"
elif number == 1:
return "two"
```
A cleaner way to do this would be to make a list of digit names, from zero to nine.
Then you could just look up a digit's name:
```
digit_names = [
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"
]
return digit_names[number]
```
## Integers under 20
If the integer is under 10, then use the procedure described above.
Otherwise, ... (this is where you take over!)
## Integers under 100
## Integers under 1000
## Integers under 1000000
## Negative integers down to -1 million
We won't deal with negative integers in this problem set,
but how would you deal with a negative integer, using the
functions above?

20
pyproject.toml Normal file
View File

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

45
test_numberwords.py Normal file
View File

@ -0,0 +1,45 @@
# test_numberwords.py
# -------------------
# 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,
)
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()