Skip to content

Instantly share code, notes, and snippets.

@vysogot
Last active September 26, 2019 09:46
Show Gist options
  • Save vysogot/78554efbd4de3c24a41eb1787d84fa47 to your computer and use it in GitHub Desktop.
Save vysogot/78554efbd4de3c24a41eb1787d84fa47 to your computer and use it in GitHub Desktop.
Odd elements of list in Elixir
defmodule Solution do
def odds(elements) do
Enum.with_index(elements)
|> Enum.map_reduce([], fn {element, index}, acc ->
case rem(index, 2) == 1 do
true -> {{element, index}, acc ++ [element]}
_ -> {{element, index}, acc}
end
end)
|> elem(1)
end
end
input = IO.read(:stdio, :all)
|> String.split("\n")
|> Enum.map(fn x ->
Integer.parse(x) |> elem(0)
end)
Solution.odds(input)
|> Enum.each(fn x -> IO.puts x end)
IO.read(:all)
|> String.split("\n")
|> Enum.drop_every(2)
|> Enum.join("\n")
|> IO.puts
f :: [Int] -> [Int]
f (_:x:xs) = x : f xs
f _ = []
-- This part deals with the Input and Output and can be used as it is. Do not modify it.
main = do
inputdata <- getContents
mapM_ (putStrLn. show). f. map read. lines $ inputdata
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment