Guessing numbers.

This is an interesting game. Jane chooses a random number (composed of four different digits from [0-9]) and gives John 10 chances to guess what the number is. Each time John provides a possible answer, Jane gives back a hint AB, of which A indicates how many rite digits with rite position there are in the answer that John just provided and B indicates the number of digits that are rite but not at the rite position.

e.g. Jane chooses the number '9205'.
John: 1234
Jane: 01
John: 5678
Jane: 01
John: 4387
Jane: 00
John: 9026
Jane: 12
John: 2095
Jane: 13
John: 9205
Jane; 40


John is a computer geek so he wants to write a program to figure out the rite number.

How would he code it? and how would you?

There may be a dozen of ways to do it. One of the most intuitive is kinda like how you brutely-crack password (remember? =).

An-easy-to-understand-and-simple-to-implement-but-maybe-not-the-perfect-algorithm:

1) Randomly pick up a number from possibilities (5040 possibilities in start).
2) Compute each possibilities' AB against the picked number.
3) Comparing with the hint, scale down the possibilies (whose AB == hint).
4) Repeat the steps above until there is only one possibility left.


Actual code always explains better than mere words. Here's a working sample. Even in the worst case, it can get the rite anwser after 7 guessings: (5040/1440)^7 == 6433. Enjoy.

Leave a comment

Recent Entries

  • Running Spaz on WebOS Emulator

    > You got root, you got everything. Yeah, Palm Pre is hot these days. Now with the [webos emulator](http://www.geektang.com/2009/06/linuxwebos.html) and [ssh](http://www.geektang.com/2009/06/sshwebos.html) available, you can have...

  • Haskell Platform

    >The Haskell Platform is a blessed library and tool suite for Haskell distilled from Hackage, along with installers for a wide variety of systems. IOW,...

  • When MT Meets iPhone/iPod Touch

    Just found [iMT](http://plugins.movabletype.org/imt/). It's the iphone / ipod touch interface for mt4. So now you can add/edit blog entries / comments on your favorite device...

  • All Project Euler Problems Solved

    [Project Euler](http://projecteuler.net) continues to be fun. First reached 100% when there were 240 problems. And have successfully guarded that honor since =). (As of this...

  • Million Digits of E, Sqrt(2), Pi

    > More puzzles, more fun. Besides [acm oj](http://acm.tju.edu.cn/toj/ranklist.html) and [projecteuler](http://projecteuler.net), i also like to solve the puzzles on [spoj](http://spoj.pl). The ACM/ICPC rules are rather limited...

Close