Newb ??

Fredrik Lundh fredrik at pythonware.com
Thu Nov 10 18:04:22 EST 2005


Steven D'Aprano wrote:

> Dude, that's what my code does, although I admit I took zero care to get
> the awkward boundary conditions right, nor did I put code in to stop the
> game after five attempts.

as my simulator shows, your code needs 5.87 attempts to make a correct
guess, on average.

if you add a 5-guess limit, it will find the right number only in 30% of all
runs.  here's a complete list of (max number of guesses; % chance that
you'll find the right answer given an even distribution of target numbers)

    1 1.0
    2 3.0
    3 6.9
    4 14.9
    5 30.7
    6 62.4
    7 100.0

fwiw, you may improve things slightly by special-casing the first guess:

    http://www.pomona.edu/Magazine/pcmfl00/1.shtml

</F>






More information about the Python-list mailing list