Home Posts Tags Post Search Tag Search

Post 25

Hacker Rank 01 - Trying to do Something Everyday

Published on: 2025-05-09 Tags: elixir, Blog, Testing, Hacker Rank
So for Hacker Rank they make you get all the information that you will need for the examples with the STDIN or STDOUT which in Elixir is going to have to use

IO

So there is:
IO.read()
IO.gets()

IO.read(:stdio, :all)
IO.gets("")

You will have to do some pattern matching or list work to get all the info that you will need.

Like if the first param is something that tells us something about the next 2 and then after those 3 we have the full list of things to worry about we might do something like:

[telling | info1 | info2 | rest] = 
IO.read(:stdin, :all)
|> String.split("\n")
|> Enum.map(fn x -> String.to_integer(x) end)

Yeah its not always the best but you can get away with it if you know what you need.

So for today I had these questions:
1. Create an array/list of size n
defmodule Solution do
  def read(entered \\ "") do
    IO.read(:stdio, :all)
    |> String.trim()
    |> String.to_integer()
  end

  def main(entered \\ "") do
    integer =
      if entered == "" do
        read()
      else
        entered
      end

    Enum.map(0..integer - 1, fn digit ->
      digit
    end)
  end
end

2. Reverse a list (to IO.puts) 
defmodule Solution do
    def read do
      IO.read(:stdio, :all)
      |> String.split("\n")
      |> Enum.map(fn x -> String.to_integer(x) end)
    end
    
    def main do
        list = read()
        |> recursive()
    end
    
    def recursive(list) do
        if list == [] do
            nil
        else
            [head | tail] = list
            recursive(tail)
            IO.puts(head)
        end
    end
end

Solution.main()

3. Sum only the odd numbers in a list and return only the final sum.
defmodule Solution do
    def read do
        IO.read(:stdio, :all)
        |> String.split("\n")
        |> Enum.map(&String.to_integer(&1))
        
    end

    def main do
        read()
        |> Enum.filter(fn(x) -> rem(x, 2) != 0 end)
        |> Enum.sum()
        |> IO.puts()
    end
end

Solution.main()

4. Count the size of a list without size/length.
defmodule Solution do
    def read do
        IO.read(:stdio, :all)
        |> String.split("\n")
        |> Enum.reduce(0, fn _, acc -> acc + 1 end)
        |> IO.puts()
    end
end

Solution.read()

5. Print the Absolute value of any given integer.
defmodule Solution do
    def main do
        IO.read(:stdio, :all)
        |> String.split("\n")
        |> Enum.map(&String.to_integer(&1))
        |> Enum.each(fn digit -> 
             if digit >= 0, do: IO.puts(digit),
             else: IO.puts(-1 * digit)
        end)
    end
end

Solution.main()

5. Output N lines, each containing the value of the series expansion of e^x, computed by your program.
defmodule Solution do
    def read do
        IO.read(:stdio, :all)
        |> String.split("\n")
        |> Enum.filter(fn x -> x != "" end)
        |> Enum.map(fn x -> Float.parse(x) |> elem(0) end)
    end    
    
    def factorial(1), do: 1
    def factorial(x), do: x * factorial(x - 1) 
    
    def pow(_, 0), do: 1
    def pow(x, 1), do: x
    def pow(x, e), do: x * pow(x, e - 1)
    
    def mathematics(value) do
        return = Enum.map(1..9, fn x -> pow(value, x) / factorial(x) end)
        |> Enum.sum()
        return + 1
      end
    def main do
        [_ | items]  = read()
        
        Enum.each(items, fn digit -> IO.puts(mathematics(digit)) end)
    end
end

Solution.main()