Skip to content

Commit 25ad17e

Browse files
committed
Merge branch 'main' of https://github.com/jagdish-15/java
Merging with remote
2 parents 3a0a219 + 85652b5 commit 25ad17e

File tree

12 files changed

+381
-55
lines changed

12 files changed

+381
-55
lines changed

exercises/concept/annalyns-infiltration/.docs/instructions.md

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
11
# Instructions
22

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

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

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

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

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

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

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.
32+
Otherwise, it returns `false`:
2433

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

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

33-
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.
44+
Otherwise, it returns `false`:
3445

3546
```java
3647
boolean knightIsAwake = false;
@@ -40,9 +51,11 @@ AnnalynsInfiltration.canSpy(knightIsAwake, archerIsAwake, prisonerIsAwake);
4051
// => true
4152
```
4253

43-
## 3. Check if the prisoner can be signalled
54+
## 3. Check if the prisoner can be signaled
4455

45-
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.
58+
Otherwise, it returns `false`:
4659

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

5467
## 4. Check if the prisoner can be freed
5568

56-
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.
73+
Otherwise, it returns `false`:
5774

5875
```java
5976
boolean knightIsAwake = false;

exercises/concept/annalyns-infiltration/.docs/introduction.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ Booleans in Java are represented by the `boolean` type, which values can be eith
77
Java supports three boolean operators:
88

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

1313
### Examples
1414

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"introduction": {
3+
"authors": [
4+
"masiljangajji"
5+
]
6+
},
7+
"approaches": [
8+
{
9+
"uuid": "dee2a79d-3e64-4220-b99f-55667549c12c",
10+
"slug": "fork-join",
11+
"title": "Fork/Join",
12+
"blurb": "Parallel Computation Using Fork/Join",
13+
"authors": [
14+
"masiljangajji"
15+
]
16+
},
17+
{
18+
"uuid": "75e9e93b-4da4-4474-8b6e-3c0cb9b3a9bb",
19+
"slug": "parallel-stream",
20+
"title": "Parallel Stream",
21+
"blurb": "Parallel Computation Using Parallel Stream",
22+
"authors": [
23+
"masiljangajji"
24+
]
25+
}
26+
]
27+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# `Fork/Join`
2+
3+
```java
4+
import java.util.Map;
5+
import java.util.List;
6+
import java.util.concurrent.ConcurrentMap;
7+
import java.util.concurrent.ConcurrentHashMap;
8+
import java.util.concurrent.ForkJoinPool;
9+
import java.util.concurrent.RecursiveTask;
10+
11+
class ParallelLetterFrequency {
12+
13+
List<String> texts;
14+
ConcurrentMap<Character, Integer> letterCount;
15+
16+
ParallelLetterFrequency(String[] texts) {
17+
this.texts = List.of(texts);
18+
letterCount = new ConcurrentHashMap<>();
19+
}
20+
21+
Map<Character, Integer> countLetters() {
22+
if (texts.isEmpty()) {
23+
return letterCount;
24+
}
25+
26+
ForkJoinPool forkJoinPool = new ForkJoinPool();
27+
forkJoinPool.invoke(new LetterCountTask(texts, 0, texts.size(), letterCount));
28+
forkJoinPool.shutdown();
29+
30+
return letterCount;
31+
}
32+
33+
private static class LetterCountTask extends RecursiveTask<Void> {
34+
private static final int THRESHOLD = 10;
35+
private final List<String> texts;
36+
private final int start;
37+
private final int end;
38+
private final ConcurrentMap<Character, Integer> letterCount;
39+
40+
LetterCountTask(List<String> texts, int start, int end, ConcurrentMap<Character, Integer> letterCount) {
41+
this.texts = texts;
42+
this.start = start;
43+
this.end = end;
44+
this.letterCount = letterCount;
45+
}
46+
47+
@Override
48+
protected Void compute() {
49+
if (end - start <= THRESHOLD) {
50+
for (int i = start; i < end; i++) {
51+
for (char c : texts.get(i).toLowerCase().toCharArray()) {
52+
if (Character.isAlphabetic(c)) {
53+
letterCount.merge(c, 1, Integer::sum);
54+
}
55+
}
56+
}
57+
} else {
58+
int mid = (start + end) / 2;
59+
LetterCountTask leftTask = new LetterCountTask(texts, start, mid, letterCount);
60+
LetterCountTask rightTask = new LetterCountTask(texts, mid, end, letterCount);
61+
invokeAll(leftTask, rightTask);
62+
}
63+
return null;
64+
}
65+
}
66+
}
67+
```
68+
69+
Using [`ConcurrentHashMap`][ConcurrentHashMap] ensures that frequency counting and updates are safely handled in a parallel environment.
70+
71+
If there are no strings, a validation step prevents unnecessary processing.
72+
73+
A [`ForkJoinPool`][ForkJoinPool] is then created.
74+
The core of [`ForkJoinPool`][ForkJoinPool] is the Fork/Join mechanism, which divides tasks into smaller units and processes them in parallel.
75+
76+
`THRESHOLD` is the criterion for task division.
77+
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.
78+
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`.
79+
For tasks that are within the `THRESHOLD`, letter frequency is calculated.
80+
81+
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`.
82+
Non-alphabetic characters, including numbers, special characters, and spaces, return `false`.
83+
84+
Additionally, since uppercase and lowercase letters are treated as the same character (e.g., `A` and `a`), each character is converted to lowercase.
85+
86+
After updating letter frequencies, the final map is returned.
87+
88+
[ConcurrentHashMap]: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html
89+
[ForkJoinPool]: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html
90+
[isAlphabetic]: https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#isAlphabetic-int-
91+
[invokeAll]: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
for (int i = start; i < end; i++) {
2+
for (char c : texts.get(i).toLowerCase().toCharArray()) {
3+
if (Character.isAlphabetic(c)) {
4+
letterCount.merge(c, 1, Integer::sum);
5+
}
6+
}
7+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Introduction
2+
3+
There are multiple ways to solve the Parallel Letter Frequency problem.
4+
One approach is to use [`Stream.parallelStream`][stream], and another involves using [`ForkJoinPool`][ForkJoinPool].
5+
6+
## General guidance
7+
8+
To count occurrences of items, a map data structure is often used, though arrays and lists can work as well.
9+
A [`map`][map], being a key-value pair structure, is suitable for recording frequency by incrementing the value for each key.
10+
If the data being counted has a limited range (e.g., `Characters` or `Integers`), an `int[] array` or [`List<Integer>`][list] can be used to record frequencies.
11+
12+
Parallel processing typically takes place in a multi-[`thread`][thread] environment.
13+
The Java 8 [`stream`][stream] API provides methods that make parallel processing easier, including the [`parallelStream()`][stream] method.
14+
With [`parallelStream()`][stream], developers can use the [`ForkJoinPool`][ForkJoinPool] model for workload division and parallel execution, without the need to manually manage threads or create custom thread pools.
15+
16+
The [`ForkJoinPool`][ForkJoinPool] class, optimized for dividing and managing tasks, makes parallel processing efficient.
17+
However, [`parallelStream()`][stream] uses the common [`ForkJoinPool`][ForkJoinPool] by default, meaning multiple [`parallelStream`][stream] instances share the same thread pool unless configured otherwise.
18+
19+
As a result, parallel streams may interfere with each other when sharing this thread pool, potentially affecting performance.
20+
Although this doesn’t directly impact solving the Parallel Letter Frequency problem, it may introduce issues when thread pool sharing causes conflicts in other applications.
21+
Therefore, a custom [`ForkJoinPool`][ForkJoinPool] approach is also provided below.
22+
23+
## Approach: `parallelStream`
24+
25+
```java
26+
import java.util.Map;
27+
import java.util.List;
28+
import java.util.concurrent.ConcurrentMap;
29+
import java.util.concurrent.ConcurrentHashMap;
30+
31+
class ParallelLetterFrequency {
32+
33+
List<String> texts;
34+
ConcurrentMap<Character, Integer> letterCount;
35+
36+
ParallelLetterFrequency(String[] texts) {
37+
this.texts = List.of(texts);
38+
letterCount = new ConcurrentHashMap<>();
39+
}
40+
41+
Map<Character, Integer> countLetters() {
42+
if (!letterCount.isEmpty() || texts.isEmpty()) {
43+
return letterCount;
44+
}
45+
texts.parallelStream().forEach(text -> {
46+
for (char c: text.toLowerCase().toCharArray()) {
47+
if (Character.isAlphabetic(c)) {
48+
letterCount.merge(c, 1, Integer::sum);
49+
}
50+
}
51+
});
52+
return letterCount;
53+
}
54+
55+
}
56+
```
57+
58+
For more information, check the [`parallelStream` approach][approach-parallel-stream].
59+
60+
## Approach: `Fork/Join`
61+
62+
```java
63+
import java.util.Map;
64+
import java.util.List;
65+
import java.util.concurrent.ConcurrentMap;
66+
import java.util.concurrent.ConcurrentHashMap;
67+
import java.util.concurrent.ForkJoinPool;
68+
import java.util.concurrent.RecursiveTask;
69+
70+
class ParallelLetterFrequency {
71+
72+
List<String> texts;
73+
ConcurrentMap<Character, Integer> letterCount;
74+
75+
ParallelLetterFrequency(String[] texts) {
76+
this.texts = List.of(texts);
77+
letterCount = new ConcurrentHashMap<>();
78+
}
79+
80+
Map<Character, Integer> countLetters() {
81+
if (!letterCount.isEmpty() || texts.isEmpty()) {
82+
return letterCount;
83+
}
84+
85+
ForkJoinPool forkJoinPool = new ForkJoinPool();
86+
forkJoinPool.invoke(new LetterCountTask(texts, 0, texts.size(), letterCount));
87+
forkJoinPool.shutdown();
88+
89+
return letterCount;
90+
}
91+
92+
private static class LetterCountTask extends RecursiveTask<Void> {
93+
private static final int THRESHOLD = 10;
94+
private final List<String> texts;
95+
private final int start;
96+
private final int end;
97+
private final ConcurrentMap<Character, Integer> letterCount;
98+
99+
LetterCountTask(List<String> texts, int start, int end, ConcurrentMap<Character, Integer> letterCount) {
100+
this.texts = texts;
101+
this.start = start;
102+
this.end = end;
103+
this.letterCount = letterCount;
104+
}
105+
106+
@Override
107+
protected Void compute() {
108+
if (end - start <= THRESHOLD) {
109+
for (int i = start; i < end; i++) {
110+
for (char c : texts.get(i).toLowerCase().toCharArray()) {
111+
if (Character.isAlphabetic(c)) {
112+
letterCount.merge(c, 1, Integer::sum);
113+
}
114+
}
115+
}
116+
} else {
117+
int mid = (start + end) / 2;
118+
LetterCountTask leftTask = new LetterCountTask(texts, start, mid, letterCount);
119+
LetterCountTask rightTask = new LetterCountTask(texts, mid, end, letterCount);
120+
invokeAll(leftTask, rightTask);
121+
}
122+
return null;
123+
}
124+
}
125+
}
126+
127+
```
128+
129+
For more information, check the [`fork/join` approach][approach-fork-join].
130+
131+
## Which approach to use?
132+
133+
When tasks are simple or do not require a dedicated thread pool (such as in this case), the [`parallelStream`][stream] approach is recommended.
134+
However, if the work is complex or there is a need to isolate thread pools from other concurrent tasks, the [`ForkJoinPool`][ForkJoinPool] approach is preferable.
135+
136+
[thread]: https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html
137+
[stream]: https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html
138+
[ForkJoinPool]: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html
139+
[map]: https://docs.oracle.com/javase/8/docs/api/?java/util/Map.html
140+
[list]: https://docs.oracle.com/javase/8/docs/api/?java/util/List.html
141+
[approach-parallel-stream]: https://exercism.org/tracks/java/exercises/parallel-letter-frequency/approaches/parallel-stream
142+
[approach-fork-join]: https://exercism.org/tracks/java/exercises/parallel-letter-frequency/approaches/fork-join

0 commit comments

Comments
 (0)