-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDictionary-Questions.py
350 lines (327 loc) · 9.64 KB
/
Dictionary-Questions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# Create dictionary with roll number, name and marks of 5 subjects
d={1:19,2:'Jonathan',3:[99,99,99,99,99]}
print(d[2])
# Input the names of students and create dictionary with names as key and value 2500
n=int(input("How many students? "))
t=()
for i in range(n):
e=eval(input("Student name: "))
t+=(e,)
d=dict.fromkeys(t,2500)
print(d)
car={'brand':'Ford','model':'Mustang','year':1964}
# Print keys
x=car.keys()
print(x)
# Add another key and value
car['color']='white'
print(x)
# Print items in dictionary
print(car.items())
# Traverse a dictionary
car={'brand':'Ford','model':'Mustang','year':1964}
for i in car:
print(str(i),':',str(car[i]))
# create a dictionary with roll number, name and computer mark
r=eval(input("Enter roll number: "))
n=eval(input("Enter name: "))
m=eval("Enter Computer marks: "))
d={}
d["Roll Number"]=r
d["Name"]=n
d["CS Mark"]=m
print('Dictionary:',d)
# Create dictionary with n key:value pairs
n=eval(input('Enter number of items: '))
d={}
for i in range(n):
k=eval(input('Enter key: '))
v=eval(input('Enter value: '))
d[k]=v
print('Dictionary:',d)
# Search for value using key in an inputted dictionary
n=eval(input("Enter number of items: "))
d={}
for i in range(n):
k=eval(input("Enter key: "))
v=eval(input("Enter value: "))
d[k]=v
kf=eval(input("Enter value to find: "))
print(d[kf])
# Input sentence and print count of each word
s=eval(input("The sentence: "))
sl=s.split()
d={}
for i in sl:
d[i]=sl.count(i)
print(d)
# Highest mark of each student
n=int(input("How many students? "))
d={}
for i in range(n):
name=eval(input("Input name: "))
m1=eval(input("Mark 1: "))
m2=eval(input("Mark 2: "))
m3=eval(input("Mark 3: "))
m4=eval(input("Mark 4: "))
m5=eval(input("Mark 5: "))
d[name]=[m1,m2,m3,m4,m5]
for i in d:
print(str(i)+"'s highest mark is "+str(max(d[i])))
# Create a dictionary with n train numbers as key and value is a list of all stops. Print the train numbers which has a stop at Chennai.
n=int(input("How many trains? "))
d={}
for i in range(n):
tn=int(input("Input the train number: "))
ns=int(input("Number of stops: "))
sl=[]
for j in range(ns):
stop=input("What is stop "+str(j+1)+"? ")
sl.append(stop)
d[tn]=sl
for i in d:
if "Chennai" in d[i] or "chennai" in d[i] or "CHENNAI" in d[i]:
print("Train",i,"passes through Chennai")
else:
pass
# Create a dictionary with n items name as key and value will be a tuple with cp and sp and print the item name with maximum sp and minimum sp
n=int(input("How many items? "))
d={}
for i in range(n):
item=input("Input name of item: ")
cp=eval(input("Cost Price: "))
sp=eval(input("Selling Price: "))
d[item]=(cp,sp)
high=0
highn=""
for j in d:
if d[j][1]>high:
high=d[j][1]
highn=j
print("Max selling price is:",high)
low=d[item][1]
lown=""
for k in d:
if d[k][1]<low:
low=d[k][1]
lown=k
print("Min selling price is:",low)
# Create a dictionary with n students name as key and value is a list of 5 marks. Change the dictionary such that the value will be a list in ascending order
Ds={}
n=int(input("How many students? "))
for i in range(n):
name=input("The name of student: ")
m1=int(input("Marks of Subject 1: "))
m2=int(input("Marks of Subject 2: "))
m3=int(input("Marks of Subject 3: "))
m4=int(input("Marks of Subject 4: "))
m5=int(input("Marks of Subject 5: "))
Ds[name]=sorted([m1,m2,m3,m4,m5])
print(Ds)
# Create a dictionary with exam number as key and value is another dictionary with name, class and gender and print key and values if class is 11.
d={}
for i in range(n):
examno=int(input("Exam number: "))
name=input("Name: ")
clas=int(input("Class: "))
gender=input("Gender: ")
d[examno]={"Name":name,"Class":clas,"Gender":gender}
for j in d:
if d[j]["Class"]==11:
print(str(j)+":"+str(d[j]))
# Create a dictionary with key as exam no and value is a dictionary with name, mark which is a list of 5 marks. Print the roll no and name of the student getting rank 1
Ds={}
n=int(input("How many students? "))
for i in range(n):
examno=int(input("The exam number of student "+str(i+1)+": "))
name=input("The name of student "+str(examno)+" : ")
m1=int(input("Marks of Sbuject 1: "))
m2=int(input("Marks of Sbuject 2: "))
m3=int(input("Marks of Sbuject 3: "))
m4=int(input("Marks of Sbuject 4: "))
m5=int(input("Marks of Sbuject 5: "))
Ds[examno]=[name,[m1,m2,m3,m4,m5]]
print(Ds)
def avg(l):
av=sum(l)/len(l)
return av
high=0
highn=""
for k in Ds:
if avg(Ds[k][1])>high:
high=avg(Ds[k][1])
highn=Ds[k][0]
else:
pass
print()
print("Highest mark is",high,"held by",highn)
# Voting details should be available in a dictionary in the format key will be roll number and value the name of student voted for and declare the winner.
n = int(input("The number of students: "))
print("Vote for student a or b or c: ")
a,b,c=0,0,0
for i in range(n):
vote = input("The vote of student "+str(i+1)+": ")
if vote == "a" or vote == "A":
a+=1
elif vote =="b" or vote == "B":
b+=1
elif vote == "c" or vote == "C":
c+=1
else:
print("Enter proper vote")
print({"a":a,"b":b,"c":c})
maxv = list({"a":a,"b":b,"c":c}.values())
if max(maxv)==a:
print("a has the highest number of votes")
elif max(maxv)==b:
print("b has the highest number of votes")
else:
print("c has the highest number of votes")
# Create a dictionary with key as name and value is phone number print all your friends’ names who stay in Sharjah assuming the first numbers are 06.
n = int(input("Number of students: "))
d = {}
for i in range(n):
name = input("Name of student: ")
phone = input("Phone number: ")
d[name] = phone
for j in d:
if d[j][:2]=="06":
print(j,"lives in sharjah")
# Create a dictionary with e-code as key and value another dictionary with name, date of birth print e-code and names of employees whose birthday falls in a given month.
employees = {}
n = int(input("Number of employees: "))
for i in range(n):
ecode = int(input("What is the ecode? "))
name = input("Name of employee: ")
year = int(input("Year or birth: "))
mnth = int(input("Month of birth: "))
day = int(input("Date of birth: "))
employees[ecode]={"Name":name,"Date of Birth":{"Day":day,"Month":mnth,"Year":year}}
print(employees)
month = int(input("Which month to print? "))
for i in employees:
if employees[i]["Date of Birth"]["Month"]==month:
print("Ecode:",i,"Name:",employees[i]["Name"])
# Count the number of times a character occurs in an inputted string
str = input("String: ")
d = {}
for i in str:
count = str.count(i)
d[i]=count
print(d)
# Create a dictionary with key as team name and value is a list of number of wins and number of losses
n = int(input("Number of teams: "))
d = {}
for i in range(n):
name = input("Name: ")
wins = int(input("Number of wins: "))
loss = int(input("Number of losses: "))
d[name] = [wins,loss]
for j in d:
if d[j][0]>d[j][1]:
print(j,"has more wins than losses")
else:
pass
# Print the names of those students who are eligible for admission
n = int(input("Number of students: "))
d = {}
for i in range(n):
regno = int(input("Registration number: "))
name = input("Name: ")
year = int(input("Year or birth: "))
mnth = int(input("Month of birth: "))
day = int(input("Date of birth: "))
marks = int(input("Total Marks: "))
d[regno]={"Name":name,"DOB":[day,mnth,year],"Total Marks":marks}
for j in d:
if (d[j]["Total Marks"]>50) and d[j]["DOB"][2]==2016:
print(d[j]["Name"],"is elligible")
else:
pass
# Perform functions on a dictionary
ODD = {3:"Three",5:"Five",7:"Seven",9:"Nine"}
print("a) ",end="")
k = list(ODD.keys())
for i in k:
print(i,end=" ")
print()
print("b) ",end="")
v = list(ODD.values())
for i in v:
print(i,end=" ")
print()
print("c)",ODD)
print("d) The length of the dictionary:",len(ODD))
print("e) ",end="")
if 7 in ODD:
print("7 is present")
else:
print("7 is not present")
print("f) ",end="")
if 2 in ODD:
print("2 is present")
else:
print("2 is not present")
print("g)",ODD[9])
del(ODD[9])
print("h)",ODD)
# Enter names of employees and their salaries as input and store them
n = int(input("How many employees? "))
d = {}
for i in range(n):
name = input("What is the name of the employee? ")
salary = int(input("What is "+name+"'s salary? "))
d[name] = salary
print(d)
# Find the highest 2 values in a dictionary
n = int(input("Number of items: "))
d = {}
for i in range(n):
key = input("The key: ")
value = eval(input("The value: "))
d[key] = value
x = list(d.values())
x.sort()
print(x[-1])
print(x[-2])
# Perform functions on a phonebook
n = int(input("Number of friends: "))
pbk = {}
for i in range(n):
name = input("Name of friend: ")
phone = int(input("Phone number of "+name+": "))
pbk[name]=phone
print()
print("a)",pbk)
print()
print("b)",end=" ")
namen = input("New name to add: ")
phonen = int(input("Phone number of "+namen+": "))
pbk[namen]=phonen
print(pbk)
print()
print("c)",end=" ")
named = input("Friend to delete: ")
del(pbk[named])
print(pbk)
print()
print("d)",end=" ")
namec = input("Which person's number to change: ")
phonec = int(input("Changed phone number: "))
pbk[namec] = phonec
print(pbk)
print()
print("e)",end=" ")
namech = input("Which person to check in the dictionary: ")
if namech in pbk:
print(namech,"is in the phonebook")
else:
print(namech,"is not in the phonebook")
print()
print("f)",end=" ")
x = list(pbk.keys())
x.sort()
pbk2 = {}
for i in x:
pbk2[i]=pbk[i]
print(pbk2)