How to resolve the algorithm Wordle comparison step by step in the Yabasic programming language
How to resolve the algorithm Wordle comparison step by step in the Yabasic 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 Yabasic programming language
Source code in the yabasic programming language
// Rosetta Code problem: http://rosettacode.org/wiki/Wordle_comparison
// by Galileo, 02/2022
sub wordle$(answer$, guess$)
local n, i, k, result$
n = len(guess$)
if n = len(answer$) then
result$ = left$("0000000000000000000", n)
for i = 1 to n
if mid$(guess$, i, 1) = mid$(answer$, i, 1) then
mid$(answer$, i, 1) = "0"
mid$(result$, i, 1) = "2"
end if
next
for i = 1 to n
k = instr(answer$, mid$(guess$, i, 1))
if k then
mid$(answer$, k, 1) = "0"
mid$(result$, i, 1) = "1"
end if
next
else
print "words must be same length"
end if
return result$
end sub
data "ALLOW", "LOLLY", "BULLY", "LOLLY", "ROBIN", "ALERT", "ROBIN", "SONIC", "ROBIN", "ROBIN"
dim colours$(3) : colours$(0) = "grey" : colours$(1) = "yellow" : colours$(2) = "green"
for i = 1 to 5
read answer$, guess$
res$ = wordle$(answer$, guess$)
print answer$, " v ", guess$, " => ";
for j = 1 to len(res$)
print colours$(val(mid$(res$, j, 1))), " ";
next
print
next
You may also check:How to resolve the algorithm Combinations with repetitions step by step in the Erlang programming language
You may also check:How to resolve the algorithm Sierpinski triangle/Graphical step by step in the Quackery programming language
You may also check:How to resolve the algorithm Atomic updates step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Sequence of primes by trial division step by step in the FileMaker programming language
You may also check:How to resolve the algorithm Matrix multiplication step by step in the SQL programming language