78 lines
2.7 KiB
Markdown
78 lines
2.7 KiB
Markdown
# Word Count
|
|
|
|
Given a phrase, count the occurrences of each _word_ in that phrase.
|
|
|
|
For the purposes of this exercise you can expect that a _word_ will always be one of:
|
|
|
|
1. A _number_ composed of one or more ASCII digits (ie "0" or "1234") OR
|
|
2. A _simple word_ composed of one or more ASCII letters (ie "a" or "they") OR
|
|
3. A _contraction_ of two _simple words_ joined by a single apostrophe (ie "it's" or "they're")
|
|
|
|
When counting words you can assume the following rules:
|
|
|
|
1. The count is _case insensitive_ (ie "You", "you", and "YOU" are 3 uses of the same word)
|
|
2. The count is _unordered_; the tests will ignore how words and counts are ordered
|
|
3. Other than the apostrophe in a _contraction_ all forms of _punctuation_ are ignored
|
|
4. The words can be separated by _any_ form of whitespace (ie "\t", "\n", " ")
|
|
|
|
For example, for the phrase `"That's the password: 'PASSWORD 123'!", cried the Special Agent.\nSo I fled.` the count would be:
|
|
|
|
```text
|
|
that's: 1
|
|
the: 2
|
|
password: 2
|
|
123: 1
|
|
cried: 1
|
|
special: 1
|
|
agent: 1
|
|
so: 1
|
|
i: 1
|
|
fled: 1
|
|
```
|
|
|
|
|
|
## 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 `pytest word_count_test.py`
|
|
|
|
Alternatively, you can tell Python to run the pytest module:
|
|
`python -m pytest word_count_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/word-count` 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
|
|
|
|
This is a classic toy problem, but we were reminded of it by seeing it in the Go Tour.
|
|
|
|
## Submitting Incomplete Solutions
|
|
|
|
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|