small medium large xlarge

### Posts by Luke Imhoff

My solution:

``````defmodule Chop do
def guess(actual, range) do
n = next_guess(range)
check_guess(actual, range, n)
end

defp next_guess(low..high) do
difference = high - low
guess = div(difference, 2) + low

IO.puts "Is it #{guess}"

guess
end

defp check_guess(actual, _, current_guess) when current_guess == actual do
actual
end

defp check_guess(actual, low.._, current_guess) when current_guess > actual do
high = current_guess - 1
n = next_guess(low..high)
check_guess(actual, low..high, n)
end

defp check_guess(actual, _..high, current_guess) when current_guess < actual do
low = current_guess + 1
n = next_guess(low..high)
check_guess(actual, low..high, n)
end
end ---
``````

P.S. What mark up do I use to make a hidden thing Dave like your “A Possible Solution”? It’s not standard Markdown, so it must be something specific to this forum.

Did I miss the ?character => integer somewhere earlier in the book? I ended up going with this since I knew only that single quotes were strings, and that single elements would give the integer:

``````defmodule MyList do
@caesar_start hd('a')
@caesar_end hd('z')
@caesar_width (@caesar_end - @caesar_start + 1)

def caesar([], _rotation) do
[]
end
def caesar([character | tail], rotation) do
offset = character - @caesar_start
rotated_offset = rem(offset + rotation, @caesar_width)
rotated_character = @caesar_start + rotated_offset

[rotated_character | caesar(tail, rotation)]
end
end ---
``````

I’m not sure pre-computing the start and end and width in attributes is faster or the Erlang VM would make the same lift out of the function. I do think this is better than hardcoding the 26 width.

I found that Erlang has a :string library, but confusingly, there strings are character lists, but since we’re just calling IO.puts and it doesn’t care, you can use :string.centre (note British English spelling even though the docs will say ‘center’ in the description).

``````defmodule DoubleQuotedStrings do
def center(list_of_strings) do
max_length = list_of_strings
|> Enum.map(&String.length/1)
|> Enum.max
list_of_strings
|> Enum.map(&String.to_char_list/1)
|> Enum.map(&(:string.centre(&1, max_length)))
|> Enum.map(&IO.puts/1)
end
end
``````

You can get rid of the argument to `Enum.join` with a slightly more complex regex as I used in my solution:

``````defmodule Sentences do
def captialize_sentences(string) do
String.split(string, ~r{(?<=\. )(?=.)})
|> Enum.map(&String.capitalize/1)
|> Enum.join
end
end
``````

The regex does a look behind `(?<=)` for `\. ` and then I had to add a look ahead `(?=)` for any character `.`, so that I didn’t get an empty final string after “woof. “.

georgi knox, if you’re using pragdave/httpotion, switch to orginal repository, myfreeweb/httpotion as the original author updated httpotion to use structs instead of records, so it works with 0.14. You do need to update the code to use the %{} syntax for structs tho. It's covered in the readme: https://github.com/myfreeweb/httpotion#usage. I'll post an errata once I figure out the new syntax.

First, here’s the change to use myfreeweb/httpotion: https://github.com/KronicDeth/issues/commit/7aa7dd83a0723def1e2fe3474575af442b56d377

Here’s the changes to use the structs version of HTTPotion instead of the out-dated Records version:

https://github.com/KronicDeth/issues/compare/59fe335d1258ae9c644d160b45a6fd367906f542…master

6 posts