Skip to content

Instantly share code, notes, and snippets.

@eibrahim
Created February 2, 2016 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eibrahim/4e0b464d8adac4b098bc to your computer and use it in GitHub Desktop.
Save eibrahim/4e0b464d8adac4b098bc to your computer and use it in GitHub Desktop.
Guess the number within a range
defp midpoint a,b do div(a+b,2) end
def guess(my_choice, a.._) when my_choice == a do IO.puts "is it #{a}?" end
def guess(my_choice, _..b) when my_choice == b do IO.puts "is it #{b}?" end
def guess(my_choice, a.._) when my_choice < a do IO.puts "out of range" end
def guess(my_choice, _..b) when my_choice > b do IO.puts "out of range" end
def guess(my_choice, a..b) when my_choice > div((a+b),2) do
IO.puts "is it #{midpoint(a,b)}?"
guess my_choice, midpoint(a,b)..b
end
def guess(my_choice, a..b) when my_choice < div((a+b),2) do
IO.puts "is it #{midpoint(a,b)}?"
guess my_choice, a..midpoint(a,b)
end
def guess(my_choice, a..b) when my_choice == div((a+b),2) do
IO.puts "i got it. it is #{midpoint(a,b)}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment