Python: isogram
This commit is contained in:
parent
7cf9d0f24d
commit
6223031c18
4 changed files with 126 additions and 0 deletions
1
python/isogram/.exercism/metadata.json
Normal file
1
python/isogram/.exercism/metadata.json
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
{"track":"python","exercise":"isogram","id":"3a1d2802f4914a84830f6e577eecb6ce","url":"https://exercism.io/my/solutions/3a1d2802f4914a84830f6e577eecb6ce","handle":"DmitryKokorin","is_requester":true,"auto_approve":false}
|
||||||
61
python/isogram/README.md
Normal file
61
python/isogram/README.md
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
# Isogram
|
||||||
|
|
||||||
|
Determine if a word or phrase is an isogram.
|
||||||
|
|
||||||
|
An isogram (also known as a "nonpattern word") is a word or phrase without a repeating letter, however spaces and hyphens are allowed to appear multiple times.
|
||||||
|
|
||||||
|
Examples of isograms:
|
||||||
|
|
||||||
|
- lumberjacks
|
||||||
|
- background
|
||||||
|
- downstream
|
||||||
|
- six-year-old
|
||||||
|
|
||||||
|
The word *isograms*, however, is not an isogram, because the s repeats.
|
||||||
|
|
||||||
|
|
||||||
|
## 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 isogram_test.py`
|
||||||
|
|
||||||
|
Alternatively, you can tell Python to run the pytest module:
|
||||||
|
`python -m pytest isogram_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/isogram` 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
|
||||||
|
|
||||||
|
Wikipedia [https://en.wikipedia.org/wiki/Isogram](https://en.wikipedia.org/wiki/Isogram)
|
||||||
|
|
||||||
|
## Submitting Incomplete Solutions
|
||||||
|
|
||||||
|
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
||||||
14
python/isogram/isogram.py
Normal file
14
python/isogram/isogram.py
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
def is_isogram(string):
|
||||||
|
|
||||||
|
seen_chars = set()
|
||||||
|
|
||||||
|
for ch in string.lower():
|
||||||
|
if not ch.isalpha():
|
||||||
|
continue
|
||||||
|
|
||||||
|
if ch in seen_chars:
|
||||||
|
return False
|
||||||
|
|
||||||
|
seen_chars.add(ch)
|
||||||
|
|
||||||
|
return True
|
||||||
50
python/isogram/isogram_test.py
Normal file
50
python/isogram/isogram_test.py
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from isogram import is_isogram
|
||||||
|
|
||||||
|
# Tests adapted from `problem-specifications//canonical-data.json`
|
||||||
|
|
||||||
|
|
||||||
|
class IsogramTest(unittest.TestCase):
|
||||||
|
def test_empty_string(self):
|
||||||
|
self.assertIs(is_isogram(""), True)
|
||||||
|
|
||||||
|
def test_isogram_with_only_lower_case_characters(self):
|
||||||
|
self.assertIs(is_isogram("isogram"), True)
|
||||||
|
|
||||||
|
def test_word_with_one_duplicated_character(self):
|
||||||
|
self.assertIs(is_isogram("eleven"), False)
|
||||||
|
|
||||||
|
def test_word_with_one_duplicated_character_from_the_end_of_the_alphabet(self):
|
||||||
|
self.assertIs(is_isogram("zzyzx"), False)
|
||||||
|
|
||||||
|
def test_longest_reported_english_isogram(self):
|
||||||
|
self.assertIs(is_isogram("subdermatoglyphic"), True)
|
||||||
|
|
||||||
|
def test_word_with_duplicated_character_in_mixed_case(self):
|
||||||
|
self.assertIs(is_isogram("Alphabet"), False)
|
||||||
|
|
||||||
|
def test_word_with_duplicated_character_in_mixed_case_lowercase_first(self):
|
||||||
|
self.assertIs(is_isogram("alphAbet"), False)
|
||||||
|
|
||||||
|
def test_hypothetical_isogrammic_word_with_hyphen(self):
|
||||||
|
self.assertIs(is_isogram("thumbscrew-japingly"), True)
|
||||||
|
|
||||||
|
def test_hypothetical_word_with_duplicated_character_following_hyphen(self):
|
||||||
|
self.assertIs(is_isogram("thumbscrew-jappingly"), False)
|
||||||
|
|
||||||
|
def test_isogram_with_duplicated_hyphen(self):
|
||||||
|
self.assertIs(is_isogram("six-year-old"), True)
|
||||||
|
|
||||||
|
def test_made_up_name_that_is_an_isogram(self):
|
||||||
|
self.assertIs(is_isogram("Emily Jung Schwartzkopf"), True)
|
||||||
|
|
||||||
|
def test_duplicated_character_in_the_middle(self):
|
||||||
|
self.assertIs(is_isogram("accentor"), False)
|
||||||
|
|
||||||
|
def test_same_first_and_last_characters(self):
|
||||||
|
self.assertIs(is_isogram("angola"), False)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue