Skip to content

Commit

Permalink
Merge branch 'main' into add-approach-bob
Browse files Browse the repository at this point in the history
  • Loading branch information
jagdish-15 authored Nov 18, 2024
2 parents e509381 + 85652b5 commit e67fc43
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 56 deletions.
49 changes: 33 additions & 16 deletions exercises/concept/annalyns-infiltration/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
# Instructions

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.
In this exercise, you'll implement the quest logic for a new RPG game that a friend is developing.
The game's main character is Annalyn, a brave girl with a fierce and loyal pet dog.
Unfortunately, disaster strikes: her best friend was kidnapped while searching for berries in the forest.
Annalyn will try to find and rescue her friend, optionally taking her dog along on the quest.

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.
After some time spent following the trail, Annalyn discovers the camp where her friend is imprisoned.
It turns out there are two kidnappers: a mighty knight and a cunning archer.

Having found the kidnappers, Annalyn considers which of the following actions she can engage in:

- 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.
- Spy: the group can be spied upon if at least one of them is awake. Otherwise, spying is a waste of time.
- 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.
- _Free prisoner_: Annalyn can try sneaking into the camp to free the prisoner.
This is a risky thing to do and can only succeed in one of two ways:
- If Annalyn has her pet dog with her she can rescue the prisoner if the archer is asleep.
- 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.
- Spy: the group can be spied upon if at least one of them is awake.
Otherwise, spying is a waste of time.
- Signal prisoner: the prisoner can be signaled using bird sounds if the prisoner is awake and the archer is sleeping.
Archers are trained in bird signaling and could intercept the message if they are awake.
- _Free prisoner_: Annalyn can attempt to sneak into the camp to free the prisoner.
This is risky and can only succeed in one of two ways:
- If Annalyn has her pet dog, she can rescue the prisoner if the archer is asleep.
The knight is scared of the dog and the archer will not have time to get ready before Annalyn and the prisoner can escape.
- If Annalyn does not have her dog then she and the prisoner must be very sneaky!
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.
- If Annalyn does not have her pet dog, then she and the prisoner must be very sneaky!
Annalyn can free the prisoner if the prisoner is awake and both the knight and archer are sleeping.
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.

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.
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.

## 1. Check if a fast attack can be made

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`:
Implement the (_static_) `AnnalynsInfiltration.canFastAttack()` method, which takes a boolean value indicating whether the knight is awake.
This method returns `true` if a fast attack can be made based on the state of the knight.
Otherwise, it returns `false`:

```java
boolean knightIsAwake = true;
Expand All @@ -30,7 +39,9 @@ AnnalynsInfiltration.canFastAttack(knightIsAwake);

## 2. Check if the group can be spied upon

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`:
Implement the (_static_) `AnnalynsInfiltration.canSpy()` method, which takes three boolean values indicating whether the knight, archer, and prisoner, respectively, are awake.
The method returns `true` if the group can be spied upon based on the state of the three characters.
Otherwise, it returns `false`:

```java
boolean knightIsAwake = false;
Expand All @@ -40,9 +51,11 @@ AnnalynsInfiltration.canSpy(knightIsAwake, archerIsAwake, prisonerIsAwake);
// => true
```

## 3. Check if the prisoner can be signalled
## 3. Check if the prisoner can be signaled

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`:
Implement the (_static_) `AnnalynsInfiltration.canSignalPrisoner()` method, which takes two boolean values indicating whether the archer and the prisoner, respectively, are awake.
The method returns `true` if the prisoner can be signaled based on the state of the two characters.
Otherwise, it returns `false`:

```java
boolean archerIsAwake = false;
Expand All @@ -53,7 +66,11 @@ AnnalynsInfiltration.canSignalPrisoner(archerIsAwake, prisonerIsAwake);

## 4. Check if the prisoner can be freed

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`:
Implement the (_static_) `AnnalynsInfiltration.canFreePrisoner()` method, which takes four boolean values.
The first three parameters indicate whether the knight, archer, and prisoner, respectively, are awake.
The last parameter indicates whether 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 the presence of Annalyn's pet dog.
Otherwise, it returns `false`:

