I’ve been looking for some elementary ‘puzzles’ to code up in Erlang so I can figure out the language a bit. In passing, I came across Facebook’s “Programming Puzzles” page.
So today, the rest of the family went to a birthday party and I stayed home to solve the first puzzle and get my hands dirty with some Erlang.
Here is the resulting code….
-module(hoppity_hop).
-export([ handle_file/1 ]).
handle_file(Fname) ->
case file:read_file(Fname) of
{ok, FData} ->
parse(FData);
{error, Reason} ->
{error, Reason}
end.
parse(Bin) ->
do_hoppity_hop(lists:seq(1,list_to_integer(lists:nth(1,string:tokens(binary_to_list(Bin)," \t\r\n"))))).
do_hoppity_hop([Head|Tail]) when Head rem 5 == 0, Head rem 3 == 0 ->
io:format('Hop~n',[]),
do_hoppity_hop(Tail);
do_hoppity_hop([Head|Tail]) when Head rem 3 == 0 ->
io:format('Hoppity~n',[]),
do_hoppity_hop(Tail);
do_hoppity_hop([Head|Tail]) when Head rem 5 == 0 ->
io:format('Hophop~n',[]),
do_hoppity_hop(Tail);
do_hoppity_hop([Head|Tail]) ->
do_hoppity_hop(Tail);
do_hoppity_hop([]) -> false.
If anyone has any feedback, I’d love to hear it. This is the first time I sat down and actually wrote anything in Erlang, so I won’t be offended.
I should probably write a quick python version too, just so I know that skill set isn’t totally wasting away. ![]()