Hackerrank Day 8: Dictionaries and Maps

Task

Given names and phone numbers, assemble a phone book that maps friends’ names to their respective phone numbers. You will then be given an unknown number of names to query your phone book for. For each name queried, print the associated entry from your phone book on a new line in the form name=phoneNumber; if an entry for name is not found, print Not found instead.

Note: Your phone book should be a Dictionary/Map/HashMap data structure.

Input Format

The first line contains an integer, n, denoting the number of entries in the phone book.
Each of the n subsequent lines describes an entry in the form of 2 space-separated values on a single line. The first value is a friend’s name, and the second value is an 8-digit phone number.

After the n lines of phone book entries, there are an unknown number of lines of queries. Each line (query) contains a name to look up, and you must continue reading lines until there is no more input.

Note: Names consist of lowercase English alphabetic letters and are first names only.

Constraints
≤ n 10^5
≤ queries 10^5
Output Format
On a new line for each query, print Not found if the name has no corresponding entry in the phone book; otherwise, print the full name  and phoneNumber in the format name=phoneNumber.
Sample Input
3
sam
99912222
tom
11122222
harry
12299933
sam
edward
harry
Sample Output
sam=99912222
Not found
harry=12299933

My Solution: 

screen-shot-2016-12-15-at-1-25-02-pm

Retrieving user input is probably the biggest challenge when working on Hacker Rank problems.

Need to review STDIN and all that jazz.

Advertisements
Hackerrank Day 8: Dictionaries and Maps

Hackerrank Day 6: Let’s Review

Task

Given a string, S, of length N that is indexed from 0 to N1, print its even-indexed and odd-indexed characters as 2 space-separated strings on a single line (see the Sample below for more detail).

Input Format

The first line contains an integer, T (the number of test cases).
Each line ii of the T subsequent lines contain a String, S.

Constraints

  • ≤ 10
  • ≤ length of 10000

Sample Input

2
Hacker
Rank

Sample Output

Hce akr
Rn ak

Explanation

Test Case 0: S=“Hacker”
S[0]=“H”
S[1]=“a”
S[2]=“c”
S[3]=“k”
S[4]=“e”
S[5]=“r”
The even indices are 0, 2, and 4, and the odd indices are 1, 3, and 5. We then print a single line of 2 space-separated strings; the first string contains the ordered characters from S‘s even indices (Hce), and the second string contains the ordered characters from S‘s odd indices (akr).

Test Case 1: S=“Rank”
S[0]=“R”
S[1]=“a”
S[2]=“n”
S[3]=“k”
The even indices are 0 and 2, and the odd indices are 1 and 3. We then print a single line of 2 space-separated strings; the first string contains the ordered characters from S‘s even indices (Rn), and the second string contains the ordered characters from S‘s odd indices (ak).

My solution:  

So I had a hard time with this challenge because I misunderstood the problem.  I thought the T and S arguments were independent of each other.  However, they are not.  The value of S is dependent on the value of T.  So for each test case (T), there is one substring (S).  Thus, the trick is to use T to prompt the number of user inputs for S, and then store the value of S in an array.

Took me an entire day and 1 Kevin Liu to figure all of this out.

screen-shot-2016-12-13-at-4-04-17-pm

Original problem here.

Hackerrank Day 6: Let’s Review

Hackerrank Day 1: Data Types

Task
Complete the code in the editor below. The variables i, d, s and are already declared and initialized for you. You must:

  1. Declare 3 variables: one of type int, one of type double, and one of type String.
  2. Read 3  lines of input from stdin (according to the sequence given in the Input Format section below) and initialize your variables.
  3. Use the operator to perform the following operations:
    1. Print the sum of i plus your int variable on a new line.
    2. Print the sum of d plus your double variable to a scale of one decimal place on a new line.
    3. Concatenate s with the string you read as input and print the result on a new line.

Sample Input

12
4.0
is the best place to learn and practice coding!

Sample Output

16
8.0
HackerRank is the best place to learn and practice coding!

Explanation

When we sum the integers 4  and 12 , we get the integer 16.
When we sum the floating-point numbers 4.0 and 4.0, we get 8.0.
When we concatenate HackerRank with is the best place to learn and practice coding!, we get HackerRank is the best place to learn and practice coding!.

My solution in Ruby:

# Declare second integer, double, and String variables.

# Read and save an integer, double, and String to your variables.
i2 = gets.to_i
d2 = gets.to_f
s2 = gets.to_s

# Print the sum of both integer variables on a new line.
puts (i + i2)

# Print the sum of the double variables on a new line.
puts (d + d2)

# Concatenate and print the String variables on a new line
# The ‘s’ variable above should be printed first.
puts (s + s2)

Hackerrank Day 1: Data Types