Skip to content

Instantly share code, notes, and snippets.

@shadow-prince
Created July 25, 2021 07:45
Show Gist options
  • Save shadow-prince/ee24a17ffd622355360865a1eda7f872 to your computer and use it in GitHub Desktop.
Save shadow-prince/ee24a17ffd622355360865a1eda7f872 to your computer and use it in GitHub Desktop.
Day 8 Of 30 Days of Code - HackerRank - Python & Java
/*
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 queried, print the associated entry from your phone book on a
new line in the form name=phoneNumber; if an entry for is not found, print Not found instead.
Java
*/
import java.util.*;
import java.io.*;
class Solution{
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Map <String,Integer> myMap= new HashMap<String,Integer>();
for(int i = 0; i < n; i++){
String name = in.next();
int phone = in.nextInt();
myMap.put(name,phone);
}
while(in.hasNext()){
String name = in.next();
if (myMap.containsKey(name)) {
int phone = myMap.get(name);
System.out.println(name + "=" + phone);
} else{
System.out.println("Not found");
}
}
in.close();
}
}
'''
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 queried, print the associated entry from your phone book on a
new line in the form name=phoneNumber; if an entry for is not found, print Not found instead.
Python
'''
phBook ={}
t = int(input())
for i in range(t):
x,y = input().split(" ")
phBook[x] = y
ls = []
for k in range(t):
ls.append(input())
for j in ls:
if(phBook.get(j)!=None):
print(j+'='+phBook.get(j))
else:
print("Not Found")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment