Skip to the content

The color code

Problem

Nadia has spent half an hour on the code-breaking minigame: the machine hides a row of colored pegs and answers every guess with two numbers. Write a function that takes secret and guess, lists of integers (one number per color) of the same length, with at least one peg. Return the list [exact, moved]. exact counts the positions where both lists have the same color. Then each peg of the guess that was not exact looks in the secret for one of its color that was not exact and is not used yet; if it finds one, add 1 to moved and that secret peg is used up.

No peg of the secret ever counts twice.

With secret [3, 1, 1, 4] and guess [1, 1, 5, 1], position 1 is exact. That leaves 3, 1 and 4 in the secret, and 1, 5 and 1 in the guess: the first 1 takes the only free 1 and the other finds none. It is [1, 1].

Examples

  • The example

    [3, 1, 1, 4], [1, 1, 5, 1] → [1, 1]

  • All in place

    [2, 5, 7], [2, 5, 7] → [3, 0]

  • All moved

    [1, 2, 3], [3, 1, 2] → [0, 3]

  • No colors match

    [1, 2], [3, 4] → [0, 0]

  • Repeats on both sides

    [1, 1, 2], [1, 2, 1] → [1, 2]

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def clues(secret, guess):
    pass

JavaScript

function clues(secret, guess) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n²) challenges

See all challenges →