Think of a number between 0 and 100. Got it? Alright I am 100% sure I can guess it! Prove me wrong in the app of today called the numbers game.
Recently I started my journey on a formal background of computer science. It’s interesting and thus far I have had a lot of “oooh” and “aaaah! that’s what it’s called!” moments. Today I learned about the bisection method. This method can be applied to quickly find a value within a sortable collection. The way it works is cool! In this application it’s applied to create a fun guessing game!
The user is asked to pick a number between 0 and 100. However, for simplicity sakes let’s limit this to a number between 0 and 10. Given the user picked 6. Where do we start guessing? The answer is right in the middle!
10 / 2 // 5
Now we ask the user if 5 is higher or lower than the solution. It’s higher so now we can disregard every number below 5. Chopping the possibilities in half! Seems a bit confusing? Don’t worry we are not done yet!
We know that the correct number is between 5 and 10. What would our next guess be?
(10 - 5) / 2 + 5 // 7
Again, we prompt the user. The user lets us know it’s too high. The next guess is in between 5 and 7. Namely … 6!
To reiterate. The algorithm divides the possibilities in half. Then determines whether the correct value is in the lower or higher half. We then disregard the half we don’t need. This continues until we find the solution!
Not convinced the app can guess your solution? I challenge you to give it your best shot!
Thank you for reading and leave a star on the repo.