Skip to content

Commit 1be5a4f

Browse files
committed
Add anagram exercise with implementation and tests
Introduces the anagram exercise including configuration, metadata, help and readme files, the implementation in anagram.py, and comprehensive unit tests in anagram_test.py.
1 parent 1cb6e8e commit 1be5a4f

File tree

6 files changed

+363
-0
lines changed

6 files changed

+363
-0
lines changed

anagram/.exercism/config.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"authors": [],
3+
"contributors": [
4+
"behrtam",
5+
"cmccandless",
6+
"crazymykl",
7+
"Dog",
8+
"dotrungkien",
9+
"henrik",
10+
"ikhadykin",
11+
"kytrinyx",
12+
"lowks",
13+
"markijbema",
14+
"N-Parsons",
15+
"n1k0",
16+
"pheanex",
17+
"roadfoodr",
18+
"sjakobi",
19+
"thomasjpfan",
20+
"tqa236",
21+
"vgerak",
22+
"yawpitch"
23+
],
24+
"files": {
25+
"solution": [
26+
"anagram.py"
27+
],
28+
"test": [
29+
"anagram_test.py"
30+
],
31+
"example": [
32+
".meta/example.py"
33+
]
34+
},
35+
"blurb": "Given a word and a list of possible anagrams, select the correct sublist.",
36+
"source": "Inspired by the Extreme Startup game",
37+
"source_url": "https://github.com/rchatley/extreme_startup"
38+
}

anagram/.exercism/metadata.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"track":"python","exercise":"anagram","id":"c89b81911fde429d8f7157e018c977e9","url":"https://exercism.org/tracks/python/exercises/anagram","handle":"myFirstCode","is_requester":true,"auto_approve":false}

