You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: exercises/concept/annalyns-infiltration/.docs/instructions.md
+33-16Lines changed: 33 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,35 @@
1
1
# Instructions
2
2
3
-
In this exercise, you'll be implementing the quest logic for a new RPG game a friend is developing. The game's main character is Annalyn, a brave girl with a fierce and loyal pet dog. Unfortunately, disaster strikes, as her best friend was kidnapped while searching for berries in the forest. Annalyn will try to find and free her best friend, optionally taking her dog with her on this quest.
3
+
In this exercise, you'll implement the quest logic for a new RPG game that a friend is developing.
4
+
The game's main character is Annalyn, a brave girl with a fierce and loyal pet dog.
5
+
Unfortunately, disaster strikes: her best friend was kidnapped while searching for berries in the forest.
6
+
Annalyn will try to find and rescue her friend, optionally taking her dog along on the quest.
4
7
5
-
After some time spent following her best friend's trail, she finds the camp in which her best friend is imprisoned. It turns out there are two kidnappers: a mighty knight and a cunning archer.
8
+
After some time spent following the trail, Annalyn discovers the camp where her friend is imprisoned.
9
+
It turns out there are two kidnappers: a mighty knight and a cunning archer.
6
10
7
11
Having found the kidnappers, Annalyn considers which of the following actions she can engage in:
8
12
9
-
- Fast attack: a fast attack can be made if the knight is sleeping, as it takes time for him to get his armor on, so he will be vulnerable.
10
-
- Spy: the group can be spied upon if at least one of them is awake. Otherwise, spying is a waste of time.
11
-
- Signal prisoner: the prisoner can be signalled using bird sounds if the prisoner is awake and the archer is sleeping, as archers are trained in bird signaling, so they could intercept the message.
12
-
-_Free prisoner_: Annalyn can try sneaking into the camp to free the prisoner.
13
-
This is a risky thing to do and can only succeed in one of two ways:
14
-
- If Annalyn has her pet dog with her she can rescue the prisoner if the archer is asleep.
13
+
- Fast attack: a fast attack can be made if the knight is sleeping, as it takes time for him to put on his armor, leaving him vulnerable.
14
+
- Spy: the group can be spied upon if at least one of them is awake.
15
+
Otherwise, spying is a waste of time.
16
+
- Signal prisoner: the prisoner can be signaled using bird sounds if the prisoner is awake and the archer is sleeping.
17
+
Archers are trained in bird signaling and could intercept the message if they are awake.
18
+
-_Free prisoner_: Annalyn can attempt to sneak into the camp to free the prisoner.
19
+
This is risky and can only succeed in one of two ways:
20
+
- If Annalyn has her pet dog, she can rescue the prisoner if the archer is asleep.
15
21
The knight is scared of the dog and the archer will not have time to get ready before Annalyn and the prisoner can escape.
16
-
- If Annalyn does not have her dog then she and the prisoner must be very sneaky!
17
-
Annalyn can free the prisoner if the prisoner is awake and the knight and archer are both sleeping, but if the prisoner is sleeping they can't be rescued: the prisoner would be startled by Annalyn's sudden appearance and wake up the knight and archer.
22
+
- If Annalyn does not have her pet dog, then she and the prisoner must be very sneaky!
23
+
Annalyn can free the prisoner if the prisoner is awake and both the knight and archer are sleeping.
24
+
However, if the prisoner is sleeping, they can't be rescued, as the prisoner would be startled by Annalyn's sudden appearance and wake up the knight and archer.
18
25
19
-
You have four tasks: to implement the logic for determining if the above actions are available based on the state of the three characters found in the forest and whether Annalyn's pet dog is present or not.
26
+
You have four tasks: to implement the logic for determining if the above actions are available based on the state of the three characters in the forest and whether Annalyn's pet dog is present or not.
20
27
21
28
## 1. Check if a fast attack can be made
22
29
23
-
Implement the (_static_) `AnnalynsInfiltration.canFastAttack()` method that takes a boolean value that indicates if the knight is awake. This method returns `true` if a fast attack can be made based on the state of the knight. Otherwise, returns `false`:
30
+
Implement the (_static_) `AnnalynsInfiltration.canFastAttack()` method, which takes a boolean value indicating whether the knight is awake.
31
+
This method returns `true` if a fast attack can be made based on the state of the knight.
Implement the (_static_) `AnnalynsInfiltration.canSpy()` method that takes three boolean values, indicating if the knight, archer and the prisoner, respectively, are awake. The method returns `true` if the group can be spied upon, based on the state of the three characters. Otherwise, returns `false`:
42
+
Implement the (_static_) `AnnalynsInfiltration.canSpy()` method, which takes three boolean values indicating whether the knight, archer, and prisoner, respectively, are awake.
43
+
The method returns `true` if the group can be spied upon based on the state of the three characters.
Implement the (_static_) `AnnalynsInfiltration.canSignalPrisoner()` method that takes two boolean values, indicating if the archer and the prisoner, respectively, are awake. The method returns `true` if the prisoner can be signalled, based on the state of the two characters. Otherwise, returns `false`:
56
+
Implement the (_static_) `AnnalynsInfiltration.canSignalPrisoner()` method, which takes two boolean values indicating whether the archer and the prisoner, respectively, are awake.
57
+
The method returns `true` if the prisoner can be signaled based on the state of the two characters.
Implement the (_static_) `AnnalynsInfiltration.canFreePrisoner()` method that takes four boolean values. The first three parameters indicate if the knight, archer and the prisoner, respectively, are awake. The last parameter indicates if Annalyn's pet dog is present. The method returns `true` if the prisoner can be freed based on the state of the three characters and Annalyn's pet dog's presence. Otherwise, it returns `false`:
69
+
Implement the (_static_) `AnnalynsInfiltration.canFreePrisoner()` method, which takes four boolean values.
70
+
The first three parameters indicate whether the knight, archer, and prisoner, respectively, are awake.
71
+
The last parameter indicates whether Annalyn's pet dog is present.
72
+
The method returns `true` if the prisoner can be freed based on the state of the three characters and the presence of Annalyn's pet dog.
Copy file name to clipboardExpand all lines: exercises/practice/scrabble-score/.approaches/if-statements/content.md
+4-8Lines changed: 4 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,21 +35,17 @@ class Scrabble {
35
35
```
36
36
37
37
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.
40
39
41
40
In the constructor, a local variable is defined for being updated in the [`for` loop][for-loop].
42
41
43
42
~~~~exercism/note
44
-
Using the same for a variable in a nested local scope that is used in its enclosing higher scope is called
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/).
46
44
~~~~
47
45
48
46
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.
53
49
When the loop is done, the class-level `score` variable is set to the value of the local `score` variable.
Copy file name to clipboardExpand all lines: exercises/practice/scrabble-score/.approaches/introduction.md
+6-10Lines changed: 6 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,14 @@
1
1
# Introduction
2
2
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.
5
5
Another approach could be to look up the score in a `HashMap` from inside the `reduce()` method.
6
6
7
7
## General guidance
8
8
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.
15
12
But then, in that case, why instantiate the `Scrabble` object at all?
16
13
17
14
## Approach: `if` statements
@@ -149,8 +146,7 @@ For more information, check the [`HashMap` with `reduce()` approach][approach-ma
149
146
150
147
## Which approach to use?
151
148
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.
Copy file name to clipboardExpand all lines: exercises/practice/scrabble-score/.approaches/map-reduce/content.md
+9-11Lines changed: 9 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,25 +46,23 @@ class Scrabble {
46
46
This approach starts by importing from packages for what is needed.
47
47
48
48
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.
51
50
52
51
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
+
56
57
They are `static` because they don't need to differ between object instantiations, so they can belong to the class itself.
57
58
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.
62
61
In another `forEach`, each letter is passed into a lambda that adds the letter and its corresponding score to the `HashMap`.
63
62
This works because the groups of letters and their scores are at the same index in their respective arrays.
64
63
65
64
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.
68
66
The score variable is set to the value returned from `reduce()`, which is the value of its accumulator.
Copy file name to clipboardExpand all lines: exercises/practice/scrabble-score/.approaches/switch-statement/content.md
+4-8Lines changed: 4 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -45,26 +45,22 @@ class Scrabble {
45
45
```
46
46
47
47
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.
50
49
51
50
In the constructor, a local variable is defined for being updated in the [`for` loop][for-loop].
52
51
53
52
~~~~exercism/note
54
-
Using the same for a variable in a nested local scope that is used in its enclosing higher scope is called
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/).
56
54
~~~~
57
55
58
56
The variable is updated by a [`switch`][switch] statement that checks each letter of the lowercased word.
59
57
60
58
~~~~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.
63
60
However, it may be considered that to use upper case letters is more readable.
64
61
~~~~
65
62
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.
68
64
When the loop is done, the class-level `score` variable is set to the value of the local `score` variable.
0 commit comments