Problem 46603. Higher! Lower! Correct!
In he game of "Guess the number", the game leader mentally pictures a number between a lower and higher number, and then asks the contestant to guess the number. If the contestant guesses the number right, they are told "Correct!" and the game is over. If the contestant guesses wrong, they are told "Higher!" or "Lower!" and they are allowed another guess. This continues until the contestant guesses the right number.
You are asked to produce the sequence of guesses if the contestant applies the bisection strategy: they will always guess the value in the middle between the lower and upper bound (rounding down if necessary), and then update their understanding of the lower and upper bound, based on the outcome of the guesses.
This is how a game could proceed:
(Joe mentally pictures the number 18)
Joe: "Guess a number between 10 and 30."
(Sam guesses between 10 and 30)
Sam: "20"
Joe: "Lower!"
(Sam guesses between 10 and 19)
Sam: "14"
Joe: "Higher!"
(Sam guesses between 15 and 19)
Sam: "17"
Joe: "Higher!"
(Sam guesses between 18 and 19)
Sam: "18"
Joe: "Correct!"
In this case the right solution would be: guesses = [20 14 17 18];
Note: this problem is based on Problem 2635 but corrects how the upper and lower bounds are updated through the game.
Solution Stats
Problem Comments
-
1 Comment
Josh
on 30 Aug 2023
i think there's a slight typo in the example... the second guess is meant to bisect the numbers [10,19] because the first guess (20) was too high. 14.5 bisects [10,19], which given the problem's ask to round down, results in a second guess of 14, not 15
i ran my solution through high_or_low(10, 20, 18) and can confirm the expected solution is [20 14 17 18]
great problem!
Solution Comments
Show commentsProblem Recent Solvers13
Suggested Problems
-
Maximum running product for a string of numbers
2197 Solvers
-
295 Solvers
-
280 Solvers
-
Convert a numerical matrix into a cell array of strings
2057 Solvers
-
808 Solvers
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!