TIL: Read from STDIN in Elixir on HackerRank

Grab input data in HackerRank problems

2023-10-11
elixir

Grab the input provided by HackerRank from STDIN with:

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

If the input provided are numbers you might want to chain the above with:

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

In recent versions of Elixir (v1.13.0 or above) you might want to use IO.read(:stdio, :eof) instead.

But HackerRank is running Elixir 1.8.2 which don’t support the :eof option yet (LeetCode uses 1.15.7 by the way…).

You can look at the Elixir version from within the REPL with: System.version.