Last active
August 29, 2015 14:03
-
-
Save mgreenly/ce3996d05e4cd101d1d0 to your computer and use it in GitHub Desktop.
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
# 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