Skip to content

Instantly share code, notes, and snippets.

@SophieDeBenedetto
Created December 7, 2020 21:31
Show Gist options
  • Select an option

  • Save SophieDeBenedetto/df2a8d6fb28b55bdd20e34c9f3c2a040 to your computer and use it in GitHub Desktop.

Select an option

Save SophieDeBenedetto/df2a8d6fb28b55bdd20e34c9f3c2a040 to your computer and use it in GitHub Desktop.
defmodule Accountant do
@sum 2020
def product_of_equals_twenty_twenty([]), do: :noop
def product_of_equals_twenty_twenty([_head | tail] = list) do
product = get_two(list)
if product do
product
else
product_of_equals_twenty_twenty(tail)
end
end
defp get_two(list) when length(list) == 1, do: nil
defp get_two([first | [second | _rest]]) when first + second == @sum do
first * second
end
defp get_two([first | [_second | rest]]) do
get_two([first | rest])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment