You are implementing the core logic for a simple word-guessing game.
There is:
L
.
For each guess you must return feedback consisting of:
exact_matches
: the number of positions
i
where
secret[i] == guess[i]
.
misplaced_matches
: the number of letters that appear in both
secret
and
guess
but
in different positions
, without double-counting any letter.
More formally:
secret
and
guess
be strings of length
L
.
i
contributes to
exact_matches
if
secret[i] == guess[i]
.
misplaced_matches
:
c
, count how many times
c
appears in the non-exact positions of
secret
and in the non-exact positions of
guess
.
misplaced_matches
the minimum of those two counts.
Implement a function with the following behavior:
get_feedback(dictionary: List[str], secret: str, guess: str) -> (int exact_matches, int misplaced_matches)
guess
is not in
dictionary
, you may assume the function should signal an invalid guess in a clear way (e.g., by raising an exception or returning
(-1, -1)
).
(exact_matches, misplaced_matches)
as defined above.
You can assume:
1 <= len(dictionary) <= 10^5
dictionary
, and
secret
and
guess
, have the same length
1 <= L <= 20
.
'a'
–
'z'
.
dictionary = ["apple", "angle", "ample"]
secret = "apple"
guess = "alley"
(this is in the dictionary or you may assume it is added for the example)
Then:
a p p l e
(secret)
`a l l e y` (guess)
exact_matches = 1
(only the first
'a'
matches in the same position)
p, p, l, e
l, l, e, y
'l'
(1 time),
'e'
(1 time)
misplaced_matches = 2
.
Design and implement an efficient algorithm to compute this feedback for a single guess.