Oracle Developers
Published in

Oracle Developers

A Wordle JLDD Kata Challenge

Learn how to use Eclipse Collections to solve a Wordle word guessing kata

Katas are coding puzzles to keep your skills sharp

I love creating and solving code katas. Code Katas are programming puzzles that can help keep your coding skills sharp. I wrote an article titled “Learn to Kata and Kata to Learn” for the “97 Things Every Java Programmer Should Know” book, and the link to the article is available for free here on Medium on the O’Reilly 97 Things publication.

The Wonderful World of Wordle

Wordle is a very popular online puzzle game where you have six chance to guess a five letter word. Each guess results in learning which characters match the characters in the word. You are given hints via colors which let you know if you got a letter correct in the right position, if the letter is in the word but in the wrong position, or if the letter does not match any letter in the word.

You can find out more about the Wordle craze in this article.

JLDD = Jet Lag Driven Development

In the “before times”, when tech conferences and global travel were a more common occurrence, a few Java Champions (José Paumard, Nikhil Nanivadekar, and myself) would share coding challenges on Twitter while we were at the conferences (usually JavaOne/Oracle CodeOne). We all had various levels of jet lag during the conferences, so we decided to use the JLDD hashtag when we posted the coding challenges. Most often the coding challenges were geared towards solving problems with Java Collections or Streams. I would usually post solutions using Eclipse Collections.

Throughout the pandemic we have still occasionally shared JLDD challenges with each other on Twitter, even though the jet lag has cleared up a long time ago. A few weeks ago I shared a Haiku Kata using Java Text Blocks and Eclipse Collections.

José Paumard then went above and beyond and live coded the JLDD challenge in a 25 minute JEP Cafe #9 video. He does an amazing job explaining both the Eclipse Collections and Java 17 solutions. Well done!

The Wordle Kata

José Paumard sent me a JLDD challenge this week in the form of a test that I needed to write the code to pass. I love this kind of kata, which follows classic TDD style using a test first approach. Here’s the test code for the kata using plain JUnit 5 assertions.

Several hidden words and corresponding guesses that result in some output

The rules based on this test are fairly straightforward.

  1. If a letter in the guess String doesn’t match a letter in the hidden word, then replace the character in the output with “.”
  2. If a letter in the guess String matches a letter in the hidden word and the letter is in the same position, then replace the character with an uppercase letter.
  3. If a letter in the guess String matches a letter in the hidden word but the letter is in a different position, then replace the character with a lowercase letter.
  4. If a letter matches, but appears more times in the guess String than in the hidden word, then replace the additional characters in the output with a “.”.

My first solution using Eclipse Collections

The solution I came up with looked as follows, and passed all of the tests.

Code for Wordle guessing using Eclipse Collections

The code takes the guess, compares each letter to the hidden word and if there is a direct match, prints an uppercase letter, otherwise it prints a lowercase letter if there is an indirect match or a “.” if there is no match or the letter is an extra matching character. The method collectWithIndex on the CharAdapter type transforms the guessChars String one character at a time. I thought at first that no char values would be boxed as Character objects, but it turns out I was wrong. The method collectWithIndex takes a CharIntToObjectFunction, which means the char values for each output character will be boxed as Character objects. This also means we do not have a purely primitive version of collectWithIndex in Eclipse Collections, like we do with collectChar. I think this is probably acceptable in most cases, and should not be too costly for this particular use case. I don’t think adding a purely primitive version named collectCharWithIndex would carry its weight.

There is however a bigger problem than boxing char values as Character objects before creating the output String. I discovered there was a missing test case, and an additional rule that we should add to the kata.

Rule: Favor direct matches over indirect matches

I added the following test case that causes my first solution to fail.

A word that has a direct match at one letter, but indirect matches occur before the direct match

Let me zoom in so you can see the test case more clearly.

The output for a word “bbabb” with a guess of “aaaaa” should be “..A..”

In this case, the letter “a” will have a direct match in the third position, but the indirect matches in the guess should be ignored in favor of the direct match in the third position.

My updated solution

For folks who know me, you’ll know I am somewhat obsessed with providing good symmetry in the Eclipse Collection API. With this particular use case, it would be ideal if there was an equivalent of selectWithIndex and rejectWithIndex available on the primitive collection types in Eclipse Collections. This particular use case might get me to acquiesce here and add the missing methods.

There is however an alternative method that I can use to implement the equivalent of these two methods. That method is injectIntoWithIndex.

Here is my updated solution using injectIntoWithIndex to create a CharBag with the remaining characters that do not have direct matches.

Wordle guessing solution that handles all rules

If you want to understand how the injectIntoWithIndex method works, you can read the following blog on injectInto. The method injectInto can be used to implement most iteration patterns, which is also the case with injectIntoWithIndex. They are both mysterious and powerful methods.

Update: An alternative solution using zipChar

Sometimes when you work on the same code base for 18 years, you forget a thing or two. Thankfully, a friend will occasionally remind you of things you may have forgotten. That happened this week when Vladimir Zakharov shared a different solution to the Wordle JLDD Kata on Twitter using a method named zipChar.

Vlad’s solution is quite cool, and is definitely not an approach I would have thought of. I had forgotten that there was a primitive version of zip available in Eclipse Collections. Each primitive OrderedIterable type in Eclipse Collections supports zip with the same type. So a CharAdapter has a zipChar method. I soon recalled there were was a blog I had written over four years ago on primitive zip.

Once I saw Vlad’s solution, I realized I could write an alternative to my own solution using zipChar to replace injectIntoIndex and collectWithIndex.

Wordle Kata using zipChar with reject and collectChar

The two solutions that use zipChar are significantly different in the algorithm they use to calculate the final guess result String.

The Source for my Final Eclipse Collections Solution

I’ve created a gist for my final Wordle JLDD Kata challenge solution using Eclipse Collections. I’m looking forward to seeing the pure Java 17 solution from José Paumard. I always learn new and interesting things about Java from José that I didn’t know before.

Final Thoughts

I hope you enjoyed this blog on my solutions to a JLDD Kata challenge using Eclipse Collections. I’d certainly welcome other solutions to these challenges so we can all learn new and different solutions to programming problems. These solutions could be in Java using your favorite libraries, or even in another programming language.

Thank you for taking the time to read!

I am a Project Lead and Committer for the Eclipse Collections OSS project at the Eclipse Foundation. Eclipse Collections is open for contributions. If you like the library, you can let us know by starring it on GitHub.

--

--

--

Aggregation of articles from Oracle engineers, Groundbreaker Ambassadors, Oracle ACEs, and Java Champions on all things Oracle technology. The views expressed are those of the authors and not necessarily of Oracle.

Recommended from Medium

Extracting data from Pushshift archives

How to Build a RegEx Engine in Python

Hacking Android Remotely (WAN) using Kali Linux

Coordinating ECS Tasks in Python

Solving The Transposition Challenge Looping Through The String Once

Multi-Cloud Enterprise Network Architectures — natively bridging AWS and Azure at scale (Part 1)

Getting started with Embedded Systems — 2: Microcontrollers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Donald Raab

Donald Raab

Java Champion. Creator of the Eclipse Collections OSS Java library (http://www.eclipse.org/collections/). Inspired by Smalltalk. Opinions are my own.

More from Medium

Speed up My mvn with mvnd

Just Don’t Return null!

How Servlet 4.0 and 5.0 are even more convenient than the perfect 3.1

GraalVM — Native Images & Microframeworks.