Skip to content

Instantly share code, notes, and snippets.

@uxjp
Last active September 25, 2022 03:25
Show Gist options
  • Save uxjp/1a0fdeec0edfc4b369f6a39c19cf4726 to your computer and use it in GitHub Desktop.
Save uxjp/1a0fdeec0edfc4b369f6a39c19cf4726 to your computer and use it in GitHub Desktop.
Sum_odd_indexes_in_a_list
# I miss interpreted the function, but the outcome was cool
defmodule ListUtil do
def su(list) do
sum(list, 0)
end
def sum([head | tail], acc) do
sum(tail, head + acc)
end
def sum([], acc) do
acc
end
end
defmodule Solution do
IO.read(:all)
|> String.split
|> Enum.map(&String.to_integer/1)
|> (&(Enum.drop_every([0 | &1], 2))).()
|> ListUtil.su
|> IO.inspect
end
defmodule ListUtil do
def su(list) do
sum(list, 0)
end
def sum([head | tail], acc) do
sum(tail, head + acc)
end
def sum([], acc) do
acc
end
end
defmodule Solution do
IO.read(:all)
|> String.split
|> Enum.map(&String.to_integer/1)
|> Enum.filter(&(rem(&1,2) != 0))
|> ListUtil.su
|> IO.inspect
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment