Skip to content

Instantly share code, notes, and snippets.

@mgreenly
Last active August 29, 2015 14:03
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 mgreenly/ce3996d05e4cd101d1d0 to your computer and use it in GitHub Desktop.
Save mgreenly/ce3996d05e4cd101d1d0 to your computer and use it in GitHub Desktop.
# Programming Elixir - exercise: ModulesAndFunctions-6
#
# Example:
#
# iex(3)> GuessingGame.search(279, 0..1000)
# is it 500
# is it 250
# is it 375
# is it 312
# is it 281
# is it 265
# is it 273
# is it 277
# 279
# :ok
#
defmodule GuessingGame do
def midpoint(min..max) do
div(min + max, 2)
end
def search(actual, range) do
search(actual, range, midpoint(range))
end
def search(actual, min..max, guess)
when guess < actual do
IO.puts "is it #{guess}"
search(actual, midpoint(min..max)..max)
end
def search(actual, _range, guess)
when guess == actual do
IO.puts actual
end
def search(actual, min..max, guess)
when guess > actual do
IO.puts "is it #{guess}"
search(actual, min..midpoint(min..max))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment