Last active
September 25, 2022 03:25
-
-
Save uxjp/1a0fdeec0edfc4b369f6a39c19cf4726 to your computer and use it in GitHub Desktop.
Sum_odd_indexes_in_a_list
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
# 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 |
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 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