Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

This question is really similar to an existing one existing one in Ruby. So the task is also the same: Day 8: Dictionaries and Maps!

The problem is that on Hackerrank my solution only passes the first and last testcases. All the other ones between them are timing out (>12s!). I tried to use map instead of dict and also read the queries first into a list and process them separately as was suggested in the discussion. I have a feeling it goes to infinite recursion because I'm doing the line read/strip wrong.

-module(solution).
-export([main/0]).
 
main() ->
 { ok, [N]} = io:fread("", "~d"),
 Dict = read_dictionary(N, dict:new()),
 queries(Dict).
 
read_dictionary(0, D) -> D;
read_dictionary(N, D) ->
 Name = stripped_next_line(),
 Number = stripped_next_line(),
 read_dictionary(N-1, dict:store(Name, Number, D)).
 
queries(Dict) ->
 queries(Dict, io:get_line("")).
queries(_, eof) -> true;
queries(Dict, Name) ->
 find_number_of(strip_line(Name), Dict),
 Next = io:get_line(""),
 queries(Dict, Next).
 
find_number_of(Name, Dict) ->
 out(dict:find(Name, Dict), Name).
 
out({ok, Number}, Name) -> io:format("~s=~s~n", [Name, Number]);
out(error, _) -> io:format("Not found~n").
 
strip_line(Line) -> string:strip(Line, both, $\n).
stripped_next_line() -> strip_line(io:get_line("")).

This question is really similar to an existing one in Ruby. So the task is also the same: Day 8: Dictionaries and Maps!

The problem is that on Hackerrank my solution only passes the first and last testcases. All the other ones between them are timing out (>12s!). I tried to use map instead of dict and also read the queries first into a list and process them separately as was suggested in the discussion. I have a feeling it goes to infinite recursion because I'm doing the line read/strip wrong.

-module(solution).
-export([main/0]).
 
main() ->
 { ok, [N]} = io:fread("", "~d"),
 Dict = read_dictionary(N, dict:new()),
 queries(Dict).
 
read_dictionary(0, D) -> D;
read_dictionary(N, D) ->
 Name = stripped_next_line(),
 Number = stripped_next_line(),
 read_dictionary(N-1, dict:store(Name, Number, D)).
 
queries(Dict) ->
 queries(Dict, io:get_line("")).
queries(_, eof) -> true;
queries(Dict, Name) ->
 find_number_of(strip_line(Name), Dict),
 Next = io:get_line(""),
 queries(Dict, Next).
 
find_number_of(Name, Dict) ->
 out(dict:find(Name, Dict), Name).
 
out({ok, Number}, Name) -> io:format("~s=~s~n", [Name, Number]);
out(error, _) -> io:format("Not found~n").
 
strip_line(Line) -> string:strip(Line, both, $\n).
stripped_next_line() -> strip_line(io:get_line("")).

This question is really similar to an existing one in Ruby. So the task is also the same: Day 8: Dictionaries and Maps!

The problem is that on Hackerrank my solution only passes the first and last testcases. All the other ones between them are timing out (>12s!). I tried to use map instead of dict and also read the queries first into a list and process them separately as was suggested in the discussion. I have a feeling it goes to infinite recursion because I'm doing the line read/strip wrong.

-module(solution).
-export([main/0]).
 
main() ->
 { ok, [N]} = io:fread("", "~d"),
 Dict = read_dictionary(N, dict:new()),
 queries(Dict).
 
read_dictionary(0, D) -> D;
read_dictionary(N, D) ->
 Name = stripped_next_line(),
 Number = stripped_next_line(),
 read_dictionary(N-1, dict:store(Name, Number, D)).
 
queries(Dict) ->
 queries(Dict, io:get_line("")).
queries(_, eof) -> true;
queries(Dict, Name) ->
 find_number_of(strip_line(Name), Dict),
 Next = io:get_line(""),
 queries(Dict, Next).
 
find_number_of(Name, Dict) ->
 out(dict:find(Name, Dict), Name).
 
out({ok, Number}, Name) -> io:format("~s=~s~n", [Name, Number]);
out(error, _) -> io:format("Not found~n").
 
strip_line(Line) -> string:strip(Line, both, $\n).
stripped_next_line() -> strip_line(io:get_line("")).
edited tags
Link
200_success
  • 145.5k
  • 22
  • 190
  • 479
Source Link
vbalazs
  • 133
  • 3

Hackerrank challenge - Dictionaries and Maps in Erlang

This question is really similar to an existing one in Ruby. So the task is also the same: Day 8: Dictionaries and Maps!

The problem is that on Hackerrank my solution only passes the first and last testcases. All the other ones between them are timing out (>12s!). I tried to use map instead of dict and also read the queries first into a list and process them separately as was suggested in the discussion. I have a feeling it goes to infinite recursion because I'm doing the line read/strip wrong.

-module(solution).
-export([main/0]).
 
main() ->
 { ok, [N]} = io:fread("", "~d"),
 Dict = read_dictionary(N, dict:new()),
 queries(Dict).
 
read_dictionary(0, D) -> D;
read_dictionary(N, D) ->
 Name = stripped_next_line(),
 Number = stripped_next_line(),
 read_dictionary(N-1, dict:store(Name, Number, D)).
 
queries(Dict) ->
 queries(Dict, io:get_line("")).
queries(_, eof) -> true;
queries(Dict, Name) ->
 find_number_of(strip_line(Name), Dict),
 Next = io:get_line(""),
 queries(Dict, Next).
 
find_number_of(Name, Dict) ->
 out(dict:find(Name, Dict), Name).
 
out({ok, Number}, Name) -> io:format("~s=~s~n", [Name, Number]);
out(error, _) -> io:format("Not found~n").
 
strip_line(Line) -> string:strip(Line, both, $\n).
stripped_next_line() -> strip_line(io:get_line("")).
default

AltStyle によって変換されたページ (->オリジナル) /