generated from mwc/problemset_numberwords
	Initial commit
This commit is contained in:
		
							
								
								
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,3 @@
 | 
			
		||||
__pycache__/*
 | 
			
		||||
*.swp
 | 
			
		||||
*.swo
 | 
			
		||||
							
								
								
									
										38
									
								
								numberwords.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								numberwords.py
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										14
									
								
								nw.py
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										55
									
								
								planning.md
									
									
									
									
									
										Normal 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?
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										7
									
								
								poetry.lock
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								poetry.lock
									
									
									
										generated
									
									
									
										Normal 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"
 | 
			
		||||
							
								
								
									
										16
									
								
								pyproject.toml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								pyproject.toml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
			
		||||
[tool.poetry]
 | 
			
		||||
name = "problemset-numberwords"
 | 
			
		||||
version = "0.1.0"
 | 
			
		||||
description = ""
 | 
			
		||||
authors = ["Chris Proctor <chris@chrisproctor.net>"]
 | 
			
		||||
readme = "README.md"
 | 
			
		||||
 | 
			
		||||
[tool.poetry.dependencies]
 | 
			
		||||
python = "^3.10"
 | 
			
		||||
 | 
			
		||||
[build-system]
 | 
			
		||||
requires = ["poetry-core"]
 | 
			
		||||
build-backend = "poetry.core.masonry.api"
 | 
			
		||||
 | 
			
		||||
[tool.poetry.scripts]
 | 
			
		||||
numwords = "cli:cli"
 | 
			
		||||
							
								
								
									
										45
									
								
								test_numberwords.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								test_numberwords.py
									
									
									
									
									
										Normal 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()
 | 
			
		||||
		Reference in New Issue
	
	Block a user