mirror of
https://github.com/Xevion/exercism.git
synced 2025-12-08 12:07:04 -06:00
113 lines
3.4 KiB
Markdown
113 lines
3.4 KiB
Markdown
# Say
|
|
|
|
Given a number from 0 to 999,999,999,999, spell out that number in English.
|
|
|
|
## Step 1
|
|
|
|
Handle the basic case of 0 through 99.
|
|
|
|
If the input to the program is `22`, then the output should be
|
|
`'twenty-two'`.
|
|
|
|
Your program should complain loudly if given a number outside the
|
|
blessed range.
|
|
|
|
Some good test cases for this program are:
|
|
|
|
- 0
|
|
- 14
|
|
- 50
|
|
- 98
|
|
- -1
|
|
- 100
|
|
|
|
### Extension
|
|
|
|
If you're on a Mac, shell out to Mac OS X's `say` program to talk out
|
|
loud. If you're on Linux or Windows, eSpeakNG may be available with the command `espeak`.
|
|
|
|
## Step 2
|
|
|
|
Implement breaking a number up into chunks of thousands.
|
|
|
|
So `1234567890` should yield a list like 1, 234, 567, and 890, while the
|
|
far simpler `1000` should yield just 1 and 0.
|
|
|
|
The program must also report any values that are out of range.
|
|
|
|
## Step 3
|
|
|
|
Now handle inserting the appropriate scale word between those chunks.
|
|
|
|
So `1234567890` should yield `'1 billion 234 million 567 thousand 890'`
|
|
|
|
The program must also report any values that are out of range. It's
|
|
fine to stop at "trillion".
|
|
|
|
## Step 4
|
|
|
|
Put it all together to get nothing but plain English.
|
|
|
|
`12345` should give `twelve thousand three hundred forty-five`.
|
|
|
|
The program must also report any values that are out of range.
|
|
|
|
### Extensions
|
|
|
|
Use _and_ (correctly) when spelling out the number in English:
|
|
|
|
- 14 becomes "fourteen".
|
|
- 100 becomes "one hundred".
|
|
- 120 becomes "one hundred and twenty".
|
|
- 1002 becomes "one thousand and two".
|
|
- 1323 becomes "one thousand three hundred and twenty-three".
|
|
|
|
## Exception messages
|
|
|
|
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
|
|
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
|
|
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
|
|
a message.
|
|
|
|
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
|
|
`raise Exception`, you should write:
|
|
|
|
```python
|
|
raise Exception("Meaningful message indicating the source of the error")
|
|
```
|
|
|
|
## Running the tests
|
|
|
|
To run the tests, run the appropriate command below ([why they are different](https://github.com/pytest-dev/pytest/issues/1629#issue-161422224)):
|
|
|
|
- Python 2.7: `py.test say_test.py`
|
|
- Python 3.4+: `pytest say_test.py`
|
|
|
|
Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version):
|
|
`python -m pytest say_test.py`
|
|
|
|
### Common `pytest` options
|
|
|
|
- `-v` : enable verbose output
|
|
- `-x` : stop running tests on first failure
|
|
- `--ff` : run failures from previous test before running other test cases
|
|
|
|
For other options, see `python -m pytest -h`
|
|
|
|
## Submitting Exercises
|
|
|
|
Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/say` directory.
|
|
|
|
You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.
|
|
|
|
For more detailed information about running tests, code style and linting,
|
|
please see [Running the Tests](http://exercism.io/tracks/python/tests).
|
|
|
|
## Source
|
|
|
|
A variation on JavaRanch CattleDrive, exercise 4a [http://www.javaranch.com/say.jsp](http://www.javaranch.com/say.jsp)
|
|
|
|
## Submitting Incomplete Solutions
|
|
|
|
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|