Created
October 8, 2021 15:30
-
-
Save adamrobbie/87c9cecc15a55034743c937c11dfdda8 to your computer and use it in GitHub Desktop.
Interview question solution
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 Test do | |
@moduledoc """ | |
Documentation for `Test`. | |
""" | |
@spec find_stack_pair(any) :: Integer | |
def find_stack_pair(list) do | |
list | |
|> pairwise() | |
|> IO.inspect(label: "after pairing") | |
|> calculate() | |
|> List.last() | |
end | |
defp pairwise(list) do | |
list | |
|> Enum.chunk_every(2, 1, :discard) | |
end | |
defp calculate(list) do | |
list | |
|> Enum.map(&Enum.sum/1) | |
|> Enum.sort() | |
|> duplicates() | |
end | |
defp duplicates(list) do | |
acc_dupes = fn(x, {elems, dupes}) -> | |
case Map.has_key?(elems, x) do | |
true -> {elems, Map.put(dupes, x, nil)} | |
false -> {Map.put(elems, x, nil), dupes} | |
end | |
end | |
acc_dupes | |
list |> Enum.reduce({%{}, %{}}, acc_dupes) |> elem(1) |> Map.keys() | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Get better.