Skip to content

Commit 3300013

Browse files
authored
General fixes in scrabble-score approaches (exercism#2865)
* fix typos/grammar * format as one sentence per line * in map-reduce approach: convert to list for better readability
1 parent e28da7b commit 3300013

File tree

4 files changed

+23
-37
lines changed

4 files changed

+23
-37
lines changed

exercises/practice/scrabble-score/.approaches/if-statements/content.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,17 @@ class Scrabble {
3535
```
3636

3737
This approach defines a [`private`][private] [`final`][final] variable to be returned by the `getScore()` method.
38-
It is `private` because it does not need to be directly accessed from outside the class, and it is `final`
39-
because its value does not need to be changed once it is set.
38+
It is `private` because it does not need to be directly accessed from outside the class, and it is `final` because its value does not need to be changed once it is set.
4039

4140
In the constructor, a local variable is defined for being updated in the [`for` loop][for-loop].
4241

4342
~~~~exercism/note
44-
Using the same for a variable in a nested local scope that is used in its enclosing higher scope is called
45-
[variable shadowing](https://www.geeksforgeeks.org/shadowing-in-java/).
43+
Using the same for a variable in a nested local scope that is used in its enclosing higher scope is called [variable shadowing](https://www.geeksforgeeks.org/shadowing-in-java/).
4644
~~~~
4745

4846
The variable is updated by a series of `if` statements that checks each letter of the uppercased word.
49-
The letter is selected as a `String` by the [`substring()`][substring] method and is passed to the
50-
[`contains()`][contains] method for the `String` representing the letters for a particular score.
51-
The first `if` statement checks for the most common letters, and each succeeding `if` statement
52-
checks for letters less common than the statement before it.
47+
The letter is selected as a `String` by the [`substring()`][substring] method and is passed to the [`contains()`][contains] method for the `String` representing the letters for a particular score.
48+
The first `if` statement checks for the most common letters, and each succeeding `if` statement checks for letters less common than the statement before it.
5349
When the loop is done, the class-level `score` variable is set to the value of the local `score` variable.
5450

5551
[private]: https://en.wikibooks.org/wiki/Java_Programming/Keywords/private

exercises/practice/scrabble-score/.approaches/introduction.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
# Introduction
22

3-
There are various idiomatiuc ways to solve Scrabble Score.
4-
The approaches could be to use a series of `if` statements, or a single `switch` statment.
3+
There are various idiomatic ways to solve Scrabble Score.
4+
The approaches could be to use a series of `if` statements, or a single `switch` statement.
55
Another approach could be to look up the score in a `HashMap` from inside the `reduce()` method.
66

77
## General guidance
88

9-
Regardless of the approach used, one thing to look out for is to whether to calculate the score
10-
in the constructor (or a method called by the constructor) or in the `getScore()` method.
11-
A benefit to calculating in the constructor is that the score is calculated only once,
12-
no matter how many times `getScore()` is called.
13-
A benefit to calculating in `getScore()` is that, if it is not called,
14-
then the calculation never has to be performed.
9+
Regardless of the approach used, one thing to look out for is to whether to calculate the score in the constructor (or a method called by the constructor) or in the `getScore()` method.
10+
A benefit to calculating in the constructor is that the score is calculated only once, no matter how many times `getScore()` is called.
11+
A benefit to calculating in `getScore()` is that, if it is not called, then the calculation never has to be performed.
1512
But then, in that case, why instantiate the `Scrabble` object at all?
1613

1714
## Approach: `if` statements
@@ -149,8 +146,7 @@ For more information, check the [`HashMap` with `reduce()` approach][approach-ma
149146

150147
## Which approach to use?
151148

152-
Since benchmarking with the [Java Microbenchmark Harness][jmh] is currently outside the scope of this document,
153-
the choice between the approaches can be made by perceived readability.
149+
Since benchmarking with the [Java Microbenchmark Harness][jmh] is currently outside the scope of this document, the choice between the approaches can be made by perceived readability.
154150

155151
[approach-if-statements]: https://exercism.org/tracks/java/exercises/scrabble-score/approaches/if-statements
156152
[approach-switch-statement]: https://exercism.org/tracks/java/exercises/scrabble-score/approaches/switch-statement

exercises/practice/scrabble-score/.approaches/map-reduce/content.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,23 @@ class Scrabble {
4646
This approach starts by importing from packages for what is needed.
4747

4848
A [`private`][private] [`final`][final] variable is defined to be returned by the `getScore()` method.
49-
It is `private` because it does not need to be directly accessed from outside the class, and it is `final`
50-
because its value does not need to be changed once it is set.
49+
It is `private` because it does not need to be directly accessed from outside the class, and it is `final` because its value does not need to be changed once it is set.
5150

5251
Several `private` `final` [`static`][static] variables are then defined:
53-
a [`HashMap`][hashmap] to hold the lookups of the scores for the letters;
54-
a `String` array of the letters grouped by their common score;
55-
and an `int` array of the scores for the letters.
52+
53+
- a [`HashMap`][hashmap] to hold the lookups of the scores for the letters
54+
- a `String` array of the letters grouped by their common score
55+
- an `int` array of the scores for the letters
56+
5657
They are `static` because they don't need to differ between object instantiations, so they can belong to the class itself.
5758

58-
In a [static block][static-block], the [`IntStream.range()`][range] method is called to iterate an index from `0`
59-
up to but including the length of the array of letters.
60-
In a [`forEach()`][foreach] method, each index value is passed into a [lambda][lambda] which calls the [`chars{}`][chars]
61-
method on each string at the index of the letters array.
59+
In a [static block][static-block], the [`IntStream.range()`][range] method is called to iterate an index from `0` up to but including the length of the array of letters.
60+
In a [`forEach()`][foreach] method, each index value is passed into a [lambda][lambda] which calls the [`chars{}`][chars] method on each string at the index of the letters array.
6261
In another `forEach`, each letter is passed into a lambda that adds the letter and its corresponding score to the `HashMap`.
6362
This works because the groups of letters and their scores are at the same index in their respective arrays.
6463

6564
In the constructor, `chars()` is called on the uppercased word and chained to the [`reduce()`][reduce] method.
66-
The accumulator is initialized to `0`, and the accumulator and each letter is passed to a lambda that adds the score
67-
looked up from the `HashMap` for the letter.
65+
The accumulator is initialized to `0`, and the accumulator and each letter is passed to a lambda that adds the score looked up from the `HashMap` for the letter.
6866
The score variable is set to the value returned from `reduce()`, which is the value of its accumulator.
6967

7068
[private]: https://en.wikibooks.org/wiki/Java_Programming/Keywords/private

exercises/practice/scrabble-score/.approaches/switch-statement/content.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,22 @@ class Scrabble {
4545
```
4646

4747
This approach defines a [`private`][private] [`final`][final] variable to be returned by the `getScore()` method.
48-
It is `private` because it does not need to be directly accessed from outside the class, and it is `final`
49-
because its value does not need to be changed once it is set.
48+
It is `private` because it does not need to be directly accessed from outside the class, and it is `final` because its value does not need to be changed once it is set.
5049

5150
In the constructor, a local variable is defined for being updated in the [`for` loop][for-loop].
5251

5352
~~~~exercism/note
54-
Using the same for a variable in a nested local scope that is used in its enclosing higher scope is called
55-
[variable shadowing](https://www.geeksforgeeks.org/shadowing-in-java/).
53+
Using the same name for a variable in a nested local scope that is used in its enclosing higher scope is called [variable shadowing](https://www.geeksforgeeks.org/shadowing-in-java/).
5654
~~~~
5755

5856
The variable is updated by a [`switch`][switch] statement that checks each letter of the lowercased word.
5957

6058
~~~~exercism/note
61-
If most of the input will already be lower case, it is a bit more performant to normalize the input as lowercased,
62-
since fewer characters will need to be changed.
59+
If most of the input will already be lower case, it is a bit more performant to normalize the input as lowercased, since fewer characters will need to be changed.
6360
However, it may be considered that to use upper case letters is more readable.
6461
~~~~
6562

66-
The letter is selected as a `char` by the [`charAt()`][charat] method and is passed to the
67-
`switch`, with each case representing the letters for a particular score.
63+
The letter is selected as a `char` by the [`charAt()`][charat] method and is passed to the `switch`, with each case representing the letters for a particular score.
6864
When the loop is done, the class-level `score` variable is set to the value of the local `score` variable.
6965

7066
[private]: https://en.wikibooks.org/wiki/Java_Programming/Keywords/private

0 commit comments

Comments
 (0)