Guess My Number Game

Andrea Griffini agriff at tin.it
Sun May 16 16:27:22 EDT 2004


On Sun, 16 May 2004 14:57:28 GMT, "Andrew Dalke"
<adalke at mindspring.com> wrote:

>And not really an answer to the OP; here's a solution which uses
>the bisect module, letting it do the heavy work.  It uses the same sort
>of guess function you have, tied to Python's __cmp__.  It isn't an answer
>because it's rather too complicated for someone learning programming.

It also has the same problem about guessing too many times.

bisect_left looks for a position in a sorted list given a value;
the "guess the number" game is different, and you're looking
for a value instead. You can come close by using a range as
the artificial list, but you will end up guessing even when
it's not needed (e.g. if answered "low" for 8 and "high" for 6
you don't need another guess; you already know the answer is 7).

It's not bisect_left fault, of course; that function is more
general and can't take advantage from knowing that the list
that is being passed is indeed a range of integers.

It's also funny to see a bisection search called the "heavy
work": the convoluted (and IMO somewhat ugly) code used to
be able to call bisect is much more complex and less
intuitive than the bisection code itself.

To me seems it would be more accurate saying that the heavy
work is trying to call bisect for a use that isn't what
bisect was written for.

Andrea



More information about the Python-list mailing list