Skip to content

Commit

Permalink
Chnaging the introduction.md for consistancy
Browse files Browse the repository at this point in the history
  • Loading branch information
jagdish-15 committed Nov 18, 2024
2 parents f0543c3 + e67fc43 commit ad27bb2
Show file tree
Hide file tree
Showing 14 changed files with 439 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
57 changes: 57 additions & 0 deletions exercises/practice/bob/.approaches/method-based/content.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
# Method-Based Approach

<<<<<<< HEAD

Check failure on line 3 in exercises/practice/bob/.approaches/method-based/content.md

View workflow job for this annotation

GitHub Actions / Lint Markdown files

Heading style

exercises/practice/bob/.approaches/method-based/content.md:3 MD003/heading-style Heading style [Expected: atx; Actual: setext] https://github.com/DavidAnson/markdownlint/blob/v0.35.0/doc/md003.md

Check failure on line 3 in exercises/practice/bob/.approaches/method-based/content.md

View workflow job for this annotation

GitHub Actions / Lint Markdown files

Headings should be surrounded by blank lines

exercises/practice/bob/.approaches/method-based/content.md:3 MD022/blanks-around-headings Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "<<<<<<< HEAD"] https://github.com/DavidAnson/markdownlint/blob/v0.35.0/doc/md022.md

Check failure on line 3 in exercises/practice/bob/.approaches/method-based/content.md

View workflow job for this annotation

GitHub Actions / Lint Markdown files

Multiple top-level headings in the same document

exercises/practice/bob/.approaches/method-based/content.md:3 MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "<<<<<<< HEAD"] https://github.com/DavidAnson/markdownlint/blob/v0.35.0/doc/md025.md
=======
In this approach, the different conditions for Bob’s responses are separated into dedicated private methods within the `Bob` class. This method-based approach improves readability and modularity by organizing each condition check into its own method, making the main response method easier to understand and maintain.

The main `hey` method determines Bob’s response by delegating each condition to a helper method (`isSilent`, `isYelling`, and `isAsking`), each encapsulating specific logic.

## Explanation

This approach simplifies the main method `hey` by breaking down each response condition into helper methods:

1. **Trimming the Input**:
The `input` is trimmed using the `String` [`trim()`][trim] method to remove any leading or trailing whitespace. This helps to accurately detect if the input is empty and should prompt a `"Fine. Be that way!"` response.

2. **Delegating to Helper Methods**:
Each condition is evaluated using the following helper methods:

- **`isSilent`**: Checks if the trimmed input has no characters.
- **`isYelling`**: Checks if the input is all uppercase and contains at least one alphabetic character, indicating shouting.
- **`isAsking`**: Verifies if the trimmed input ends with a question mark.

This modular approach keeps each condition encapsulated, enhancing code clarity.

3. **Order of Checks**:
The order of checks within `hey` is important:
- Silence is evaluated first, as it requires an immediate response.
- Shouted questions take precedence over individual checks for yelling and asking.
- Yelling comes next, requiring its response if not combined with a question.
- Asking (a non-shouted question) is checked afterward.

This ordering ensures that Bob’s response matches the expected behavior without redundancy.

## Code structure

