How to resolve the algorithm Wordle comparison step by step in the Go programming language
How to resolve the algorithm Wordle comparison step by step in the Go programming language
Table of Contents
Problem Statement
While similar to both Bulls and cows and Mastermind, Wordle is a notable variation, having experienced a viral surge in popularity, and reverse engineering the game or creating variants has become a popular programming exercise. However, a sampling of the "code a Wordle clone" videos on YouTube shows that seven of the eight reviewed had a serious flaw in the way that they assigned colours to the letters of a guessed word. This aspect of the game is described here: en.wikipedia.org/wiki/Wordle#Gameplay Create a function or procedure that takes two strings; the answer string, and the guess string, and returns a string, list, dynamic array or other ordered sequence indicating how each letter should be marked as per the description above. (e.g. "green", "yellow", or "grey", or, equivalently, the integers 2, 1, or 0 or suchlike.) You can assume that both the answer string and the guess string are the same length, and contain only printable characters/code points in the ASCII/UniCode Range ! to ~ (hex 20 to 7F) and that case is significant. (The original game only uses strings of 5 characters, all alphabetic letters, all in the same case, but this allows for most existing variants of the game.) Provide test data and show the output here. The test data should include the answer string ALLOW and the guess string LOLLY, and the result should be (yellow, yellow, green, grey, grey) or equivalent.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Wordle comparison step by step in the Go programming language
The provided Go code is a simple implementation of the Wordle game. It takes two words as input, the answer word and the guess word, and returns an array of integers representing the color of each letter in the guess word based on the Wordle rules.
The color of each letter is determined as follows:
- 2: The letter is in the correct position and is the correct letter (green).
- 1: The letter is in the answer word but not in the correct position (yellow).
- 0: The letter is not in the answer word (grey).
The wordle
function is defined as follows:
func wordle(answer, guess string) []int {
n := len(guess)
if n != len(answer) {
log.Fatal("The words must be of the same length.")
}
answerBytes := []byte(answer)
result := make([]int, n) // all zero by default
for i := 0; i < n; i++ {
if guess[i] == answerBytes[i] {
answerBytes[i] = '\000' // Mark the letter green
result[i] = 2
}
}
for i := 0; i < n; i++ {
ix := bytes.IndexByte(answerBytes, guess[i])
if ix >= 0 {
answerBytes[ix] = '\000' // Mark the letter yellow
result[i] = 1
}
}
return result
}
The main function creates a slice of color names and a slice of word pairs. It then iterates through the word pairs, calling the wordle
function on each pair. The result of the wordle
function is an array of integers representing the color of each letter in the guess word. These integers are then converted to color names and printed to the console.
Here is an example of the output of the program:
ALLOW v LOLLY => [0 1 0 1 0] => [grey yellow grey yellow grey]
BULLY v LOLLY => [0 2 0 1 0] => [grey green grey yellow grey]
ROBIN v ALERT => [0 1 2 1 0] => [grey yellow green yellow grey]
ROBIN v SONIC => [0 1 2 0 0] => [grey yellow green grey grey]
ROBIN v ROBIN => [2 2 2 2 2] => [green green green green green]
Source code in the go programming language
package main
import (
"bytes"
"fmt"
"log"
)
func wordle(answer, guess string) []int {
n := len(guess)
if n != len(answer) {
log.Fatal("The words must be of the same length.")
}
answerBytes := []byte(answer)
result := make([]int, n) // all zero by default
for i := 0; i < n; i++ {
if guess[i] == answerBytes[i] {
answerBytes[i] = '\000'
result[i] = 2
}
}
for i := 0; i < n; i++ {
ix := bytes.IndexByte(answerBytes, guess[i])
if ix >= 0 {
answerBytes[ix] = '\000'
result[i] = 1
}
}
return result
}
func main() {
colors := []string{"grey", "yellow", "green"}
pairs := [][]string{
{"ALLOW", "LOLLY"},
{"BULLY", "LOLLY"},
{"ROBIN", "ALERT"},
{"ROBIN", "SONIC"},
{"ROBIN", "ROBIN"},
}
for _, pair := range pairs {
res := wordle(pair[0], pair[1])
res2 := make([]string, len(res))
for i := 0; i < len(res); i++ {
res2[i] = colors[res[i]]
}
fmt.Printf("%s v %s => %v => %v\n", pair[0], pair[1], res, res2)
}
}
You may also check:How to resolve the algorithm Find common directory path step by step in the Haskell programming language
You may also check:How to resolve the algorithm Matrix multiplication step by step in the Ursala programming language
You may also check:How to resolve the algorithm Binary digits step by step in the Locomotive Basic programming language
You may also check:How to resolve the algorithm Safe addition step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Set step by step in the Groovy programming language