Skip to content

Instantly share code, notes, and snippets.

@dkarter
Created August 11, 2017 22:39
Show Gist options
  • Save dkarter/40a928e50784f32193713c287ddbb75c to your computer and use it in GitHub Desktop.
Save dkarter/40a928e50784f32193713c287ddbb75c to your computer and use it in GitHub Desktop.
defmodule ThreeTwentyThree do
def find_sum_zero_triplets(list) do
for x <- list, y <- list, z <- list, filter(list, x,y,z) do
Enum.sort([x,y,z])
end
|> Enum.uniq
end
defp filter(list, x,y,z) do
([x,y,z] -- list == []) && (x+y+z == 0)
end
def print(result) do
IO.inspect(result, label: "Count: #{length(result)}")
end
end
# -----------------------------------------------------------------------------
test_data = [
[4, 5, -1, -2, -7, 2, -5, -3, -7, -3, 1],
[-1, -6, -3, -7, 5, -8, 2, -8, 1],
[-5, -1, -4, 2, 9, -9, -6, -1, -7],
]
# print the results
test_data
|> Enum.map(&ThreeTwentyThree.find_sum_zero_triplets/1)
|> Enum.map(&ThreeTwentyThree.print/1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment