Solving coding challenges is hard but it's great practice for tech interviews.

@cassidoo posts a solving challenge in her newsletter every Monday.

Let's work through this week's Wordle guess checker.

📃 Take a page from my book!

🟩 What's a wordle?

If you're my age or older, it's Mastermind put for words.

There's a five-letter word to be guessed. For each guess, you get a colour code back for each letter

  • 🟩 Correct letter at correct position
  • 🟨 Wrong position
  • ⬛ Wrong letter

Check out Wordle


>>> wordle_guess('reads', 'fudge')

>>> wordle_guess('lodge', 'fudge')

>>> wordle_guess('deeds', 'fudge')

>>> wordle_guess('error', 'tries')

>>> wordle_guess('cooks', 'blond')

>>> wordle_guess('abbbb', 'aaccc')

🦾 Start by looking at the function signature!

For each problem, you want to clarify your inputs and outputs.

When you interview with @Google or @Amazon, this is a good first question.

💬 "What inputs and outputs do we expect, which type, and should I validate the inputs?"

The Wordle expects a guess word as a string and a solution word as a string. Then we output another string.

a proper signature with type hints would look like this:

def wordle_guess(guess: str, solution: str) -> str:

🧠 Before we start coding, think back to your Data Structures & Algorithms course!

What is a good representation of the data?

In Python know all basics (int, float, str, etc) but esp.

  • lists
  • tuple
  • dicts
  • set
  • deque

❤ the Jovian course

❗ This is the most important tweet ❗

I know I have to check every character in the guess string and save the result. Maybe a for loop and add the result to the output.

➡️ We need a mutable ordered data type. A list!

Alternative: Output is a string, so we just merge strings.

For the wordle, we even know the size of the list.

Individual items will be strings we can replace those each turn.

Philosophically, you can assume all guesses are right

["🟩", "🟩", "🟩", "🟩", "🟩"]

or all are wrong

["⬛", "⬛", "⬛", "⬛", "⬛"]

I prefer all wrong.

matches: List[str] = ["⬛"] * len(guess)

🥩 The meat of this function is checking every letter in the guess.

That calls for a for loop. We need to track the position, because we want to change our output variable.

Python has the enumerate function for this.

Super useful!

for i, letter in enumerate(guess):

🤖 Now we need the logic!

The simplest is checking the 🟩. Compare "letter" to the character in position "i" of our solution.

if guess[i] == solution[i]:
    matches[i] = "🟩"

But what about 🟨? If 'deeds' is the guess and 'fudge' is the solution, we need

["🟨", "🟨", "⬛", "⬛", "⬛"]


["🟨", "🟨", "🟨", "🟨", "⬛"]

🎨 This is where you can get creative.

We need to keep track of occurrences of letters in the solution.

I like using the Counter() object from collections.

Count all letters in the solution once. Then simply check if there are still letters left in the solution!

# Get counts of letters in the solution
occurrences = Counter(solution)

# Prepare dictionary for sparse misplaced letters
misplaced_candidate = {}

# 🟩 ⬛ Handle exact matches and misses
for i, letter in enumerate(guess):
    # Exact match
    if letter == solution[i]:
        matches[i] = "🟩"
        occurrences[letter] -= 1
    # Not misses, so candidate for misplaced
    elif occurrences[letter] > 0:
        misplaced_candidate[i] = letter

Then we can go through the letters that we stored in the dictionary misplaced_candidate and check. This dictionary enables us to use sparse indices and not iterate through the entire guess again.

# 🟨 Handle possibly misplaced letters
for i, letter in misplaced_candidate.items():
    if occurrences[letter] > 0:
        matches[i] = "🟨"
        occurrences[letter] -= 1

🎀 Tie it all together

and don't forget to convert the list into a string in the end!

  • 1️⃣ Make list
  • 2️⃣ Count letters in the solution
  • 3️⃣ Iterate through each letter in guess
  • 4️⃣ Apply wordle logic
  • 5️⃣ Make output string

Use comments and a docstring for extra points!

Wordle Guesser solution for Cassidoo Newsletter