A dictionary is used to map or associate things you want to store with a key and a key is required to get values associated with that particular key. A dictionary in Python is just like a dictionary in the real world. Python Dictionary is defined by two elements Keys and Values.

  • Keys will be a single element.
  • Values can be a list or list within a list, numbers, etc.

To learn more about dictionary visit here.

Objective
Today, we’re learning about Key-Value pair mappings using a Map or Dictionary data structure. Check out the Tutorial tab for learning materials and an instructional video!

Task
Given n 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 phonebook 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

  • 1<= n <=10000
  • 1<= queries <=10000

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 phone number 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

Solution:

First, we read input integer n, which is the number of further input we have to read and assign to the dictionary. Then we create a phone book dictionary which as the name suggests will contain names and phone number.

Next, we apply a for loop to read the subsequent input and assign their values to the dictionary phone_book. 

Try and except is used to read the indefinite number of input.  Inside try we have applied a while loop and append the input to list name.

Then a for loop is applied to search the name from phone_book and output the number if the name exists in the phone_book else outputs ‘Not Found’.

Code :

n = int(input().strip())
phone_book={}
for i in range(n):
    x= input().strip()
    listx = list(x.split(' '))
    phone_book[listx[0]] = listx[1]
name=[]
try:
    while True:
        inp = input().strip()
        if inp != "":
            name.append(inp)
        else:
            break
except EOFError:
    pass
for i in name:
    c=0
    if i in phone_book:
 
        print(i+'='+phone_book[i])
 
    else:
        print('Not found')

Leave a Reply

Your email address will not be published.