Skip to content

Commit

Permalink
Add new Haiku Kata solutions module.
Browse files Browse the repository at this point in the history
Signed-off-by: Donald Raab <Donald.Raab@bnymellon.com>
  • Loading branch information
donraab committed Jul 19, 2023
1 parent e98be90 commit 819b9b6
Show file tree
Hide file tree
Showing 7 changed files with 490 additions and 0 deletions.
48 changes: 48 additions & 0 deletions haiku-kata-solutions/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2023 Goldman Sachs.
~ All rights reserved. This program and the accompanying materials
~ are made available under the terms of the Eclipse Public License v1.0
~ and Eclipse Distribution License v. 1.0 which accompany this distribution.
~ The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
~ and the Eclipse Distribution License is available at
~ http://www.eclipse.org/org/documents/edl-v10.php.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>eclipse-collections-kata-parent-17</artifactId>
<groupId>org.eclipse.collections.kata</groupId>
<version>7.1.0-SNAPSHOT</version>
<relativePath>../pom-jdk17.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>haiku-kata-solutions</artifactId>

<dependencies>
<dependency>
<groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections-api</artifactId>
</dependency>

<dependency>
<groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections</artifactId>
</dependency>

<dependency>
<groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections-testutils</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2023 The Bank of New York Mellon.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/

package org.eclipse.collections;