```java
boolean knightIsAwake = false;
Expand Down
4 changes: 2 additions & 2 deletions exercises/concept/annalyns-infiltration/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Booleans in Java are represented by the `boolean` type, which values can be eith
Java supports three boolean operators:

- `!` (NOT): negates the boolean
- `&&` (AND): takes two booleans and results in true if they're both true
- `||` (OR): results in true if any of the two booleans is true
- `&&` (AND): takes two booleans and returns `true` if they're both `true`
- `||` (OR): returns `true` if any of the two booleans is `true`

### Examples

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,17 @@ class Scrabble {
```

This approach defines a [`private`][private] [`final`][final] variable to be returned by the `getScore()` method.
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.
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.

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

~~~~exercism/note
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/).
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/).
~~~~

The variable is updated by a series of `if` statements that checks each letter of the uppercased word.
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.
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.
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.
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.
When the loop is done, the class-level `score` variable is set to the value of the local `score` variable.

[private]: https://en.wikibooks.org/wiki/Java_Programming/Keywords/private
Expand Down
16 changes: 6 additions & 10 deletions exercises/practice/scrabble-score/.approaches/introduction.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
# Introduction

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

## General guidance

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.
A benefit to calculating in the constructor is that the score is calculated only once,
no matter how many times `getScore()` is called.
A benefit to calculating in `getScore()` is that, if it is not called,
then the calculation never has to be performed.
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.
A benefit to calculating in the constructor is that the score is calculated only once, no matter how many times `getScore()` is called.
A benefit to calculating in `getScore()` is that, if it is not called, then the calculation never has to be performed.
But then, in that case, why instantiate the `Scrabble` object at all?

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

## Which approach to use?

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.
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.

[approach-if-statements]: https://exercism.org/tracks/java/exercises/scrabble-score/approaches/if-statements
[approach-switch-statement]: https://exercism.org/tracks/java/exercises/scrabble-score/approaches/switch-statement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,23 @@ class Scrabble {
This approach starts by importing from packages for what is needed.

A [`private`][private] [`final`][final] variable is defined to be returned by the `getScore()` method.
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.
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.

Several `private` `final` [`static`][static] variables are then defined:
a [`HashMap`][hashmap] to hold the lookups of the scores for the letters;
a `String` array of the letters grouped by their common score;
and an `int` array of the scores for the letters.

- a [`HashMap`][hashmap] to hold the lookups of the scores for the letters
- a `String` array of the letters grouped by their common score
- an `int` array of the scores for the letters

They are `static` because they don't need to differ between object instantiations, so they can belong to the class itself.

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.
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.
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.
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.
In another `forEach`, each letter is passed into a lambda that adds the letter and its corresponding score to the `HashMap`.
This works because the groups of letters and their scores are at the same index in their respective arrays.

In the constructor, `chars()` is called on the uppercased word and chained to the [`reduce()`][reduce] method.
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.
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.
The score variable is set to the value returned from `reduce()`, which is the value of its accumulator.

[private]: https://en.wikibooks.org/wiki/Java_Programming/Keywords/private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,22 @@ class Scrabble {
```

This approach defines a [`private`][private] [`final`][final] variable to be returned by the `getScore()` method.
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.
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.

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

~~~~exercism/note
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/).
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/).
~~~~

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

~~~~exercism/note
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.
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.
However, it may be considered that to use upper case letters is more readable.
~~~~

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.
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.
When the loop is done, the class-level `score` variable is set to the value of the local `score` variable.

[private]: https://en.wikibooks.org/wiki/Java_Programming/Keywords/private
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/yacht/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
]
},
"blurb": "Score a single throw of dice in the game Yacht.",
"source": "James Kilfiger, using wikipedia",
"source": "James Kilfiger, using Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Yacht_(dice_game)"
}

0 comments on commit e67fc43

Please sign in to comment.