From 74f8471f6119f54e55e395d3f06b385c366ed41e Mon Sep 17 00:00:00 2001 From: Xevion Date: Sat, 13 Jul 2019 17:35:02 -0500 Subject: [PATCH] scrabble score --- python/scrabble-score/.exercism/metadata.json | 1 + python/scrabble-score/README.md | 89 +++++++++++++++++++ python/scrabble-score/scrabble_score.py | 3 + python/scrabble-score/scrabble_score_test.py | 44 +++++++++ 4 files changed, 137 insertions(+) create mode 100644 python/scrabble-score/.exercism/metadata.json create mode 100644 python/scrabble-score/README.md create mode 100644 python/scrabble-score/scrabble_score.py create mode 100644 python/scrabble-score/scrabble_score_test.py diff --git a/python/scrabble-score/.exercism/metadata.json b/python/scrabble-score/.exercism/metadata.json new file mode 100644 index 0000000..e5aaa10 --- /dev/null +++ b/python/scrabble-score/.exercism/metadata.json @@ -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} \ No newline at end of file diff --git a/python/scrabble-score/README.md b/python/scrabble-score/README.md new file mode 100644 index 0000000..adc30c4 --- /dev/null +++ b/python/scrabble-score/README.md @@ -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. diff --git a/python/scrabble-score/scrabble_score.py b/python/scrabble-score/scrabble_score.py new file mode 100644 index 0000000..a91447f --- /dev/null +++ b/python/scrabble-score/scrabble_score.py @@ -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) \ No newline at end of file diff --git a/python/scrabble-score/scrabble_score_test.py b/python/scrabble-score/scrabble_score_test.py new file mode 100644 index 0000000..8300bee --- /dev/null +++ b/python/scrabble-score/scrabble_score_test.py @@ -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()