public class HaikuCollection
{
private final String text = """
Breaking Through Pavement Wakin' with Bacon Homeward Found
---------------- -------- ----------------- --------------
The wall disappears Beautiful pavement! Wakin' with Bacon House is where I am
As soon as you break through the Imperfect path before me On a Saturday morning Home is where I want to be
Intimidation Thank you for the ride Life’s little pleasures Both may be the same
Winter Slip and Slide Simple Nothings With Deepest Regrets
--------------------- --------------- --------------------
Run up the ladder A simple flower With deepest regrets
Swoosh down the slide in the snow Petals shine vibrant and pure That which you have yet to write
Winter slip and slide Stares into the void At death, won't be wrote
Caffeinated Coding Rituals Finding Solace Curious Cat Eleven
-------------------------- -------------- ----------- ------
I arrange my desk, Floating marshmallows I see something move This is how many
refactor some ugly code, Cocoa brewed hot underneath What it is, I am not sure Haiku I write before I
and drink my coffee. Comfort in a cup Should I pounce or not? Write a new tech blog.
""";

public String getText()
{
return this.text;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (c) 2023 The Bank of New York Mellon.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/

package org.eclipse.collections;

import org.eclipse.collections.api.bag.primitive.CharBag;
import org.eclipse.collections.api.bag.primitive.MutableCharBag;
import org.eclipse.collections.api.list.ListIterable;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.set.primitive.CharSet;
import org.eclipse.collections.api.tuple.Triple;
import org.eclipse.collections.api.tuple.primitive.CharCharPair;
import org.eclipse.collections.api.tuple.primitive.CharIntPair;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.factory.Strings;
import org.eclipse.collections.impl.tuple.Tuples;
import org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples;
import org.eclipse.collections.impl.utility.StringIterate;
import org.junit.jupiter.api.Assertions;

public class TextProcessorEC
{
public String getHaiku()
{
return new HaikuCollection().getText();
}

public ListIterable<CharIntPair> topLetters()
{
CharBag chars = Strings.asChars(this.getHaiku())
.select(Character::isAlphabetic)
.collectChar(Character::toLowerCase)
.toBag();

return chars.topOccurrences(3);
}

public String distinctLetters()
{
return Strings.asChars(this.getHaiku())
.select(Character::isAlphabetic)
.collectChar(Character::toLowerCase)
.distinct()
.toString();
}

public Triple<CharBag, CharBag, CharSet> duplicatesAndUnique()
{
MutableCharBag chars = Strings.asChars(this.getHaiku())
.select(Character::isAlphabetic)
.collectChar(Character::toLowerCase)
.toBag();

CharBag duplicates = chars.selectDuplicates();
CharSet unique = chars.selectUnique();

return Tuples.triple(chars, duplicates, unique);
}

public CharCharPair topVowelAndConsonant()
{
MutableList<CharIntPair> charIntPairs = Strings.asChars(this.getHaiku())
.asLazy()
.select(Character::isAlphabetic)
.collectChar(Character::toLowerCase)
.toBag()
.topOccurrences(26);

char topVowel = charIntPairs.detect(pair -> this.isVowel(pair.getOne())).getOne();
char topConsonant = charIntPairs.detect(pair -> !this.isVowel(pair.getOne())).getOne();

return PrimitiveTuples.pair(topVowel, topConsonant);
}

public boolean isVowel(char character)
{
return switch (character)
{
case 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' -> true;
default -> false;
};
}

public MutableSet<String> findWordleWords()
{
MutableList<String> words = Lists.mutable.empty();
StringIterate.forEachToken(this.getHaiku(), " ,.-!?\t\n\r\f", words::add);
MutableSet<String> wordleWords = words.asLazy()
.reject(word -> word.contains("'"))
.select(word -> word.length() == 5)
.collect(String::toLowerCase)
.toSet();
return wordleWords;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright (c) 2023 The Bank of New York Mellon.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/

package org.eclipse.collections;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.collections.impl.block.factory.Functions;
import org.junit.jupiter.api.Assertions;

public class TextProcessorJDK
{
public String getHaiku()
{
return new HaikuCollection().getText();
}

public List<Map.Entry<Character, Long>> topLetters()
{
Map<Character, Long> chars = this.getHaiku().chars()
.filter(Character::isAlphabetic)
.map(Character::toLowerCase)
.mapToObj(i -> (char) i)
.collect(Collectors.groupingBy(Functions.identity(), Collectors.counting()));

return chars.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.limit(3)
.toList();
}

public String distinctLetters()
{
return this.getHaiku().chars()
.filter(Character::isAlphabetic)
.map(Character::toLowerCase)
.distinct()
.mapToObj(i -> (char) i)
.collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
.toString();
}

public record CharCountsDuplicatesUnique(
Map<Character, Long> chars,
Map<Character, Long> duplicates,
Set<Character> unique
) {}

;

public CharCountsDuplicatesUnique duplicatesAndUnique()
{
Map<Character, Long> chars = this.getHaiku().chars()
.filter(Character::isAlphabetic)
.map(Character::toLowerCase)
.mapToObj(i -> (char) i)
.collect(Collectors.groupingBy(Functions.identity(), Collectors.counting()));

Map<Character, Long> duplicates = chars
.entrySet()
.stream()
.filter(entry -> entry.getValue() > 1)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

Set<Character> unique = chars
.entrySet()
.stream()
.filter(entry -> entry.getValue() < 2)
.map(Map.Entry::getKey)
.collect(Collectors.toSet());

return new CharCountsDuplicatesUnique(chars, duplicates, unique);
}

public record TopVowelAndConsonant(char vowel, char consonant) {}

;

public TopVowelAndConsonant topVowelAndConsonant()
{
List<Map.Entry<Character, Long>> entries = this.getHaiku().chars()
.filter(Character::isAlphabetic)
.map(Character::toLowerCase)
.mapToObj(i -> (char) i)
.collect(Collectors.groupingBy(Functions.identity(), Collectors.counting()))
.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.toList();

char topVowel = entries.stream()
.filter(e -> this.isVowel(e.getKey()))
.findFirst()
.orElseThrow()
.getKey();

char topConsonant = entries.stream()
.filter(e -> !this.isVowel(e.getKey()))
.findFirst()
.orElseThrow()
.getKey();

return new TopVowelAndConsonant(topVowel, topConsonant);
}

public boolean isVowel(char character)
{
return switch (character)
{
case 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' -> true;
default -> false;
};
}

public Set<String> findWordleWords()
{
List<String> words = new Scanner(this.getHaiku()).useDelimiter("[\\s,.!?-]+").tokens().toList();
Set<String> wordleWords = words.stream()
.filter(word -> !word.contains("'"))
.filter(word -> word.length() == 5)
.map(String::toLowerCase)
.collect(Collectors.toSet());
return wordleWords;
}
}
Loading

0 comments on commit 819b9b6

Please sign in to comment.