>>>>>>> e67fc434c32715ee323ebc5079ef0497932738f6
```java

Check failure on line 37 in exercises/practice/bob/.approaches/method-based/content.md

View workflow job for this annotation

GitHub Actions / Lint Markdown files

Fenced code blocks should be surrounded by blank lines

exercises/practice/bob/.approaches/method-based/content.md:37 MD031/blanks-around-fences Fenced code blocks should be surrounded by blank lines [Context: "```java"] https://github.com/DavidAnson/markdownlint/blob/v0.35.0/doc/md031.md
class Bob {
String hey(String input) {
Expand Down Expand Up @@ -35,6 +69,7 @@ class Bob {
}
```

<<<<<<< HEAD
In this approach, the different conditions for Bob’s responses are separated into dedicated private methods within the `Bob` class. This method-based approach improves readability and modularity by organizing each condition check into its own method, making the main response method easier to understand and maintain.

## Explanation
Expand Down Expand Up @@ -80,4 +115,26 @@ if (isSilent(inputTrimmed))
However, the [Java Coding Conventions][coding-conventions] advise always using curly braces for `if` statements, which helps to avoid errors. Your team may choose to overrule them at its own risk.

Check failure on line 115 in exercises/practice/bob/.approaches/method-based/content.md

View workflow job for this annotation

GitHub Actions / Lint Markdown files

Reference links and images should use a label that is defined

exercises/practice/bob/.approaches/method-based/content.md:115:14 MD052/reference-links-images Reference links and images should use a label that is defined [Missing link or image reference definition: "coding-conventions"] [Context: "[Java Coding Conventions][coding-conventions]"] https://github.com/DavidAnson/markdownlint/blob/v0.35.0/doc/md052.md

[trim]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim()
=======
## Advantages of the Method-Based Approach

Check failure on line 119 in exercises/practice/bob/.approaches/method-based/content.md

View workflow job for this annotation

GitHub Actions / Lint Markdown files

Headings should be surrounded by blank lines

exercises/practice/bob/.approaches/method-based/content.md:119 MD022/blanks-around-headings Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Above] [Context: "## Advantages of the Method-Based Approach"] https://github.com/DavidAnson/markdownlint/blob/v0.35.0/doc/md022.md

- **Readability**: Each method is clearly responsible for a specific condition, making the main response logic easy to follow.
- **Maintainability**: Changes to a condition can be confined to its method, minimizing impacts on the rest of the code.
- **Code Reusability**: Helper methods can be reused or adapted easily if new conditions are added in the future.

## Considerations

- **Efficiency**: While this approach introduces multiple method calls, it enhances readability significantly, which is often more valuable in non-performance-critical applications.
- **Error Prevention**: This approach avoids redundant code, reducing the risk of maintenance errors.

## Shortening Condition Checks

If each `if` statement body is only a single line, braces can be omitted, or the test expression and result could be placed on a single line. However, [Java Coding Conventions][coding-conventions] recommend always using curly braces for error prevention and easier future modifications.

Check failure on line 132 in exercises/practice/bob/.approaches/method-based/content.md

View workflow job for this annotation

GitHub Actions / Lint Markdown files

Reference links and images should use a label that is defined

exercises/practice/bob/.approaches/method-based/content.md:132:152 MD052/reference-links-images Reference links and images should use a label that is defined [Missing link or image reference definition: "coding-conventions"] [Context: "[Java Coding Conventions][coding-conventions]"] https://github.com/DavidAnson/markdownlint/blob/v0.35.0/doc/md052.md

### Alternative: Inline Helper Methods

For smaller projects, consider implementing helper methods inline or as lambdas, though this might reduce readability.

[trim]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim()

Check failure on line 138 in exercises/practice/bob/.approaches/method-based/content.md

View workflow job for this annotation

GitHub Actions / Lint Markdown files

Link and image reference definitions should be needed

exercises/practice/bob/.approaches/method-based/content.md:138:1 MD053/link-image-reference-definitions Link and image reference definitions should be needed [Duplicate link or image reference definition: "trim"] [Context: "[trim]: https://docs.oracle.co..."] https://github.com/DavidAnson/markdownlint/blob/v0.35.0/doc/md053.md
>>>>>>> e67fc434c32715ee323ebc5079ef0497932738f6
[coding-conventions]: https://www.oracle.com/java/technologies/javase/codeconventions-statements.html#449

Check failure on line 140 in exercises/practice/bob/.approaches/method-based/content.md

View workflow job for this annotation

GitHub Actions / Lint Markdown files

Bare URL used

exercises/practice/bob/.approaches/method-based/content.md:140:23 MD034/no-bare-urls Bare URL used [Context: "https://www.oracle.com/java/te..."] https://github.com/DavidAnson/markdownlint/blob/v0.35.0/doc/md034.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"introduction": {
"authors": [
"masiljangajji"
]
},
"approaches": [
{
"uuid": "dee2a79d-3e64-4220-b99f-55667549c12c",
"slug": "fork-join",
"title": "Fork/Join",
"blurb": "Parallel Computation Using Fork/Join",
"authors": [
"masiljangajji"
]
},
{
"uuid": "75e9e93b-4da4-4474-8b6e-3c0cb9b3a9bb",
"slug": "parallel-stream",
"title": "Parallel Stream",
"blurb": "Parallel Computation Using Parallel Stream",
"authors": [
"masiljangajji"
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# `Fork/Join`

```java
import java.util.Map;
import java.util.List;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

class ParallelLetterFrequency {

List<String> texts;
ConcurrentMap<Character, Integer> letterCount;

ParallelLetterFrequency(String[] texts) {
this.texts = List.of(texts);
letterCount = new ConcurrentHashMap<>();
}

Map<Character, Integer> countLetters() {
if (texts.isEmpty()) {
return letterCount;
}

ForkJoinPool forkJoinPool = new ForkJoinPool();
forkJoinPool.invoke(new LetterCountTask(texts, 0, texts.size(), letterCount));
forkJoinPool.shutdown();

return letterCount;
}

private static class LetterCountTask extends RecursiveTask<Void> {
private static final int THRESHOLD = 10;
private final List<String> texts;
private final int start;
private final int end;
private final ConcurrentMap<Character, Integer> letterCount;

LetterCountTask(List<String> texts, int start, int end, ConcurrentMap<Character, Integer> letterCount) {
this.texts = texts;
this.start = start;
this.end = end;
this.letterCount = letterCount;
}

@Override
protected Void compute() {
if (end - start <= THRESHOLD) {
for (int i = start; i < end; i++) {
for (char c : texts.get(i).toLowerCase().toCharArray()) {
if (Character.isAlphabetic(c)) {
letterCount.merge(c, 1, Integer::sum);
}
}
}
} else {
int mid = (start + end) / 2;
LetterCountTask leftTask = new LetterCountTask(texts, start, mid, letterCount);
LetterCountTask rightTask = new LetterCountTask(texts, mid, end, letterCount);
invokeAll(leftTask, rightTask);
}
return null;
}
}
}
```

Using [`ConcurrentHashMap`][ConcurrentHashMap] ensures that frequency counting and updates are safely handled in a parallel environment.

If there are no strings, a validation step prevents unnecessary processing.

A [`ForkJoinPool`][ForkJoinPool] is then created.
The core of [`ForkJoinPool`][ForkJoinPool] is the Fork/Join mechanism, which divides tasks into smaller units and processes them in parallel.

`THRESHOLD` is the criterion for task division.
If the range of texts exceeds the `THRESHOLD`, the task is divided into two subtasks, and [`invokeAll(leftTask, rightTask)`][invokeAll] is called to execute both tasks in parallel.
Each subtask in `LetterCountTask` will continue calling `compute()` (via `invokeAll(leftTask, rightTask)`) to divide itself further until the range is smaller than or equal to the `THRESHOLD`.
For tasks that are within the `THRESHOLD`, letter frequency is calculated.

The [`Character.isAlphabetic`][isAlphabetic] method identifies all characters classified as alphabetic in Unicode, covering characters from various languages like English, Korean, Japanese, Chinese, etc., returning `true`.
Non-alphabetic characters, including numbers, special characters, and spaces, return `false`.

Additionally, since uppercase and lowercase letters are treated as the same character (e.g., `A` and `a`), each character is converted to lowercase.

After updating letter frequencies, the final map is returned.

[ConcurrentHashMap]: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html
[ForkJoinPool]: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html
[isAlphabetic]: https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#isAlphabetic-int-
[invokeAll]: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
for (int i = start; i < end; i++) {
for (char c : texts.get(i).toLowerCase().toCharArray()) {
if (Character.isAlphabetic(c)) {
letterCount.merge(c, 1, Integer::sum);
}
}
}
Loading

0 comments on commit ad27bb2

Please sign in to comment.