Last active
September 26, 2019 09:46
-
-
Save vysogot/78554efbd4de3c24a41eb1787d84fa47 to your computer and use it in GitHub Desktop.
Odd elements of list in Elixir
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 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) |
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
IO.read(:all) | |
|> String.split("\n") | |
|> Enum.drop_every(2) | |
|> Enum.join("\n") | |
|> IO.puts |
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
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