[Tutor] Python_About "Guess my number" program
Steven D'Aprano
steve at pearwood.info
Wed Dec 12 03:52:52 EST 2018
Hi Dat, and welcome!
On Wed, Dec 12, 2018 at 11:26:30AM +0600, Hoang Quoc Dat wrote:
[...]
> After a while, I went on and search on the internet and
> find out 1 similar program but the coding logic is like: The computer keeps
> guessing the middle number of the range and narrow down the range by
> focusing on only the upper/lower part of the range after receving our
> answer.
Yes, that is the classic algorithm. It is a variant of binary search.
> I somehow have come to believe that the Python language and our logical
> thinking can do better than that option of choosing the middle number all
> the time but I still cannot find out the proper answer (probably because I
> am very much new to this). Could someone provide answer for this program?
If the only information you have is whether the secret number is
"higher" or "lower", then no, there is no better algorithm.
If you had more information, like "red hot, hot, warmer, warm, cool,
cold, colder, ice cold" to indicate how close the guess is, then you
might be able to do better, if you had some objective rule for deciding
how close "warm" etc was.
--
Steve
More information about the Tutor
mailing list