2.9 Counting the Guesses
Using the binary search method, the computer is able to guess the secret number in just a few attempts. In fact, the maximum number of attempts is related to the range of numbers being searched. The maximum number of attempts to find the secret number in a group of n numbers should be no more than log2(n). So for our range of 100 numbers, calculating log2(100) is 6.6. Rounding off, the maximum number of guesses for 100 numbers is 7. We can verify this by counting the number of guesses the computer makes. To do that, we can use the accumulator pattern.
2.9.1 The Accumulator Pattern
An important problem-solving technique is the accumulator pattern. Your ability to recognize this commonly occurring pattern and then implement ...
Get Python Programming in Context, 4th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.