HiveBrain v1.2.0
Get Started
← Back to all entries
patternrubyMinor

Hackerrank challenge - Dictionaries and Maps

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
challengemapsandhackerrankdictionaries

Problem

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.

Solution

You got it (mostly) right, I'd say. I wouldn't worry about runtime on hackerrank. They're not going to throw massive resources at every piece of code someone uploads. You're basically sharing their server with everyone else, so they're throttling things. Plus, there's just basic network lag.

Still, there's a bug:

N is the number of phonebook entries to read, but after that you should read to the end of input. But your code just reads N times in both cases.

Other things:

-
The Ruby convention is 2 spaces of indentation. Not 4 spaces, not tabs.

-
Don't use print and then puts with an empty line; just use puts.

-
Don't use strip unless you want exactly what it does. Most often, you'll see gets.chomp for input handling. #chomp only strips the trailing linebreak, but leaves other whitespace intact.

And you can shorten the if..else to a ternary:

N = gets.chomp.to_i # make this a constant, because why not
phonebook = {}      # shorter way to write Hash.new

N.times do
  name = gets.chomp
  number = gets.chomp
  phonebook[name] = number
end

until STDIN.eof? do # read to end-of-file (i.e. end of input)
  name = gets.chomp
  number = phonebook[name]
  puts number ? "#{name}=#{number}" : "Not found"
end


However, you could also do something else:

N = gets.chomp.to_i
phonebook = Hash.new { "Not found" } # set a default value for undefined keys

N.times do
  name = gets.chomp
  number = gets.chomp
  phonebook[name] = "#{name}=#{number}" # set the entire output string
end

# Will print the name=number string or "Not found"
puts phonebook[gets.chomp] until STDIN.eof?

Code Snippets

N = gets.chomp.to_i # make this a constant, because why not
phonebook = {}      # shorter way to write Hash.new

N.times do
  name = gets.chomp
  number = gets.chomp
  phonebook[name] = number
end

until STDIN.eof? do # read to end-of-file (i.e. end of input)
  name = gets.chomp
  number = phonebook[name]
  puts number ? "#{name}=#{number}" : "Not found"
end
N = gets.chomp.to_i
phonebook = Hash.new { "Not found" } # set a default value for undefined keys

N.times do
  name = gets.chomp
  number = gets.chomp
  phonebook[name] = "#{name}=#{number}" # set the entire output string
end

# Will print the name=number string or "Not found"
puts phonebook[gets.chomp] until STDIN.eof?

Context

StackExchange Code Review Q#116236, answer score: 4

Revisions (0)

No revisions yet.