Skip to content

Commit def38ed

Browse files
authored
Create Day8-Dictionaries_and_Maps.c
1 parent 4c358cb commit def38ed

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* PROBLEM STATEMENT
2+
Given n names and phone numbers, assemble a phone book that maps friends' names to their respective phone numbers.
3+
You will then be given an unknown number of names to query your phone book for.
4+
For each name queried, print the associated entry from your phone book on a new line in the form name=phoneNumber;
5+
if an entry for name is not found, print Not found instead.
6+
7+
Note: Your phone book should be a Dictionary/Map/HashMap data structure.
8+
9+
Input Format
10+
11+
The first line contains an integer,n , denoting the number of entries in the phone book.
12+
Each of the n subsequent lines describes an entry in the form of 2 space-separated values on a single line.
13+
The first value is a friend's name, and the second value is an 8-digit phone number.
14+
15+
After the n lines of phone book entries, there are an unknown number of lines of queries.
16+
Each line (query) contains a name to look up, and you must continue reading lines until there is no more input.
17+
18+
Note: Names consist of lowercase English alphabetic letters and are first names only.
19+
20+
Output Format
21+
22+
On a new line for each query, print Not found if the name has no corresponding entry in the phone book; otherwise,
23+
print the full name and phone number in the format name=phoneNumber.
24+
25+
Sample Input
26+
27+
3
28+
sam 99912222
29+
tom 11122222
30+
harry 12299933
31+
sam
32+
edward
33+
harry
34+
35+
Sample Output
36+
37+
sam=99912222
38+
Not found
39+
harry=12299933
40+
Explanation
41+
42+
We add the following n=3 (Key,Value) pairs to our map so it looks like this:
43+
phonebook={ (sam ,99912222),(tom, 11122222),(harry, 12299933)}
44+
45+
We then process each query and print key=value if the queried is found in the map; otherwise, we print Not found.
46+
47+
Query 0: sam
48+
Sam is one of the keys in our dictionary, so we print sam=99912222.
49+
50+
Query 1: edward
51+
Edward is not one of the keys in our dictionary, so we print Not found.
52+
53+
Query 2: harry
54+
Harry is one of the keys in our dictionary, so we print harry=12299933. */
55+
56+
57+
#include <cmath>
58+
#include <cstdio>
59+
#include <vector>
60+
#include <iostream>
61+
#include <algorithm>
62+
#include <map>
63+
using namespace std;
64+
65+
66+
int main() {
67+
68+
int n;
69+
string name;
70+
long num;
71+
cin >> n;
72+
cin.ignore();
73+
map <string, long> pBook;
74+
for (int i = 0; i < n; i++) {
75+
cin >> name;
76+
cin >> num;
77+
pBook[name] = num;
78+
}
79+
while(cin >> name) {
80+
if (pBook.find(name) != pBook.end())
81+
//pBook.end means that it has reached to the last element of pBook.
82+
//so if we use != , it means that it has not reached to the end. Thus it means that the name is found somewhere in the middle of pBook.
83+
{
84+
85+
cout << name << "=" << pBook.find(name)->second << endl;
86+
//map.find() iterates through your map structure. if it finds the element then it returns an iterator to that element else it returns end.
87+
//End being an element past the last element of that container, essentially telling you it wasn't found.
88+
//Remember containers in c++: arrays, vectors, maps, queues, stacks, etc. start at 0.
89+
//So if you have a map container with 10 elements and an index of 10 is returned this is outside the bounds of the container so in this function end is returned.
90+
91+
} else {
92+
cout << "Not found" << endl;
93+
}
94+
}
95+
return 0;
96+
}
97+
98+
// Time Comlexity: O(n)
99+
// Space Complexity: O(1)

0 commit comments

Comments
 (0)