mirror of
https://github.com/Xevion/exercism.git
synced 2025-12-06 19:14:59 -06:00
anagram exercise
This commit is contained in:
1
python/anagram/.exercism/metadata.json
Normal file
1
python/anagram/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
||||
{"track":"python","exercise":"anagram","id":"c00ab57051fd4bc8ad350a2cd42349f7","url":"https://exercism.io/my/solutions/c00ab57051fd4bc8ad350a2cd42349f7","handle":"Xevion","is_requester":true,"auto_approve":false}
|
||||
56
python/anagram/README.md
Normal file
56
python/anagram/README.md
Normal file
@@ -0,0 +1,56 @@
|
||||
# Anagram
|
||||
|
||||
Given a word and a list of possible anagrams, select the correct sublist.
|
||||
|
||||
Given `"listen"` and a list of candidates like `"enlists" "google"
|
||||
"inlets" "banana"` the program should return a list containing
|
||||
`"inlets"`.
|
||||
|
||||
## 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 anagram_test.py`
|
||||
- Python 3.4+: `pytest anagram_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 anagram_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/anagram` 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
|
||||
|
||||
Inspired by the Extreme Startup game [https://github.com/rchatley/extreme_startup](https://github.com/rchatley/extreme_startup)
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
||||
4
python/anagram/anagram.py
Normal file
4
python/anagram/anagram.py
Normal file
@@ -0,0 +1,4 @@
|
||||
def find_anagrams(word, candidates):
|
||||
word, lowercandidates = word.lower(), list(map(lambda item : item.lower(), candidates))
|
||||
build = {char : word.count(char) for char in word.lower()}
|
||||
return [candidates[index] for index, candidate in enumerate(lowercandidates) if {char : candidate.count(char) for char in candidate} == build and candidate != word]
|
||||
67
python/anagram/anagram_test.py
Normal file
67
python/anagram/anagram_test.py
Normal file
@@ -0,0 +1,67 @@
|
||||
import unittest
|
||||
|
||||
from anagram import find_anagrams
|
||||
|
||||
# Python 2/3 compatibility
|
||||
if not hasattr(unittest.TestCase, 'assertCountEqual'):
|
||||
unittest.TestCase.assertCountEqual = unittest.TestCase.assertItemsEqual
|
||||
|
||||
|
||||
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.4.1
|
||||
|
||||
class AnagramTest(unittest.TestCase):
|
||||
def test_no_matches(self):
|
||||
candidates = ["hello", "world", "zombies", "pants"]
|
||||
self.assertEqual(find_anagrams("diaper", candidates), [])
|
||||
|
||||
def test_detects_two_anagrams(self):
|
||||
candidates = ["stream", "pigeon", "maters"]
|
||||
self.assertCountEqual(
|
||||
find_anagrams("master", candidates), ["stream", "maters"])
|
||||
|
||||
def test_does_not_detect_anagram_subsets(self):
|
||||
self.assertEqual(find_anagrams("good", ["dog", "goody"]), [])
|
||||
|
||||
def test_detects_anagram(self):
|
||||
candidates = ["enlists", "google", "inlets", "banana"]
|
||||
self.assertEqual(find_anagrams("listen", candidates), ["inlets"])
|
||||
|
||||
def test_detects_three_anagrams(self):
|
||||
candidates = [
|
||||
"gallery", "ballerina", "regally", "clergy", "largely", "leading"
|
||||
]
|
||||
self.assertCountEqual(
|
||||
find_anagrams("allergy", candidates),
|
||||
["gallery", "regally", "largely"])
|
||||
|
||||
def test_does_not_detect_non_anagrams_with_identical_checksum(self):
|
||||
self.assertEqual(find_anagrams("mass", ["last"]), [])
|
||||
|
||||
def test_detects_anagrams_case_insensitively(self):
|
||||
candidates = ["cashregister", "Carthorse", "radishes"]
|
||||
self.assertEqual(
|
||||
find_anagrams("Orchestra", candidates), ["Carthorse"])
|
||||
|
||||
def test_detects_anagrams_using_case_insensitive_subject(self):
|
||||
candidates = ["cashregister", "carthorse", "radishes"]
|
||||
self.assertEqual(
|
||||
find_anagrams("Orchestra", candidates), ["carthorse"])
|
||||
|
||||
def test_detects_anagrams_using_case_insensitive_possible_matches(self):
|
||||
candidates = ["cashregister", "Carthorse", "radishes"]
|
||||
self.assertEqual(
|
||||
find_anagrams("orchestra", candidates), ["Carthorse"])
|
||||
|
||||
def test_does_not_detect_an_anagram_if_the_original_word_is_repeated(self):
|
||||
self.assertEqual(find_anagrams("go", ["go Go GO"]), [])
|
||||
|
||||
def test_anagrams_must_use_all_letters_exactly_once(self):
|
||||
self.assertEqual(find_anagrams("tapper", ["patter"]), [])
|
||||
|
||||
def test_words_are_not_anagrams_of_themselves_case_insensitive(self):
|
||||
candidates = ["BANANA", "Banana", "banana"]
|
||||
self.assertEqual(find_anagrams("BANANA", candidates), [])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user