liveBook · Manning

Misplaced digit

As explained in Learn AI-Assisted Python Programming, Second Edition

A misplaced digit is a guessed digit that occurs in the secret code but is located at a different position.

A misplaced digit appears in the secret code but occupies a different position from the one in the guess. It differs from a correct digit, which matches both value and position. section 10.3.1

Comparing 3821 with 1862

The guess 3821 has one correct digit: 8 is second in both strings. Its 2 and 1 occur in the secret code but at different positions, so the result is one correct and two misplaced digits. section 10.3.1

A feedback function checks each position: it counts a matching character as correct; otherwise, it counts the character as misplaced when that character exists elsewhere in the secret code. Using elif prevents a correct digit from being counted twice. section 10.3.4

Table 10.1 Example of playing the game
Guess
Misplaced
Correct
Comments
0123
1
0
One of 0, 1, 2, 3 is in the answer; none are in the correct location.
4567
3
0
Three of 4, 5, 6, 7 are in the answer; none are in the correct location.
9045
0
1
Because one number from 0123 and three numbers from 4567 are in the answer, we know 8 and 9 aren’t in the answer. We know at least one of the numbers 4 or 5 must be in the answer from prior guesses and that 0 could be in the answer. One correct means that either 4 or 5 is in the correct location, either 4 or 5 isn’t present in the solution, and 0 isn’t in the solution.
9048
0
0
We know 8, 9, and 0 aren’t in the answer from prior guesses. Zero correct and zero misplaced tells us 4 is also not in the answer, and from the previous guess, we now know that 5 is the last digit.
1290
1
0
Going back to the original guess, we want to know which digit of 1, 2, and 3 is in the answer. We know 9 and 0 aren’t in the answer, so one misplaced means either 1 or 2 is in the answer and 3 isn’t in the answer. In addition, whichever of the numbers 1 and 2 are in the answer, it’s currently in the wrong spot.
6715
2
1
Because 4 isn’t in the solution, we know from the second guess that 5, 6, and 7 are. Our guess here tells us that 1 isn’t in the answer and that 6 and 7 are in the wrong place. Since 1 isn’t in the answer, 2 must be (from the previous guess). Because 5 is at the end and we’ve tried 2 in the second and third position previously with zero correct, 2 must be in the first position. Because we’ve tried 6 in the first and third position and neither were correct, 6 must be in the second position. That leaves 7 for the third position. We’ve got it.
2675
0
4
Yes, this is correct.

Use the example table to follow how successive guesses and their correct or misplaced counts narrow the possible secret code. section 10.3.1

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Explains the definition, works through examples, and implements position-by-position counting, without addressing duplicate-digit scoring.
Examples
Demonstrates 3821 against 1862, 1234 against 4321, and successive guesses in a feedback table.
What it assumes
Assumes basic Python functions, strings, loops, indexing, and conditionals while explaining the game rules from the beginning.
What you can do after
Build it from scratch by implementing and testing a Python function that returns correct and misplaced digit counts.
How it connects
Connects misplaced digits with correct digits, feedback functions, top-down design, and Wordle.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.