Created
August 11, 2017 22:39
-
-
Save dkarter/40a928e50784f32193713c287ddbb75c to your computer and use it in GitHub Desktop.
Solution: https://www.reddit.com/r/dailyprogrammer/comments/6melen/20170710_challenge_323_easy_3sum/
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 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