Skip to content

Commit

Permalink
Sync tests for practice exercise parallel letter frequency (#2619)
Browse files Browse the repository at this point in the history
* Sync tests for practice exercise parallel letter frequency

* Updating reference resolution to match the new tests

* Adding tests.toml

* Changing tests and example resolution to use parallelism

* Reverting starting implementation to ask for Map<Integer, Integer> again

* Change return type to Map<Character, Integer>

* Update contributors

---------

Co-authored-by: Sander Ploegsma <sanderploegsma@gmail.com>
  • Loading branch information
manumafe98 and sanderploegsma authored Jan 4, 2024
1 parent 8f7b9b3 commit 2c296ac
Show file tree
Hide file tree
Showing 5 changed files with 432 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
"jmrunkle",
"kytrinyx",
"lemoncurry",
"manumafe98",
"mirkoperillo",
"msomji",
"muzimuzhi",
"sanderploegsma",
"sjwarner-bp",
"SleeplessByte",
"sshine",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,28 @@
import java.util.HashMap;
import java.util.Map;


class ParallelLetterFrequency {
private String letters;

private static Map<Integer, Integer> letterFrequencyMap;
private static final String NOT_A_LETTER = "\\P{L}+";


ParallelLetterFrequency(String letters) {
letterFrequencyMap = getMapFromLetters(letters.toLowerCase().replaceAll(NOT_A_LETTER, ""));
ParallelLetterFrequency(String[] texts) {
this.letters = String.join("", texts)
.replaceAll("[^\\p{L}a-zA-Z]", "")
.toLowerCase();
}

Map<Integer, Integer> getMapFromLetters(String letters) {
return letters
Map<Character, Integer> countLetters() {
return this.letters
.chars()
.mapToObj(i -> (char) i)
.parallel()
.collect(Counts::new, Counts::increment, Counts::combine)
.buildMap();
}

private static class Counts {
private Map<Character, Integer> letterCounts = new HashMap<>();

private Map<Integer, Integer> letterCounts = new HashMap<>();

private void increment(int letter) {
private void increment(char letter) {
letterCounts.put(letter, letterCounts.getOrDefault(letter, 0) + 1);
}

Expand All @@ -34,12 +32,8 @@ private void combine(Counts other) {
.forEach(letter -> letterCounts.merge(letter, other.letterCounts.get(letter), Integer::sum));
}

private Map<Integer, Integer> buildMap() {
private Map<Character, Integer> buildMap() {
return Collections.unmodifiableMap(letterCounts);
}
}

Map<Integer, Integer> letterCounts() {
return letterFrequencyMap;
}
}
49 changes: 49 additions & 0 deletions exercises/practice/parallel-letter-frequency/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[c054d642-c1fa-4234-8007-9339f2337886]
description = "no texts"

[818031be-49dc-4675-b2f9-c4047f638a2a]
description = "one text with one letter"

[c0b81d1b-940d-4cea-9f49-8445c69c17ae]
description = "one text with multiple letters"

[708ff1e0-f14a-43fd-adb5-e76750dcf108]
description = "two texts with one letter"

[1b5c28bb-4619-4c9d-8db9-a4bb9c3bdca0]
description = "two texts with multiple letters"

[6366e2b8-b84c-4334-a047-03a00a656d63]
description = "ignore letter casing"

[92ebcbb0-9181-4421-a784-f6f5aa79f75b]
description = "ignore whitespace"

[bc5f4203-00ce-4acc-a5fa-f7b865376fd9]
description = "ignore punctuation"

[68032b8b-346b-4389-a380-e397618f6831]
description = "ignore numbers"

[aa9f97ac-3961-4af1-88e7-6efed1bfddfd]
description = "Unicode letters"

[7b1da046-701b-41fc-813e-dcfb5ee51813]
description = "combination of lower- and uppercase letters, punctuation and white space"

[4727f020-df62-4dcf-99b2-a6e58319cb4f]
description = "large texts"

[adf8e57b-8e54-4483-b6b8-8b32c115884c]
description = "many small texts"
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

class ParallelLetterFrequency {

ParallelLetterFrequency(String letters) {
ParallelLetterFrequency(String[] texts) {
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
}

Map<Integer, Integer> letterCounts() {
Map<Character, Integer> countLetters() {
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
}

}
}
Loading

0 comments on commit 2c296ac

Please sign in to comment.