diff --git a/exercises/concept/annalyns-infiltration/.docs/instructions.md b/exercises/concept/annalyns-infiltration/.docs/instructions.md index 4ae00ea14..f8e35fd96 100644 --- a/exercises/concept/annalyns-infiltration/.docs/instructions.md +++ b/exercises/concept/annalyns-infiltration/.docs/instructions.md @@ -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; @@ -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; @@ -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; @@ -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; diff --git a/exercises/concept/annalyns-infiltration/.docs/introduction.md b/exercises/concept/annalyns-infiltration/.docs/introduction.md index 46597333e..6ff36c4db 100644 --- a/exercises/concept/annalyns-infiltration/.docs/introduction.md +++ b/exercises/concept/annalyns-infiltration/.docs/introduction.md @@ -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 diff --git a/exercises/practice/bob/.approaches/method-based/content.md b/exercises/practice/bob/.approaches/method-based/content.md index 816b60643..77b477222 100644 --- a/exercises/practice/bob/.approaches/method-based/content.md +++ b/exercises/practice/bob/.approaches/method-based/content.md @@ -1,5 +1,39 @@ # Method-Based Approach +<<<<<<< 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. + +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 class Bob { String hey(String input) { @@ -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 @@ -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. [trim]: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim() +======= +## Advantages of the Method-Based Approach + +- **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. + +### 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() +>>>>>>> e67fc434c32715ee323ebc5079ef0497932738f6 [coding-conventions]: https://www.oracle.com/java/technologies/javase/codeconventions-statements.html#449 diff --git a/exercises/practice/parallel-letter-frequency/.approaches/config.json b/exercises/practice/parallel-letter-frequency/.approaches/config.json new file mode 100644 index 000000000..dad02090c --- /dev/null +++ b/exercises/practice/parallel-letter-frequency/.approaches/config.json @@ -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" + ] + } + ] +} diff --git a/exercises/practice/parallel-letter-frequency/.approaches/fork-join/content.md b/exercises/practice/parallel-letter-frequency/.approaches/fork-join/content.md new file mode 100644 index 000000000..de2cd7307 --- /dev/null +++ b/exercises/practice/parallel-letter-frequency/.approaches/fork-join/content.md @@ -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 texts; + ConcurrentMap letterCount; + + ParallelLetterFrequency(String[] texts) { + this.texts = List.of(texts); + letterCount = new ConcurrentHashMap<>(); + } + + Map 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 { + private static final int THRESHOLD = 10; + private final List texts; + private final int start; + private final int end; + private final ConcurrentMap letterCount; + + LetterCountTask(List texts, int start, int end, ConcurrentMap 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 diff --git a/exercises/practice/parallel-letter-frequency/.approaches/fork-join/snippet.txt b/exercises/practice/parallel-letter-frequency/.approaches/fork-join/snippet.txt new file mode 100644 index 000000000..7e782eb30 --- /dev/null +++ b/exercises/practice/parallel-letter-frequency/.approaches/fork-join/snippet.txt @@ -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); + } + } +} \ No newline at end of file diff --git a/exercises/practice/parallel-letter-frequency/.approaches/introduction.md b/exercises/practice/parallel-letter-frequency/.approaches/introduction.md new file mode 100644 index 000000000..8ca8fbf92 --- /dev/null +++ b/exercises/practice/parallel-letter-frequency/.approaches/introduction.md @@ -0,0 +1,142 @@ +# Introduction + +There are multiple ways to solve the Parallel Letter Frequency problem. +One approach is to use [`Stream.parallelStream`][stream], and another involves using [`ForkJoinPool`][ForkJoinPool]. + +## General guidance + +To count occurrences of items, a map data structure is often used, though arrays and lists can work as well. +A [`map`][map], being a key-value pair structure, is suitable for recording frequency by incrementing the value for each key. +If the data being counted has a limited range (e.g., `Characters` or `Integers`), an `int[] array` or [`List`][list] can be used to record frequencies. + +Parallel processing typically takes place in a multi-[`thread`][thread] environment. +The Java 8 [`stream`][stream] API provides methods that make parallel processing easier, including the [`parallelStream()`][stream] method. +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. + +The [`ForkJoinPool`][ForkJoinPool] class, optimized for dividing and managing tasks, makes parallel processing efficient. +However, [`parallelStream()`][stream] uses the common [`ForkJoinPool`][ForkJoinPool] by default, meaning multiple [`parallelStream`][stream] instances share the same thread pool unless configured otherwise. + +As a result, parallel streams may interfere with each other when sharing this thread pool, potentially affecting performance. +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. +Therefore, a custom [`ForkJoinPool`][ForkJoinPool] approach is also provided below. + +## Approach: `parallelStream` + +```java +import java.util.Map; +import java.util.List; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ConcurrentHashMap; + +class ParallelLetterFrequency { + + List texts; + ConcurrentMap letterCount; + + ParallelLetterFrequency(String[] texts) { + this.texts = List.of(texts); + letterCount = new ConcurrentHashMap<>(); + } + + Map countLetters() { + if (!letterCount.isEmpty() || texts.isEmpty()) { + return letterCount; + } + texts.parallelStream().forEach(text -> { + for (char c: text.toLowerCase().toCharArray()) { + if (Character.isAlphabetic(c)) { + letterCount.merge(c, 1, Integer::sum); + } + } + }); + return letterCount; + } + +} +``` + +For more information, check the [`parallelStream` approach][approach-parallel-stream]. + +## Approach: `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 texts; + ConcurrentMap letterCount; + + ParallelLetterFrequency(String[] texts) { + this.texts = List.of(texts); + letterCount = new ConcurrentHashMap<>(); + } + + Map countLetters() { + if (!letterCount.isEmpty() || 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 { + private static final int THRESHOLD = 10; + private final List texts; + private final int start; + private final int end; + private final ConcurrentMap letterCount; + + LetterCountTask(List texts, int start, int end, ConcurrentMap 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; + } + } +} + +``` + +For more information, check the [`fork/join` approach][approach-fork-join]. + +## Which approach to use? + +When tasks are simple or do not require a dedicated thread pool (such as in this case), the [`parallelStream`][stream] approach is recommended. +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. + +[thread]: https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html +[stream]: https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html +[ForkJoinPool]: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html +[map]: https://docs.oracle.com/javase/8/docs/api/?java/util/Map.html +[list]: https://docs.oracle.com/javase/8/docs/api/?java/util/List.html +[approach-parallel-stream]: https://exercism.org/tracks/java/exercises/parallel-letter-frequency/approaches/parallel-stream +[approach-fork-join]: https://exercism.org/tracks/java/exercises/parallel-letter-frequency/approaches/fork-join diff --git a/exercises/practice/parallel-letter-frequency/.approaches/parallel-stream/content.md b/exercises/practice/parallel-letter-frequency/.approaches/parallel-stream/content.md new file mode 100644 index 000000000..6b532b767 --- /dev/null +++ b/exercises/practice/parallel-letter-frequency/.approaches/parallel-stream/content.md @@ -0,0 +1,49 @@ +# `parallelStream` + +```java +import java.util.Map; +import java.util.List; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ConcurrentHashMap; + +class ParallelLetterFrequency { + + List texts; + ConcurrentMap letterCount; + + ParallelLetterFrequency(String[] texts) { + this.texts = List.of(texts); + letterCount = new ConcurrentHashMap<>(); + } + + Map countLetters() { + if (texts.isEmpty()) { + return letterCount; + } + texts.parallelStream().forEach(text -> { + for (char c: text.toLowerCase().toCharArray()) { + if (Character.isAlphabetic(c)) { + letterCount.merge(c, 1, Integer::sum); + } + } + }); + return letterCount; + } + +} +``` + +Using [`ConcurrentHashMap`][ConcurrentHashMap] ensures that frequency counting and updates are safely handled in a parallel environment. + +If there are no strings to process, a validation step avoids unnecessary computation. + +To calculate letter frequency, a parallel stream is used. +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`. + +Since we treat uppercase and lowercase letters as the same character (e.g., `A` and `a`), characters are 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 +[isAlphabetic]: https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html#isAlphabetic-int- diff --git a/exercises/practice/parallel-letter-frequency/.approaches/parallel-stream/snippet.txt b/exercises/practice/parallel-letter-frequency/.approaches/parallel-stream/snippet.txt new file mode 100644 index 000000000..9cbb9cffa --- /dev/null +++ b/exercises/practice/parallel-letter-frequency/.approaches/parallel-stream/snippet.txt @@ -0,0 +1,7 @@ +texts.parallelStream().forEach(text -> { + for (char c: text.toLowerCase().toCharArray()) { + if (Character.isAlphabetic(c)) { + letterCount.merge(c, 1, Integer::sum); + } + } +}); \ No newline at end of file diff --git a/exercises/practice/scrabble-score/.approaches/if-statements/content.md b/exercises/practice/scrabble-score/.approaches/if-statements/content.md index ae274fe8c..14d7c2ad8 100644 --- a/exercises/practice/scrabble-score/.approaches/if-statements/content.md +++ b/exercises/practice/scrabble-score/.approaches/if-statements/content.md @@ -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 diff --git a/exercises/practice/scrabble-score/.approaches/introduction.md b/exercises/practice/scrabble-score/.approaches/introduction.md index f0254b392..dd3a630c1 100644 --- a/exercises/practice/scrabble-score/.approaches/introduction.md +++ b/exercises/practice/scrabble-score/.approaches/introduction.md @@ -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 @@ -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 diff --git a/exercises/practice/scrabble-score/.approaches/map-reduce/content.md b/exercises/practice/scrabble-score/.approaches/map-reduce/content.md index af19d9eea..108c67df9 100644 --- a/exercises/practice/scrabble-score/.approaches/map-reduce/content.md +++ b/exercises/practice/scrabble-score/.approaches/map-reduce/content.md @@ -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 diff --git a/exercises/practice/scrabble-score/.approaches/switch-statement/content.md b/exercises/practice/scrabble-score/.approaches/switch-statement/content.md index 30e084a36..80205cfe5 100644 --- a/exercises/practice/scrabble-score/.approaches/switch-statement/content.md +++ b/exercises/practice/scrabble-score/.approaches/switch-statement/content.md @@ -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 diff --git a/exercises/practice/yacht/.meta/config.json b/exercises/practice/yacht/.meta/config.json index 122b450c0..971e3587d 100644 --- a/exercises/practice/yacht/.meta/config.json +++ b/exercises/practice/yacht/.meta/config.json @@ -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)" }