Last active
May 18, 2022 20:25
-
-
Save joeesteves/4cc0ec0736e2764a8d01d352b2582cc8 to your computer and use it in GitHub Desktop.
Elixir Challenge
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 | |
def resolve(stocks,target) do | |
map = build_map(stocks, target) | |
count_matching(stocks, map) | |
end | |
defp build_map(stocks, target) do | |
Enum.reduce(stocks, %{}, fn stock, acc -> | |
Map.put(acc, (stock - target) * -1, true) | |
end) | |
end | |
defp count_matching(stocks, map) do | |
Enum.filter(stocks, fn stock -> Map.get(map, stock) end) | |
|> Enum.uniq() | |
|> Enum.count() | |
end | |
end | |
stocks = [5, 6, 9, 13, 11, 6, 6, 3, 3] | |
target = 12 | |
IO.puts Challenge.resolve(stocks, target) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment