O'Reilly logo

Programming Elixir by Dave Thomas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Your Turn

  • Exercise: ModulesAndFunctions-6

    I’m thinking of a number between 1 and 1000.…

    The most efficient way to find the number is to guess halfway between the low and high numbers of the range. If our guess is too big, then the answer lies between the bottom of the range and one less than our guess. If our guess is too small, then the answer lies between one more than our guess and the end of the range.

    Your API will be guess(actual, range), where range is an Elixir range.

    Your output should look similar to this:

     
    iex> Chop.guess(273, 1..1000)
     
    Is it 500
     
    Is it 250
     
    Is it 375
     
    Is it 312
     
    Is it 281
     
    Is it 265
     
    Is it 273
     
    273

    Hints:

    • You may need to implement helper functions with an additional parameter (the currently guessed number).

    • The ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required