How to resolve the algorithm Guess the number/With feedback (player) step by step in the UNIX Shell programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Guess the number/With feedback (player) step by step in the UNIX Shell programming language
Table of Contents
Problem Statement
Write a player for the game that follows the following rules: The computer should guess intelligently based on the accumulated scores given. One way is to use a Binary search based algorithm.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Guess the number/With feedback (player) step by step in the UNIX Shell programming language
Source code in the unix programming language
read -p "Lower bound: " lower
read -p "Upper bound: " upper
moves=0
PS3="> "
while :; do
((moves++))
guess=$(( lower + (upper-lower)/2 ))
echo "Is it $guess?"
select ans in "too small" "too big" "got it!"; do
case $ans in
"got it!") break 2 ;;
"too big") upper=$(( upper==guess ? upper-1 : guess )); break ;;
"too small") lower=$(( lower==guess ? lower+1 : guess )); break ;;
esac
done
((lower>upper)) && echo "you must be cheating!"
done
echo "I guessed it in $moves guesses"
You may also check:How to resolve the algorithm Sierpinski carpet step by step in the Asymptote programming language
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the Sidef programming language
You may also check:How to resolve the algorithm Rep-string step by step in the Python programming language
You may also check:How to resolve the algorithm Range extraction step by step in the K programming language
You may also check:How to resolve the algorithm Anagrams step by step in the SuperCollider programming language