How to resolve the algorithm Wordle comparison step by step in the Swift programming language
How to resolve the algorithm Wordle comparison step by step in the Swift 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 Swift programming language
Source code in the swift programming language
enum Colour : CustomStringConvertible {
case grey
case yellow
case green
var description : String {
switch self {
case .grey: return "grey"
case .yellow: return "yellow"
case .green: return "green"
}
}
}
func wordle(answer: String, guess: String) -> [Colour]? {
guard answer.count == guess.count else {
return nil
}
var a = Array(answer)
let g = Array(guess)
let n = a.count
var result = Array(repeating: Colour.grey, count: n)
for i in 0..<n {
if g[i] == a[i] {
a[i] = "\0"
result[i] = Colour.green
}
}
for i in 0..<n {
if let j = a.firstIndex(of: g[i]) {
a[j] = "\0"
result[i] = Colour.yellow
}
}
return result
}
let pairs = [("ALLOW", "LOLLY"), ("BULLY", "LOLLY"),
("ROBIN", "ALERT"), ("ROBIN", "SONIC"),
("ROBIN", "ROBIN")]
for pair in pairs {
if let result = wordle(answer: pair.0, guess: pair.1) {
print("\(pair.0) v \(pair.1) => \(result)")
}
}
You may also check:How to resolve the algorithm Sorting algorithms/Permutation sort step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Ascending primes step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Forest fire step by step in the Ceylon programming language
You may also check:How to resolve the algorithm Rot-13 step by step in the FALSE programming language
You may also check:How to resolve the algorithm Literals/Floating point step by step in the Go programming language