twelve days and word count exercises

This commit is contained in:
Xevion
2019-07-13 17:06:54 -05:00
parent cbd5c44d63
commit f127035e22
9 changed files with 409 additions and 1 deletions

View File

@@ -0,0 +1 @@
{"track":"python","exercise":"word-count","id":"b8740c62231043358e4bdaa590764dbe","url":"https://exercism.io/my/solutions/b8740c62231043358e4bdaa590764dbe","handle":"Xevion","is_requester":true,"auto_approve":false}

View File

@@ -0,0 +1,61 @@
# Word Count
Given a phrase, count the occurrences of each word in that phrase.
For example for the input `"olly olly in come free"`
```text
olly: 2
in: 1
come: 1
free: 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 the appropriate command below ([why they are different](https://github.com/pytest-dev/pytest/issues/1629#issue-161422224)):
- Python 2.7: `py.test word_count_test.py`
- Python 3.4+: `pytest word_count_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 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.

View File

@@ -0,0 +1,4 @@
import string, re
def count_words(sentence):
sentence = [x.strip(string.punctuation) for x in [word for word in re.split(r'[_,\s]+', sentence.casefold()) if len(word.strip(string.punctuation + string.whitespace)) >= 1]]
return {k : sentence.count(k) for k in list(dict.fromkeys(sentence))}

View File

@@ -0,0 +1,101 @@
import unittest
from word_count import count_words
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.3.0
class WordCountTest(unittest.TestCase):
def test_count_one_word(self):
self.assertEqual(
count_words('word'),
{'word': 1}
)
def test_count_one_of_each_word(self):
self.assertEqual(
count_words('one of each'),
{'one': 1, 'of': 1, 'each': 1}
)
def test_multiple_occurrences_of_a_word(self):
self.assertEqual(
count_words('one fish two fish red fish blue fish'),
{'one': 1, 'fish': 4, 'two': 1, 'red': 1, 'blue': 1}
)
def test_handles_cramped_lists(self):
self.assertEqual(
count_words('one,two,three'),
{'one': 1, 'two': 1, 'three': 1}
)
def test_handles_expanded_lists(self):
self.assertEqual(
count_words('one,\ntwo,\nthree'),
{'one': 1, 'two': 1, 'three': 1}
)
def test_ignore_punctuation(self):
self.assertEqual(
count_words('car : carpet as java : javascript!!&@$%^&'),
{'car': 1, 'carpet': 1, 'as': 1, 'java': 1, 'javascript': 1}
)
def test_include_numbers(self):
self.assertEqual(
count_words('testing 1 2 testing'),
{'testing': 2, '1': 1, '2': 1}
)
def test_normalize_case(self):
self.assertEqual(
count_words('go Go GO Stop stop'),
{'go': 3, 'stop': 2}
)
def test_with_apostrophes(self):
self.assertEqual(
count_words("First: don't laugh. Then: don't cry."),
{'first': 1, "don't": 2, 'laugh': 1, 'then': 1, 'cry': 1}
)
def test_with_quotations(self):
self.assertEqual(
count_words("Joe can't tell between 'large' and large."),
{'joe': 1, "can't": 1, 'tell': 1, 'between': 1, 'large': 2,
'and': 1}
)
def test_multiple_spaces_not_detected_as_a_word(self):
self.assertEqual(
count_words(' multiple whitespaces'),
{'multiple': 1, 'whitespaces': 1}
)
def test_alternating_word_separators_not_detected_as_a_word(self):
self.assertEqual(
count_words(",\n,one,\n ,two \n 'three'"),
{'one': 1, 'two': 1, 'three': 1}
)
# Additional tests for this track
def test_tabs(self):
self.assertEqual(
count_words('rah rah ah ah ah\troma roma ma\tga ga oh la la\t'
'want your bad romance'),
{'rah': 2, 'ah': 3, 'roma': 2, 'ma': 1, 'ga': 2, 'oh': 1, 'la': 2,
'want': 1, 'your': 1, 'bad': 1, 'romance': 1}
)
def test_non_alphanumeric(self):
self.assertEqual(
count_words('hey,my_spacebar_is_broken.'),
{'hey': 1, 'my': 1, 'spacebar': 1, 'is': 1, 'broken': 1}
)
if __name__ == '__main__':
unittest.main()