Python: scrabble_score
This commit is contained in:
parent
0dd5b31529
commit
c3f4773fd7
4 changed files with 144 additions and 0 deletions
1
python/scrabble-score/.exercism/metadata.json
Normal file
1
python/scrabble-score/.exercism/metadata.json
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"track":"python","exercise":"scrabble-score","id":"047f7e0f63664072b45c1f57ef807e01","url":"https://exercism.io/my/solutions/047f7e0f63664072b45c1f57ef807e01","handle":"DmitryKokorin","is_requester":true,"auto_approve":false}
|
||||
87
python/scrabble-score/README.md
Normal file
87
python/scrabble-score/README.md
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
# Scrabble Score
|
||||
|
||||
Given a word, compute the scrabble score for that word.
|
||||
|
||||
## Letter Values
|
||||
|
||||
You'll need these:
|
||||
|
||||
```text
|
||||
Letter Value
|
||||
A, E, I, O, U, L, N, R, S, T 1
|
||||
D, G 2
|
||||
B, C, M, P 3
|
||||
F, H, V, W, Y 4
|
||||
K 5
|
||||
J, X 8
|
||||
Q, Z 10
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
"cabbage" should be scored as worth 14 points:
|
||||
|
||||
- 3 points for C
|
||||
- 1 point for A, twice
|
||||
- 3 points for B, twice
|
||||
- 2 points for G
|
||||
- 1 point for E
|
||||
|
||||
And to total:
|
||||
|
||||
- `3 + 2*1 + 2*3 + 2 + 1`
|
||||
- = `3 + 2 + 6 + 3`
|
||||
- = `5 + 9`
|
||||
- = 14
|
||||
|
||||
## Extensions
|
||||
|
||||
- You can play a double or a triple letter.
|
||||
- You can play a double or a triple word.
|
||||
|
||||
|
||||
## 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 scrabble_score_test.py`
|
||||
|
||||
Alternatively, you can tell Python to run the pytest module:
|
||||
`python -m pytest scrabble_score_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/scrabble-score` 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.
|
||||
12
python/scrabble-score/scrabble_score.py
Normal file
12
python/scrabble-score/scrabble_score.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
SCORES = {'A': 1, 'E': 1, 'I': 1, 'O': 1, 'U': 1, 'L': 1, 'N': 1, 'R': 1,
|
||||
'S': 1, 'T': 1,
|
||||
'D': 2, 'G': 2,
|
||||
'B': 3, 'C': 3, 'M': 3, 'P': 3,
|
||||
'F': 4, 'H': 4, 'V': 4, 'W': 4, 'Y': 4,
|
||||
'K': 5,
|
||||
'J': 8, 'X': 8,
|
||||
'Q': 10, 'Z': 10}
|
||||
|
||||
|
||||
def score(word):
|
||||
return sum(SCORES[ch.upper()] for ch in word)
|
||||
44
python/scrabble-score/scrabble_score_test.py
Normal file
44
python/scrabble-score/scrabble_score_test.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import unittest
|
||||
|
||||
from scrabble_score import score
|
||||
|
||||
# Tests adapted from `problem-specifications//canonical-data.json`
|
||||
|
||||
|
||||
class ScrabbleScoreTest(unittest.TestCase):
|
||||
def test_lowercase_letter(self):
|
||||
self.assertEqual(score("a"), 1)
|
||||
|
||||
def test_uppercase_letter(self):
|
||||
self.assertEqual(score("A"), 1)
|
||||
|
||||
def test_valuable_letter(self):
|
||||
self.assertEqual(score("f"), 4)
|
||||
|
||||
def test_short_word(self):
|
||||
self.assertEqual(score("at"), 2)
|
||||
|
||||
def test_short_valuable_word(self):
|
||||
self.assertEqual(score("zoo"), 12)
|
||||
|
||||
def test_medium_word(self):
|
||||
self.assertEqual(score("street"), 6)
|
||||
|
||||
def test_medium_valuable_word(self):
|
||||
self.assertEqual(score("quirky"), 22)
|
||||
|
||||
def test_long_mixed_case_word(self):
|
||||
self.assertEqual(score("OxyphenButazone"), 41)
|
||||
|
||||
def test_english_like_word(self):
|
||||
self.assertEqual(score("pinata"), 8)
|
||||
|
||||
def test_empty_input(self):
|
||||
self.assertEqual(score(""), 0)
|
||||
|
||||
def test_entire_alphabet_available(self):
|
||||
self.assertEqual(score("abcdefghijklmnopqrstuvwxyz"), 87)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue