How to resolve the algorithm Wordle comparison step by step in the V (Vlang) programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Wordle comparison step by step in the V (Vlang) 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 V (Vlang) programming language

Source code in the v programming language

fn wordle(answer string, guess string) []int {
    n := guess.len
    if n != answer.len {
        println("The words must be of the same length.")
    }
    mut answer_bytes := answer.bytes()
    mut result := []int{len:n} // all zero by default
    for i := 0; i < n; i++ {
        if guess[i] == answer_bytes[i] {
            answer_bytes[i] = u8(000)
            result[i] = 2
        }
    }
    for i := 0; i < n; i++ {
        ix := answer_bytes.index(guess[i])
        if ix >= 0 {
            answer_bytes[ix] = u8(000)
            result[i] = 1
        }
    }
    return result
}
 
fn main() {
    colors := ["grey", "yellow", "green"]
    pairs := [
        ["ALLOW", "LOLLY"],
        ["BULLY", "LOLLY"],
        ["ROBIN", "ALERT"],
        ["ROBIN", "SONIC"],
        ["ROBIN", "ROBIN"],
    ]
    for pair in pairs {
        res := wordle(pair[0], pair[1])
        mut res2 := []string{len: res.len}
        for i := 0; i < res.len; i++ {
            res2[i] = colors[res[i]]
        }
        println("${pair[0]} v ${pair[1]} => $res => $res2\n")
    }
}

  

You may also check:How to resolve the algorithm XML/XPath step by step in the Erlang programming language
You may also check:How to resolve the algorithm Animation step by step in the smart BASIC programming language
You may also check:How to resolve the algorithm Runge-Kutta method step by step in the EDSAC order code programming language
You may also check:How to resolve the algorithm Display a linear combination step by step in the Ada programming language
You may also check:How to resolve the algorithm Harshad or Niven series step by step in the Quackery programming language