Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'duplicatesAndUnique' method to analyze haiku text characters #295 #323

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ public String getHaiku()

public IntStream getHaikuAsChars()
{
// TODO: Create an IntStream representing the chars from this.getHaiku()
// Hint: Look at the chars() method on the String class
return null;
return this.getHaiku().chars();
}

public List<Map.Entry<Character, Long>> topLetters()
Expand Down Expand Up @@ -72,20 +70,20 @@ public record CharCountsDuplicatesUnique(

public CharCountsDuplicatesUnique duplicatesAndUnique()
{
// TODO: Find all of the alphabetic letters from this.getHaikuAsChars(), convert them to lowercase
// and count and store them in a Map<Character, Long>.
// Hint: Look at IntStream's filter, map, mapToObj
// Hint: Also Look at Stream.collect, Collectors.groupingBy, Collectors.counting
Map<Character, Long> chars = null;

// TODO: Find all the Characters with duplicates in the map (value > 1)
// Hint: Look at entrySet, stream, filter, collect
// Hint: Also look at Collectors.toMap and using Map.Entry.getKey and Map.Entry.getValue as method references
Map<Character, Long> duplicates = null;

// TODO: Find all the unique Characters in the Map (value < 2)
// Hint: Look at entrySet, stream, filter, map, Map.Entry.getKey, collect, Collectors.toSet
Set<Character> unique = null;
Map<Character, Long> chars = this.getHaikuAsChars()
.filter(Character::isLetter) // Filter alphabetic characters
.mapToObj(ch -> (char) ch) // Convert to characters
.map(Character::toLowerCase) // Convert to lowercase
.collect(Collectors.groupingBy(c -> c, Collectors.counting()));

Map<Character, Long> duplicates = chars.entrySet().stream()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please rename this to duplicateChars

.filter(entry -> 1L < entry.getValue()) // Filter duplicates
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

Set<Character> unique = chars.entrySet().stream()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, please rename to uniqueChars to be specific and readable code

.filter(entry -> 2L > entry.getValue()) // Filter unique
.map(Map.Entry::getKey)
.collect(Collectors.toSet());

// Returns a specialized "triple" type implemented as a Java Record (see CharCountsDuplicatesUnique above)
return new CharCountsDuplicatesUnique(chars, duplicates, unique);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public void distinctLetters()
Assertions.assertEquals("breakingthoupvmwcdflsy", distinctLetters);
}

// @Test // Uncomment once duplicatesAndUnique is implemented for JDK
// @Tag("SOLUTION")
@Test
@Tag("SOLUTION")
public void duplicatesAndUnique()
{
TextProcessorJDK.CharCountsDuplicatesUnique cdu = new TextProcessorJDK().duplicatesAndUnique();
Expand Down