anagram/HELP.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Help
2+
3+
## Running the tests
4+
5+
We use [pytest][pytest: Getting Started Guide] as our website test runner.
6+
You will need to install `pytest` on your development machine if you want to run tests for the Python track locally.
7+
You should also install the following `pytest` plugins:
8+
9+
- [pytest-cache][pytest-cache]
10+
- [pytest-subtests][pytest-subtests]
11+
12+
Extended information can be found in our website [Python testing guide][Python track tests page].
13+
14+
15+
### Running Tests
16+
17+
To run the included tests, navigate to the folder where the exercise is stored using `cd` in your terminal (_replace `{exercise-folder-location}` below with your path_).
18+
Test files usually end in `_test.py`, and are the same tests that run on the website when a solution is uploaded.
19+
20+
Linux/MacOS
21+
```bash
22+
$ cd {path/to/exercise-folder-location}
23+
```
24+
25+
Windows
26+
```powershell
27+
PS C:\Users\foobar> cd {path\to\exercise-folder-location}
28+
```
29+
30+
<br>
31+
32+
Next, run the `pytest` command in your terminal, replacing `{exercise_test.py}` with the name of the test file:
33+
34+
Linux/MacOS
35+
```bash
36+
$ python3 -m pytest -o markers=task {exercise_test.py}
37+
==================== 7 passed in 0.08s ====================
38+
```
39+
40+
Windows
41+
```powershell
42+
PS C:\Users\foobar> py -m pytest -o markers=task {exercise_test.py}
43+
==================== 7 passed in 0.08s ====================
44+
```
45+
46+
47+
### Common options
48+
- `-o` : override default `pytest.ini` (_you can use this to avoid marker warnings_)
49+
- `-v` : enable verbose output.
50+
- `-x` : stop running tests on first failure.
51+
- `--ff` : run failures from previous test before running other test cases.
52+
53+
For additional options, use `python3 -m pytest -h` or `py -m pytest -h`.
54+
55+
56+
### Fixing warnings
57+
58+
If you do not use `pytest -o markers=task` when invoking `pytest`, you might receive a `PytestUnknownMarkWarning` for tests that use our new syntax:
59+
60+
```bash
61+
PytestUnknownMarkWarning: Unknown pytest.mark.task - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html
62+
```
63+
64+
To avoid typing `pytest -o markers=task` for every test you run, you can use a `pytest.ini` configuration file.
65+
We have made one that can be downloaded from the top level of the Python track directory: [pytest.ini][pytest.ini].
66+
67+
You can also create your own `pytest.ini` file with the following content:
68+
69+
```ini
70+
[pytest]
71+
markers =
72+
task: A concept exercise task.
73+
```
74+
75+
Placing the `pytest.ini` file in the _root_ or _working_ directory for your Python track exercises will register the marks and stop the warnings.
76+
More information on pytest marks can be found in the `pytest` documentation on [marking test functions][pytest: marking test functions with attributes] and the `pytest` documentation on [working with custom markers][pytest: working with custom markers].
77+
78+
Information on customizing pytest configurations can be found in the `pytest` documentation on [configuration file formats][pytest: configuration file formats].
79+
80+
81+
### Extending your IDE or Code Editor
82+
83+
Many IDEs and code editors have built-in support for using `pytest` and other code quality tools.
84+
Some community-sourced options can be found on our [Python track tools page][Python track tools page].
85+
86+
[Pytest: Getting Started Guide]: https://docs.pytest.org/en/latest/getting-started.html
87+
[Python track tools page]: https://exercism.org/docs/tracks/python/tools
88+
[Python track tests page]: https://exercism.org/docs/tracks/python/tests
89+
[pytest-cache]:http://pythonhosted.org/pytest-cache/
90+
[pytest-subtests]:https://github.com/pytest-dev/pytest-subtests
91+
[pytest.ini]: https://github.com/exercism/python/blob/main/pytest.ini
92+
[pytest: configuration file formats]: https://docs.pytest.org/en/6.2.x/customize.html#configuration-file-formats
93+
[pytest: marking test functions with attributes]: https://docs.pytest.org/en/6.2.x/mark.html#raising-errors-on-unknown-marks
94+
[pytest: working with custom markers]: https://docs.pytest.org/en/6.2.x/example/markers.html#working-with-custom-markers
95+
96+
## Submitting your solution
97+
98+
You can submit your solution using the `exercism submit anagram.py` command.
99+
This command will upload your solution to the Exercism website and print the solution page's URL.
100+
101+
It's possible to submit an incomplete solution which allows you to:
102+
103+
- See how others have completed the exercise
104+
- Request help from a mentor
105+
106+
## Need to get help?
107+
108+
If you'd like help solving the exercise, check the following pages:
109+
110+
- The [Python track's documentation](https://exercism.org/docs/tracks/python)
111+
- The [Python track's programming category on the forum](https://forum.exercism.org/c/programming/python)
112+
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
113+
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
114+
115+
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
116+
117+
Below are some resources for getting help if you run into trouble:
118+
119+
- [The PSF](https://www.python.org) hosts Python downloads, documentation, and community resources.
120+
- [The Exercism Community on Discord](https://exercism.org/r/discord)
121+
- [Python Community on Discord](https://pythondiscord.com/) is a very helpful and active community.
122+
- [/r/learnpython/](https://www.reddit.com/r/learnpython/) is a subreddit designed for Python learners.
123+
- [#python on Libera.chat](https://www.python.org/community/irc/) this is where the core developers for the language hang out and get work done.
124+
- [Python Community Forums](https://discuss.python.org/)
125+
- [Free Code Camp Community Forums](https://forum.freecodecamp.org/)
126+
- [CodeNewbie Community Help Tag](https://community.codenewbie.org/t/help)
127+
- [Pythontutor](http://pythontutor.com/) for stepping through small code snippets visually.
128+
129+
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.
130+
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.

anagram/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Anagram
2+
3+
Welcome to Anagram on Exercism's Python Track.
4+
If you need help running the tests or submitting your code, check out `HELP.md`.
5+
6+
## Introduction
7+
8+
At a garage sale, you find a lovely vintage typewriter at a bargain price!
9+
Excitedly, you rush home, insert a sheet of paper, and start typing away.
10+
However, your excitement wanes when you examine the output: all words are garbled!
11+
For example, it prints "stop" instead of "post" and "least" instead of "stale."
12+
Carefully, you try again, but now it prints "spot" and "slate."
13+
After some experimentation, you find there is a random delay before each letter is printed, which messes up the order.
14+
You now understand why they sold it for so little money!
15+
16+
You realize this quirk allows you to generate anagrams, which are words formed by rearranging the letters of another word.
17+
Pleased with your finding, you spend the rest of the day generating hundreds of anagrams.
18+
19+
## Instructions
20+
21+
Given a target word and one or more candidate words, your task is to find the candidates that are anagrams of the target.
22+
23+
An anagram is a rearrangement of letters to form a new word: for example `"owns"` is an anagram of `"snow"`.
24+
A word is _not_ its own anagram: for example, `"stop"` is not an anagram of `"stop"`.
25+
26+
The target word and candidate words are made up of one or more ASCII alphabetic characters (`A`-`Z` and `a`-`z`).
27+
Lowercase and uppercase characters are equivalent: for example, `"PoTS"` is an anagram of `"sTOp"`, but `"StoP"` is not an anagram of `"sTOp"`.
28+
The words you need to find should be taken from the candidate words, using the same letter case.
29+
30+
Given the target `"stone"` and the candidate words `"stone"`, `"tones"`, `"banana"`, `"tons"`, `"notes"`, and `"Seton"`, the anagram words you need to find are `"tones"`, `"notes"`, and `"Seton"`.
31+
32+
## Source
33+
34+
### Contributed to by
35+
36+
- @behrtam
37+
- @cmccandless
38+
- @crazymykl
39+
- @Dog
40+
- @dotrungkien
41+
- @henrik
42+
- @ikhadykin
43+
- @kytrinyx
44+
- @lowks
45+
- @markijbema
46+
- @N-Parsons
47+
- @n1k0
48+
- @pheanex
49+
- @roadfoodr
50+
- @sjakobi
51+
- @thomasjpfan
52+
- @tqa236
53+
- @vgerak
54+
- @yawpitch
55+
56+
### Based on
57+
58+
Inspired by the Extreme Startup game - https://github.com/rchatley/extreme_startup

anagram/anagram.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Anagram.
3+
4+
Given a target word and one or more candidate words,
5+
your task is to find the candidates that are anagrams
6+
of the target.
7+
"""
8+
9+
10+
def find_anagrams(word: str, candidates: list[str]) -> list[str]:
11+
"""
12+
Return the list of candidates that are anagrams of ``word``.
13+
14+
Two words are considered anagrams if they consist of the same letters
15+
with the same multiplicities when case is ignored. The original casing of
16+
candidates is preserved in the returned list. Exact same word (case-insensitive)
17+
as the target is excluded.
18+
19+
:param word: Target word to compare against.
20+
:param candidates: Sequence of candidate words to test.
21+
:returns: A list containing each candidate that is an anagram of ``word``.
22+
"""
23+
word_ordered_lower_chars: list[str] = sorted(char.lower() for char in word)
24+
word_lower = word.lower()
25+
return [
26+
candidate
27+
for candidate in candidates
28+
if candidate.lower() != word_lower
29+
and sorted(char for char in candidate.lower()) == word_ordered_lower_chars
30+
]

anagram/anagram_test.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# pylint: disable=C0301, C0114, C0115, C0116, R0904
2+
# These tests are auto-generated with test data from:
3+
# https://github.com/exercism/problem-specifications/tree/main/exercises/anagram/canonical-data.json
4+
# File last updated on 2024-02-28
5+
6+
import unittest
7+
8+
from anagram import (
9+
find_anagrams,
10+
)
11+
12+
13+
class AnagramTest(unittest.TestCase):
14+
def test_no_matches(self):
15+
candidates = ["hello", "world", "zombies", "pants"]
16+
expected = []
17+
self.assertCountEqual(find_anagrams("diaper", candidates), expected)
18+
19+
def test_detects_two_anagrams(self):
20+
candidates = ["lemons", "cherry", "melons"]
21+
expected = ["lemons", "melons"]
22+
self.assertCountEqual(find_anagrams("solemn", candidates), expected)
23+
24+
def test_does_not_detect_anagram_subsets(self):
25+
candidates = ["dog", "goody"]
26+
expected = []
27+
self.assertCountEqual(find_anagrams("good", candidates), expected)
28+
29+
def test_detects_anagram(self):
30+
candidates = ["enlists", "google", "inlets", "banana"]
31+
expected = ["inlets"]
32+
self.assertCountEqual(find_anagrams("listen", candidates), expected)
33+
34+
def test_detects_three_anagrams(self):
35+
candidates = ["gallery", "ballerina", "regally", "clergy", "largely", "leading"]
36+
expected = ["gallery", "regally", "largely"]
37+
self.assertCountEqual(find_anagrams("allergy", candidates), expected)
38+
39+
def test_detects_multiple_anagrams_with_different_case(self):
40+
candidates = ["Eons", "ONES"]
41+
expected = ["Eons", "ONES"]
42+
self.assertCountEqual(find_anagrams("nose", candidates), expected)
43+
44+
def test_does_not_detect_non_anagrams_with_identical_checksum(self):
45+
candidates = ["last"]
46+
expected = []
47+
self.assertCountEqual(find_anagrams("mass", candidates), expected)
48+
49+
def test_detects_anagrams_case_insensitively(self):
50+
candidates = ["cashregister", "Carthorse", "radishes"]
51+
expected = ["Carthorse"]
52+
self.assertCountEqual(find_anagrams("Orchestra", candidates), expected)
53+
54+
def test_detects_anagrams_using_case_insensitive_subject(self):
55+
candidates = ["cashregister", "carthorse", "radishes"]
56+
expected = ["carthorse"]
57+
self.assertCountEqual(find_anagrams("Orchestra", candidates), expected)
58+
59+
def test_detects_anagrams_using_case_insensitive_possible_matches(self):
60+
candidates = ["cashregister", "Carthorse", "radishes"]
61+
expected = ["Carthorse"]
62+
self.assertCountEqual(find_anagrams("orchestra", candidates), expected)
63+
64+
def test_does_not_detect_an_anagram_if_the_original_word_is_repeated(self):
65+
candidates = ["goGoGO"]
66+
expected = []
67+
self.assertCountEqual(find_anagrams("go", candidates), expected)
68+
69+
def test_anagrams_must_use_all_letters_exactly_once(self):
70+
candidates = ["patter"]
71+
expected = []
72+
self.assertCountEqual(find_anagrams("tapper", candidates), expected)
73+
74+
def test_words_are_not_anagrams_of_themselves(self):
75+
candidates = ["BANANA"]
76+
expected = []
77+
self.assertCountEqual(find_anagrams("BANANA", candidates), expected)
78+
79+
def test_words_are_not_anagrams_of_themselves_even_if_letter_case_is_partially_different(
80+
self,
81+
):
82+
candidates = ["Banana"]
83+
expected = []
84+
self.assertCountEqual(find_anagrams("BANANA", candidates), expected)
85+
86+
def test_words_are_not_anagrams_of_themselves_even_if_letter_case_is_completely_different(
87+
self,
88+
):
89+
candidates = ["banana"]
90+
expected = []
91+
self.assertCountEqual(find_anagrams("BANANA", candidates), expected)
92+
93+
def test_words_other_than_themselves_can_be_anagrams(self):
94+
candidates = ["LISTEN", "Silent"]
95+
expected = ["Silent"]
96+
self.assertCountEqual(find_anagrams("LISTEN", candidates), expected)
97+
98+
def test_handles_case_of_greek_letters(self):
99+
candidates = ["ΒΓΑ", "ΒΓΔ", "γβα", "αβγ"]
100+
expected = ["ΒΓΑ", "γβα"]
101+
self.assertCountEqual(find_anagrams("ΑΒΓ", candidates), expected)
102+
103+
def test_different_characters_may_have_the_same_bytes(self):
104+
candidates = ["€a"]
105+
expected = []
106+
self.assertCountEqual(find_anagrams("a⬂", candidates), expected)

0 commit comments

Comments
 (0)