This is a solution for the Day 8 hackerrank.com challenge. The basic idea of the challenge is to create a mapping of names to phone numbers based on the the given input, and later look up the phone numbers for certain names. If the entry exists, print it and its number. If it doesn't, print out "Not found"
Here's my solution:
n = gets.strip.to_i
phonebook = Hash.new
n.times do
name = gets.strip
number = gets.strip
phonebook[name] = number
end
n.times do
check_name = gets.strip
if phonebook.has_key?(check_name)
print "#{check_name}=#{phonebook[check_name]}"
puts ""
else
puts "Not found"
end
end
I was wondering about more efficient and elegant Ruby solution. I'm not sure how big sample input was for some of those test cases, but Testcase #1,2,3 take up to 0.4s which seems a little bit too high for me.