Skip to content

TIL: Read HackerRank input from standard input in Elixir

Read and parse line-based HackerRank input in Elixir.

1 min read

Read all input supplied by HackerRank  through standard input, then split it into non-empty lines:

Elixir
input =
IO.read(:stdio, :eof)
|> String.split("\n", trim: true)

If each line contains one integer, parse the lines after splitting them:

Elixir
input =
IO.read(:stdio, :eof)
|> String.split("\n", trim: true)
|> Enum.map(&String.to_integer/1)

The :eof option has been available since Elixir 1.13. When I wrote this note in October 2023, HackerRank used Elixir 1.8.2, so its editor required the older :all option instead:

Elixir
input =
IO.read(:stdio, :all)
|> String.split("\n", trim: true)

Check the active Elixir version with System.version() before choosing between them. Online coding platforms can change their available runtimes independently.

More posts connected by shared tags.