Created
December 7, 2020 21:33
-
-
Save SophieDeBenedetto/62b0364aefd045a28f2d8e82157ef95e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Accountant do | |
| @sum 2020 | |
| defguard equals_twenty_twenty(first, second) when first + second == @sum | |
| def product_of_equals_twenty_twenty(list, product \\ nil) | |
| def product_of_equals_twenty_twenty(_list, product) when product != nil, do: product | |
| def product_of_equals_twenty_twenty([_head | tail] = list, _product) do | |
| product_of_equals_twenty_twenty(tail, get_two(list)) | |
| end | |
| defp get_two(list) when length(list) == 1, do: nil | |
| defp get_two([first | [second | _rest]]) when equals_twenty_twenty(first, second) 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