add a new exercise

This commit is contained in:
Andreas Zweili 2021-10-31 12:52:25 +01:00
parent 294d39981b
commit 6a7a1cb8e5
6 changed files with 276 additions and 0 deletions

View File

@ -0,0 +1,38 @@
{
"blurb": "Given a phrase, count the occurrences of each word in that phrase.",
"authors": [],
"contributors": [
"behrtam",
"c4llmeco4ch",
"cmccandless",
"Dog",
"gabriel376",
"Grociu",
"guygastineau",
"ikhadykin",
"jackattack24",
"kytrinyx",
"lowks",
"N-Parsons",
"pheanex",
"rivergillis",
"samdec11",
"sjakobi",
"tqa236",
"wobh",
"yawpitch",
"ZacharyRSmith"
],
"files": {
"solution": [
"word_count.py"
],
"test": [
"word_count_test.py"
],
"example": [
".meta/example.py"
]
},
"source": "This is a classic toy problem, but we were reminded of it by seeing it in the Go Tour."
}

View File

@ -0,0 +1 @@
{"track":"python","exercise":"word-count","id":"bdd6135b0dbb4fec9695a92a43da8dce","url":"https://exercism.org/tracks/python/exercises/word-count","handle":"Nebucatnetzer","is_requester":true,"auto_approve":false}

55
python/word-count/HELP.md Normal file
View File

@ -0,0 +1,55 @@
# Help
## Running the tests
To run the included *tests*, run the test file using the `pytest` module, replacing `{exercise_name}`:
```bash
$ python3 -m pytest {exercise_name}_test.py
```
Many IDE's and code editors have built-in support for using Pytest to run tests; check them out [here](https://github.com/exercism/python/blob/main/docs/TOOLS.md#editors-and-ides).
For more information about running tests using `pytest`, checkout our [Python testing guide](https://github.com/exercism/python/blob/main/docs/TESTS.md#pytest).
### 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 `python3 -m pytest -h`.
## Submitting your solution
You can submit your solution using the `exercism submit word_count.py` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Python track's documentation](https://exercism.org/docs/tracks/python)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
Below are some resources for getting help if you run into trouble:
- [The PSF](https://www.python.org) hosts Python downloads, documentation, and community resources.
- [Python Community on Discord](https://pythondiscord.com/) is a very helpful and active community.
- [#python on Libera.chat](https://www.python.org/community/irc/) this is where the cored developers for the language hang out and get work done.
- [Exercism on Gitter](https://gitter.im/exercism/home) join the Python room for Python-related questions or problems.
- [/r/learnpython/](https://www.reddit.com/r/learnpython/) is a subreddit designed for Python learners.
- [Python Community Forums](https://discuss.python.org/)
- [Pythontutor](http://pythontutor.com/) for stepping through small code snippets visually.
Additionally, [StackOverflow](http://stackoverflow.com/questions/tagged/python) is a good spot to search for your problem/question to see if it has been answered already.
If not - you can always [ask](https://stackoverflow.com/help/how-to-ask) or [answer](https://stackoverflow.com/help/how-to-answer) someone else's question.

View File

@ -0,0 +1,65 @@
# Word Count
Welcome to Word Count on Exercism's Python Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
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
```
## Source
### Contributed to by
- @behrtam
- @c4llmeco4ch
- @cmccandless
- @Dog
- @gabriel376
- @Grociu
- @guygastineau
- @ikhadykin
- @jackattack24
- @kytrinyx
- @lowks
- @N-Parsons
- @pheanex
- @rivergillis
- @samdec11
- @sjakobi
- @tqa236
- @wobh
- @yawpitch
- @ZacharyRSmith
### Based on
This is a classic toy problem, but we were reminded of it by seeing it in the Go Tour.

View File

@ -0,0 +1,2 @@
def count_words(sentence):
pass

View File

@ -0,0 +1,115 @@
import unittest
from word_count import (
count_words,
)
# Tests adapted from `problem-specifications//canonical-data.json`
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_substrings_from_the_beginning(self):
self.assertEqual(
count_words("Joe can't tell between app, apple and a."),
{
"joe": 1,
"can't": 1,
"tell": 1,
"between": 1,
"app": 1,
"apple": 1,
"and": 1,
"a": 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 roma roma ma ga ga oh la la 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},
)
def test_multiple_apostrophes_ignored(self):
self.assertEqual(count_words("''hey''"), {"hey": 1})
if __name__ == "__main__":
unittest.main()