Created
November 24, 2016 00:12
-
-
Save PandaWhisperer/505654d049ae6ffe133c25c26f961b0e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Johnann do | |
def john(n) when n >= 1 do | |
john_and_ann(n)[:john] | |
end | |
def ann(n) when n >= 1 do | |
john_and_ann(n)[:ann] | |
end | |
def sum_john(n) when n >= 1 do | |
john(n) |> Enum.sum | |
end | |
def sum_ann(n) when n >= 1 do | |
ann(n) |> Enum.sum | |
end | |
defp john_and_ann(n, katas \\ %{john: %{0 => 0}, ann: %{0 => 1}}) do | |
k = map_size(katas[:john]) | |
if k < n do | |
john = Map.put(katas[:john], k, k - katas[:ann][katas[:john][k-1]]) | |
ann = Map.put(katas[:ann], k, k - katas[:john][katas[:ann][k-1]]) | |
john_and_ann(n, %{ john: john, ann: ann }) | |
else | |
katas | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment