Created
January 26, 2022 14:39
-
-
Save caioertai/e46d99d1679fc95019a446227a4b5eb0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
start_time = Time.now | |
min = 1 | |
# We can optionally represent add the commas of large numbers | |
# as _ in Ruby for better legibility | |
max = 100_000_000 | |
#Generate random price between a min and max (initially 1 and 100) | |
price = rand(min..max) | |
# Starts at 0 and is increased at every guess | |
tries_counter = 0 | |
loop do | |
# "Player" guess (now our algo) | |
guessed = (min + max) / 2 | |
tries_counter += 1 | |
if guessed == price | |
puts "Greattttt! :)" | |
puts "Took you #{tries_counter} tries" | |
puts "And #{Time.now - start_time} seconds" | |
break | |
else | |
if guessed > price | |
max = guessed - 1 | |
else | |
min = guessed + 1 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment