Created
October 29, 2016 03:05
-
-
Save CaseyLeask/91caa7919efffe38900aee455d497b34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Enter your code here. Read input from STDIN. Print output to STDOUT | |
% Your class should be named solution | |
-module(solution). | |
-export([main/0]). | |
main() -> | |
runQueries(createPhoneBook(readAll())). | |
readAll() -> | |
readAll([], io:get_chars("", 4096)). | |
readAll(In, eof) -> | |
[StringN|Lines] = string:tokens(lists:flatten(lists:reverse(In)), "\n"), | |
{list_to_integer(StringN), Lines}; | |
readAll(In, Data) -> | |
readAll([Data|In], io:get_chars("", 4096)). | |
createPhoneBook({N, Queries}) -> | |
createPhoneBook({N, Queries}, #{}). | |
createPhoneBook({0, Queries}, PhoneBook) -> | |
{Queries, PhoneBook}; | |
createPhoneBook({N, [Entry|Lines]}, PhoneBook) -> | |
[Key, Value] = string:tokens(Entry, " "), | |
createPhoneBook({N-1, Lines}, maps:put(Key, Key ++ "=" ++ Value ++ "\n", PhoneBook)). | |
runQueries({[], _PhoneBook}) -> | |
ok; | |
runQueries({[Key|Queries], PhoneBook}) -> | |
io:put_chars(maps:get(Key, PhoneBook, "Not found\n")), | |
runQueries({Queries, PhoneBook}). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment