Skip to content

Instantly share code, notes, and snippets.

@alexdovzhanyn
Created August 26, 2016 02:01
Show Gist options
  • Save alexdovzhanyn/a5b7130a8fed1ac09f8e538f705150c1 to your computer and use it in GitHub Desktop.
Save alexdovzhanyn/a5b7130a8fed1ac09f8e538f705150c1 to your computer and use it in GitHub Desktop.
puts "Min?"; @minimum = gets.chomp.to_i; @base = @minimum
puts "Max?"; @maximum = gets.chomp.to_i; @guesses = 0
def search(min, max)
guess = min + (max - min) / 2
puts guess
answer(min, max, guess)
end
def answer(min, max, guess)
ans = gets.chomp
if ans == "yes"
@guesses += 1
puts "Nice! Took #{@guesses} guesses!"
elsif ans == "tl"
min = guess
@guesses += 1
search(min, max)
elsif ans == "th"
max = guess
@guesses += 1
search(min, max)
end
end
search(@minimum, @maximum)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment