How to resolve the algorithm Wordle comparison step by step in the J programming language
How to resolve the algorithm Wordle comparison step by step in the J 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 J programming language
Source code in the j programming language
wrdcmp=: {{
yw=.gr=. I.x=y
key=. '#' gr} x
for_ch.y do.
if.ch e. key do.
key=. '#' (key i.ch)} key
yw=. yw, ch_index
end.
end.
2 gr} 1 yw} (#y)#0
}}
wrdcmp=: {{
yw=. ;(] , ({.~1<.#)@-.)&.>/(<@I.y=/~x#~y~:x),<I.x=y
2 (I.x=y)} 1 yw} (#y)#0
}}
assert 1 1 2 0 0-: 'allow' wrdcmp 'lolly'
assert 0 0 2 2 2-: 'bully' wrdcmp 'lolly'
assert 0 0 0 1 0-: 'robin' wrdcmp 'alert'
assert 0 2 1 2 0-: 'robin' wrdcmp 'sonic'
assert 2 2 2 2 2-: 'robin' wrdcmp 'robin'
assert 0 0 2 1 0-: 'mamma' wrdcmp 'nomad'
assert 0 1 2 0 0-: 'nomad' wrdcmp 'mamma'
('allow' wrdcmp 'lolly')&{&.;: 'gray yellow green'
yellow yellow green gray gray
You may also check:How to resolve the algorithm Pernicious numbers step by step in the Rust programming language
You may also check:How to resolve the algorithm Ethiopian multiplication step by step in the Nemerle programming language
You may also check:How to resolve the algorithm Sailors, coconuts and a monkey problem step by step in the Python programming language
You may also check:How to resolve the algorithm Count the coins step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Loops/For step by step in the TransFORTH programming language