mirror of
https://github.com/Xevion/exercism.git
synced 2025-12-06 15:15:01 -06:00
scrabble score
This commit is contained in:
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":"ee423be717314687b974302d5cc82503","url":"https://exercism.io/my/solutions/ee423be717314687b974302d5cc82503","handle":"Xevion","is_requester":true,"auto_approve":false}
|
||||
89
python/scrabble-score/README.md
Normal file
89
python/scrabble-score/README.md
Normal file
@@ -0,0 +1,89 @@
|
||||
# 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 the appropriate command below ([why they are different](https://github.com/pytest-dev/pytest/issues/1629#issue-161422224)):
|
||||
|
||||
- Python 2.7: `py.test scrabble_score_test.py`
|
||||
- Python 3.4+: `pytest scrabble_score_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 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.
|
||||
3
python/scrabble-score/scrabble_score.py
Normal file
3
python/scrabble-score/scrabble_score.py
Normal file
@@ -0,0 +1,3 @@
|
||||
dat = [('AEIOULNRST', 1), ('DG', 2), ('BCMP', 3), ('FHVWY', 4), ('K', 5), ('JX', 8), ('QZ',10)]
|
||||
def score(word):
|
||||
return sum([subarr[1][1] for subarr in enumerate(dat) if char.upper() in subarr[1][0]][0] for char 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` @ v1.1.0
|
||||
|
||||
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()
|
||||
Reference in New Issue
Block a user