Last active
August 29, 2015 14:20
-
-
Save bcardarella/fadecd14266bf5582d17 to your computer and use it in GitHub Desktop.
challenges.exs
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
defmodule Challenge do | |
# Problem 1 | |
def sum(list) do | |
_sum(list, 0) | |
end | |
defp _sum([], total), do: total | |
defp _sum([head|tail], total), do: _sum(tail, total+head) | |
# Problem 2 | |
def alt(list1, list2) do | |
_alt(list1, list2, []) | |
end | |
defp _alt([], [], combined), do: Enum.reverse combined | |
defp _alt([head1|tail1], [head2|tail2], combined), do: _alt(tail1, tail2, [head2|[head1|combined]]) | |
# Problem 3 | |
def fib(count) do | |
_fib([0, 1], count - 2) | |
end | |
defp _fib(list, count) when count > 0 do | |
left = Enum.at(list, -2) | |
right = Enum.at(list, -1) | |
_fib(List.insert_at(list, -1, left + right), count - 1) | |
end | |
defp _fib(list, _), do: list | |
# Problem 4 | |
def arrange(list) do | |
Enum.sort(list, fn(x, y) -> | |
first_digit = fn(n) -> | |
n | |
|> Integer.to_string | |
|> String.at(0) | |
|> String.to_integer | |
end | |
first_digit.(x) > first_digit.(y) | |
end) | |
|> Enum.join | |
|> String.to_integer | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment