|
| 1 | +# Scrabble score |
| 2 | + |
| 3 | +## Task |
| 4 | + |
| 5 | +Given a word, compute the Scrabble score for that word. |
| 6 | + |
| 7 | +### Letter Values |
| 8 | + |
| 9 | +You'll need these: |
| 10 | + |
| 11 | +```text |
| 12 | +Letter Value |
| 13 | +A, E, I, O, U, L, N, R, S, T 1 |
| 14 | +D, G 2 |
| 15 | +B, C, M, P 3 |
| 16 | +F, H, V, W, Y 4 |
| 17 | +K 5 |
| 18 | +J, X 8 |
| 19 | +Q, Z 10 |
| 20 | +``` |
| 21 | + |
| 22 | +### Examples |
| 23 | + |
| 24 | +"cabbage" should be scored as worth 14 points: |
| 25 | + |
| 26 | +- 3 points for C |
| 27 | +- 1 point for A, twice |
| 28 | +- 3 points for B, twice |
| 29 | +- 2 points for G |
| 30 | +- 1 point for E |
| 31 | + |
| 32 | +And to total: |
| 33 | + |
| 34 | +- `3 + 2*1 + 2*3 + 2 + 1` |
| 35 | +- = `3 + 2 + 6 + 3` |
| 36 | +- = `5 + 9` |
| 37 | +- = 14 |
| 38 | + |
| 39 | + |
| 40 | +## Example |
| 41 | + |
| 42 | +`scrabble_score.h` |
| 43 | +```cpp |
| 44 | +#if !defined(SCRABBLE_SCORE_H) |
| 45 | +#define SCRABBLE_SCORE_H |
| 46 | + |
| 47 | +#include <string> |
| 48 | + |
| 49 | +namespace scrabble_score |
| 50 | +{ |
| 51 | + |
| 52 | +int score(const std::string &word); |
| 53 | + |
| 54 | +} |
| 55 | + |
| 56 | +#endif |
| 57 | +``` |
| 58 | + |
| 59 | +`example.cpp` |
| 60 | + |
| 61 | +```cpp |
| 62 | +#include "scrabble_score.h" |
| 63 | +#include <cctype> |
| 64 | + |
| 65 | +namespace scrabble_score |
| 66 | +{ |
| 67 | + |
| 68 | +namespace |
| 69 | +{ |
| 70 | +const int letter_scores[26] = { |
| 71 | + 1, // A |
| 72 | + 3, // B |
| 73 | + 3, // C |
| 74 | + 2, // D |
| 75 | + 1, // E |
| 76 | + 4, // F |
| 77 | + 2, // G |
| 78 | + 4, // H |
| 79 | + 1, // I |
| 80 | + 8, // J |
| 81 | + 5, // K |
| 82 | + 1, // L |
| 83 | + 3, // M |
| 84 | + 1, // N |
| 85 | + 1, // O |
| 86 | + 3, // P |
| 87 | + 10, // Q |
| 88 | + 1, // R |
| 89 | + 1, // S |
| 90 | + 1, // T |
| 91 | + 1, // U |
| 92 | + 4, // V |
| 93 | + 4, // W |
| 94 | + 8, // X |
| 95 | + 4, // Y |
| 96 | + 10 // Z |
| 97 | +}; |
| 98 | + |
| 99 | +} |
| 100 | + |
| 101 | +int score(const std::string &word) |
| 102 | +{ |
| 103 | + int result = 0; |
| 104 | + for (const char c : word) { |
| 105 | + if (std::isalpha(c)) { |
| 106 | + result += letter_scores[std::tolower(c) - 'a']; |
| 107 | + } |
| 108 | + } |
| 109 | + return result; |
| 110 | +} |
| 111 | + |
| 112 | +} |
| 113 | +``` |
| 114 | +
|
| 115 | +
|
| 116 | +## Concepts |
| 117 | +
|
| 118 | +- .h file: the most common approach is to use a `.h` for the declarations |
| 119 | +- .cpp file: the most common approach is to use a `.cpp` for the definitions |
| 120 | +- include guard: protect against multiple inclusion within a single translation unit |
| 121 | +- .cpp includes .h: a `.cpp` file should include its `.h` file |
| 122 | +- including directives: `<string>` |
| 123 | +- namespaces: the function is member of a namespace |
| 124 | +- functions: used as the main entry point for the exercise |
| 125 | +- function arguments: word is passed as argument |
| 126 | +- integers: an `int` is for keeping score |
| 127 | +- signed integers: the core guidelines recommend signed integers for arithmetic |
| 128 | +- equality operators: `==` and `!=` |
| 129 | +- return values: the result is returned by the function |
| 130 | +- const-qualification: input and letters in the word do not change |
| 131 | +- conditional statements: `if` to filter out non-alphanumeric characters |
| 132 | +- functions: create function with a compatible signature |
| 133 | +- `noexcept` function specifier: solution should not throw |
| 134 | +- range for-loop or for-loop: to get though characters in the input |
| 135 | +- arrays: to hold letter scores |
| 136 | +- template specializations: store score per letter in a hash-map |
| 137 | +- pass by `const` reference: large string may be expensive to copy |
| 138 | +
|
| 139 | +
|
| 140 | +## Alternative approaches |
| 141 | +
|
| 142 | +- using `tranform_reduce` to get from the letters to letter scores and from the letter scores to the final score |
| 143 | +- using `tranform` to get from the letters to letter scores and `accumulate` from the letter scores to the final score |
| 144 | +- substracting 'a' from letter code and using that as an index in array of scores |
| 145 | +- creating an accumulator and adding letters' scores in a loop |
| 146 | +- using `boost::flat_map` as a container for letters' scores |
| 147 | +- using `std::array` as a container for letters' scores |
| 148 | +
|
| 149 | +
|
| 150 | +## Common mistakes |
| 151 | +
|
| 152 | +- making copy of the input instead of passing as `const` reference |
| 153 | +- using `std::map` instead of `std::unordered_map`to store scores |
| 154 | +- using `switch-case` or `if` to map letters to scores |
| 155 | +- not making letter scores `const` or `constexpr` |
| 156 | +- using C-style arrays |
| 157 | +- explicitly using iterators |
| 158 | +
|
| 159 | +
|
| 160 | +## Common suggestions to improve a solution |
| 161 | +
|
| 162 | +- use more cache-friendly container, such as `boost::flat_map` or `std::array` |
| 163 | +- use range-for loop instead of for loop with an index |
| 164 | +- use `accumulate` and `transform` |
| 165 | +- use `transform_reduce` |
| 166 | +- use `const` for individual characters and input word |
| 167 | +- use `constexpr` for data known at compile time |
| 168 | +- add `noexcept` to mark all functions that are not expected to throw exceptions |
| 169 | +- use `std::string_view` instead of `std::string` |
0 commit comments