Skip to main content

Featured

say hello world with C++ - Solution in Hacker Rank - hackerranksolutions8

  Objective This is a simple challenge to help you practice printing to  stdout . You may also want to complete  Solve Me First  in C++ before attempting this challenge. We’re starting out by printing the most famous computing phrase of all time! In the editor below, use either  printf  or  cout  to print the string  Hello ,World!  to  stdout . The more popular command form is  cout . It has the following basic form: cout<<value_to_print<<value_to_print; Any number of values can be printed using one command as shown. The  printf  command comes from C language. It accepts an optional format specification and a list of variables. Two examples for printing a string are: printf("%s", string);   printf(string); Note that neither method adds a newline. It only prints what you tell it to. Output Format Print   Hello ,World!   to stdout. Sample Output Hello, World! Solution:- //Say Hello, ...

Java Map HackerRank Solution - Good Practices

 Problem:-

You are given a phone book that consists of people’s names and their phone number. After that you will be given some person’s name as query. For each query, print the phone number of that person.

Input Format

The first line will have an integer n denoting the number of entries in the phone book. Each entry consists of two lines: a name and the corresponding phone number.

After these, there will be some queries. Each query will contain a person’s name. Read the queries until end-of-file.

Constraints:
A person’s name consists of only lower-case English letters and it may be in the format ‘first-name last-name’ or in the format ‘first-name’. Each phone number has exactly 8 digits without any leading zeros.

< n < 100000

< Query < 100000

Output Format

For each case, print “Not found” if the person has no entry in the phone book. Otherwise, print the person’s name and phone number. See sample output for the exact format.

To make the problem easier, we provided a portion of the code in the editor. You can either complete that code or write completely on your own.

Sample Input

3
uncle sam
99912222
tom
11122222
harry
12299933
uncle sam
uncle tom
harry

Sample Output

uncle sam=99912222
Not found
harry=12299933

Solution:-

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
      		try {
			int no_of_entries = 0;
			int i = 0;
			String name = null;
			int number = 0;
			String query = null;
			HashMap<String, Integer> phoneBook = new HashMap<String, Integer>();
			BufferedReader b = new BufferedReader(new InputStreamReader(
					System.in));
			no_of_entries = Integer.parseInt(b.readLine());			
			while (i < no_of_entries) {
				name = b.readLine();
				number = Integer.parseInt(b.readLine());
				phoneBook.put(name, number);
				i++;
			}
			while (!(query = b.readLine().trim()).isEmpty()) {
				if (phoneBook.containsKey(query))
					System.out.println(query + "=" + phoneBook.get(query));
				else
					System.out.println("Not found");
			}
		} catch (Exception e) {
			
		}
    }
}

Comments