Last active
July 5, 2019 00:18
-
-
Save ricmarinovic/913bb1ed1ca16d0a6b1c90a4e2997015 to your computer and use it in GitHub Desktop.
guess number
This file contains 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
defmodule Playground do | |
def guess(actual, a..b) do | |
do_guess(actual, a..b, div(b, 2)) | |
end | |
def do_guess(actual, a.._b, number) when actual < number do | |
print_number(number) | |
do_guess(actual, a..number, div(a + number, 2)) | |
end | |
def do_guess(actual, _a..b, number) when actual > number do | |
print_number(number) | |
do_guess(actual, number..b, div(number + b, 2)) | |
end | |
def do_guess(number, _, number) do | |
print_number(number) | |
IO.puts("#{number}") | |
end | |
def print_number(number), do: IO.puts("Is it #{number}") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment