-4

I've received a question to solve. I've manage to solved it by hand but couldn't find an exact algorithm to solved it.

The question: The user insert a 4 digits number without repetition of any of the digits (can have a leading 0). The algorithm needs to find this number.

There is a check function that received a 4 digits number and returns how many numbers were correct, and how many of the numbers were in the correct place without overlap, and without mentioning which one is. The system has 7 tries.

For example: The user insert the Number: 0584

If the system sends the number: 6549

The return would be:

1 item in place
1 item is correct

David
  • 287
  • 2
  • 3
  • 13
  • 1
    If you solved this by hand, then you can figure out an algorithm by yourself. We're not here to do your homework for you. – r3mainer Jul 14 '17 at 13:09
  • when I said i solved it by hand, i meant that i couldn't find any consistent algorithm that would succeed in any case. – David Jul 14 '17 at 13:11
  • Try here: https://en.wikipedia.org/wiki/Bulls_and_Cows – r3mainer Jul 14 '17 at 13:16
  • 1
    What seems to be a problem in this question, it's seems like you are matching two numbers given by the user input and by the computer... so just run a for loop and check for the numbers that are not in place – zenwraight Jul 14 '17 at 13:29
  • If you have no idea at all you can simply try all possible combinations. But if you think about it for a while you will figure out a method to use the answers to eliminate some digits which could not occur in the solution. – MrSmith42 Jul 14 '17 at 14:07
  • you have a 7 guess limit... – David Jul 14 '17 at 14:07
  • Possible duplicate of [Mastermind minimax algorithm](https://stackoverflow.com/questions/20298190/mastermind-minimax-algorithm) – Prune Jul 14 '17 at 16:33
  • This game is known as "Bulls and Cows" or "pico, fermi, and bagels". It's marketed under the name "Mastermind". Use those keywords in your research. – Prune Jul 14 '17 at 16:35

1 Answers1

0

I would try to solve this puzzle in 2 steps:

  1. Try to find out which 4 digits the solution contains of.
  2. For the miss-placed positions find the right order.

I'm not sure if there is an algorithm to always find the solution within 7 requests, but this way you should at least get closer to a solution.

It's a puzzle, so think about digit-patters that reveal as much information about the solution as possible. And try to extract as much information from the answer to reduce the number of still possible solutions.

MrSmith42
  • 9,961
  • 6
  • 38
  • 49