Skip to content

Instantly share code, notes, and snippets.

View SophieDeBenedetto's full-sized avatar

Sophie DeBenedetto SophieDeBenedetto

  • GitHub
  • New York, NY
View GitHub Profile
defp get_two([first | [_second | rest]]) do
get_two([first | rest])
end
defp get_two([first | [second | _rest]]) when first + second == @sum do
first * second
end
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
defmodule Accountant do
@sum 2020
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
defmodule Accountant do
@sum 2020
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
# ...
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
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
defmodule Accountant do
@sum 2020
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
defmodule Accountant do
@sum 2020
def product_of_equals_twenty_twenty([_head | tail] = list) do
product = get_two(list)
if product do
product
else
# keep going
end
defmodule Accountant do
@sum 2020
# ...
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