Created
October 10, 2016 12:00
-
-
Save nathanchere/a08f43c5de701f383be2c1312f206c64 to your computer and use it in GitHub Desktop.
HackerRank CCTI data structures 06
This file contains hidden or 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
# HackerRank "Cracking The Coding Interview" - Data Structures (Queues) - A Tale of Two Stacks | |
defmodule Solution do | |
def process_commands(counter, queue) when counter == 0, do: nil | |
def process_commands(counter, queue) do | |
command = IO.gets("") | |
|> String.strip | |
|> process_command(counter - 1, queue) | |
end | |
def process_command("1 " <> arg, counter, queue), do: process_commands(counter, queue ++ [arg]) | |
def process_command("2", counter, [head | queue]), do: process_commands(counter, queue) | |
def process_command("3", counter, queue) do | |
IO.puts(Enum.at(queue, 0)) | |
process_commands(counter, queue) | |
end | |
def main() do | |
IO.gets("") |> String.strip |> String.to_integer | |
|> process_commands([]) | |
end | |
end | |
Solution.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment