Hacker Rank And Elixir

So I was just working through some of the materials over at HackerRank and I can’t quite figure out what they’re doing. I mean they give you a template

defmodule Solution do end 

And you have to write the code inline in the module, like this:

defmodule Solution do IO.puts("Hello World") end 

I can’t figure out what it’s doing to get that code to work. It’s not legal Elixir syntax (or is it?) so what’s the trick? I’m not trying to learn anything from HR because I’d like to think I have a good grasp of Elixir already. This strikes me as a poor resource for anyone trying to learn Elixir though.

Well it is legal, but it will be executed during compilation, not something you can easily assert.

They don’t give you the name of the function and which parameters it should have? It is in neither the tests provided? (I don’t remember if they provide some).

If you like to learn solving these puzzles I prefer Codewars. If not a great resource to hands on learning is Exercism

2 Likes

This is how you have to write code in hackerrank

defmodule Solution do #Enter your code here. Read input from STDIN. Print output to STDOUT def print_n() do n = IO.gets("") |> String.trim |> String.to_integer Enum.each(1..n, fn _n -> IO.puts("Hello World") end) end end Solution.print_n() 

this code is for “Hello World” challenge

Again I submitted a Hackerrank challenge called “Reverse a list”
submitted code is here:

defmodule Solution do def receive do IO.read(:all) |> String.trim |> String.split("\n") |> rev() |> Enum.map(& IO.inspect(String.to_integer(&1))) end def rev(list), do: rev(list, []) def rev([], acc), do: acc def rev([h | t], acc), do: rev(t, [h | acc]) end Solution.receive()