-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathdata_structures_exercises_python.py
More file actions
645 lines (525 loc) · 12.5 KB
/
Copy pathdata_structures_exercises_python.py
File metadata and controls
645 lines (525 loc) · 12.5 KB
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
"""
1. Write a Python program to create an Enum object and display a member name
and value.
Sample data :
Afghanistan = 93
Albania = 355
Algeria = 213
Andorra = 376
Angola = 244
Antarctica = 672
Expected Output :
Member name: Albania
Member value: 355
"""
from enum import Enum
class Country(Enum):
Afghanistan = 93
Albania = 355
Algeria = 213
Andorra = 376
Angola = 244
Antarctica = 672
##print(Country.Albania)
##print(repr(Country.Albania))
##print(type(Country.Albania))
##print(isinstance(Country.Albania, Country))
##print(Country['Albania'])
##print(Country['Antarctica'].value)
##for country in Country:
## print(country)
print('Member name:', Country.Albania.name)
print('Member value:', Country.Albania.value)
"""
2. Write a Python program to iterate over an enum class and display individual
member and their value.
Expected Output:
Afghanistan = 93
Albania = 355
Algeria = 213
Andorra = 376
Angola = 244
Antarctica = 672
"""
from enum import Enum
class Country(Enum):
Afghanistan = 93
Albania = 355
Algeria = 213
Andorra = 376
Angola = 244
Antarctica = 672
for country in Country:
print('Member name is {:<11}; member value is {}'.format(country.name, country.value))
"""
3. Write a Python program to display all the member name of an enum class
ordered by their values.
Expected Output:
Country Name ordered by Country Code:
Afghanistan
Algeria
Angola
Albania
Andorra
Antarctica
"""
# solution 1
from enum import Enum
class Country(Enum):
Afghanistan = 93
Albania = 355
Algeria = 213
Andorra = 376
Angola = 244
Antarctica = 672
print('Country Name ordered by Country Code:')
for country in sorted(Country, key=lambda x: x.value):
print(country.name)
# solution 2
from enum import IntEnum
class Country(IntEnum):
Afghanistan = 93
Albania = 355
Algeria = 213
Andorra = 376
Angola = 244
Antarctica = 672
print('Country Name ordered by Country Code:')
for country in sorted(Country):
print(country.name)
"""
4. Write a Python program to get all values from an enum class.
Expected output:
[93, 355, 213, 376, 244, 672]
"""
from enum import Enum
class Country(Enum):
Afghanistan = 93
Albania = 355
Algeria = 213
Andorra = 376
Angola = 244
Antarctica = 672
output = []
for country in Country:
output.append(country.value)
print(output)
# alternatively:
output = [country.value for country in Country]
print(output)
"""
5. Write a Python program to count the most common words in a dictionary.
Expected Output:
[('pink', 6), ('black', 5), ('white', 5), ('red', 4)]
"""
words = ['red', 'green', 'black', 'pink', 'black', 'white', 'black', 'eyes',
'white', 'black', 'orange', 'pink', 'pink', 'red', 'red', 'white',
'orange', 'white', 'black', 'pink', 'green', 'green', 'pink', 'green',
'white', 'orange', "orange", 'red']
# solution 1 withour collections module
counts = set()
for word in words:
counts.add((word, words.count(word)))
output = sorted(counts, key=lambda x: x[1], reverse=True)
print(output[:4])
# solution 2 with collections
from collections import Counter
print(Counter(words).most_common(4))
"""
6. Write a Python program to find the class wise roll number from a
tuple-of-tuples.
Sample data:
classes = (
('V', 1),
('VI', 1),
('V', 2),
('VI', 2),
('VI', 3),
('VII', 1),
)
Expected Output:
defaultdict(, {'VII': [1], 'V': [1, 2], 'VI': [1, 2, 3]})
"""
classes = (('V', 1),
('VI', 1),
('V', 2),
('VI', 2),
('VI', 3),
('VII', 1))
my_dict = {}
for a,b in classes:
my_dict.setdefault(a, []).append(b)
print(my_dict)
"""
7. Write a Python program to count the number of students of individual class.
Sample data:
classes = (
('V', 1),
('VI', 1),
('V', 2),
('VI', 2),
('VI', 3),
('VII', 1),
)
Expected Output:
Counter({'VI': 3, 'V': 2, 'VII': 1})
"""
classes = (('V', 1),
('VI', 1),
('V', 2),
('VI', 2),
('VI', 3),
('VII', 1))
# solution 1 (if numbers are sequentials and start with 1)
my_dict = {}
for a,b in classes:
if a in my_dict:
if b > my_dict[a]:
my_dict[a] = b
else:
my_dict[a] = b
print(my_dict)
# solution 2 (count occurencies of each class)
my_dict = {}
only_classes, roll_numbers = zip(*classes)
for a in only_classes:
my_dict[a]=only_classes.count(a)
print(my_dict)
"""
8.Write a Python program to get the unique enumeration values.
Expected Output:
Afghanistan = 93
Albania = 355
Algeria = 213
Andorra = 376
Angola = 244
"""
from enum import Enum, unique
@unique
class Country(Enum):
Afghanistan = 93
Albania = 355
Algeria = 213
Andorra = 376
Angola = 244
for c in Country:
print(c.name, c.value)
"""
9. Write a Python program to create an instance of an OrderedDict using a given
dictionary. Sort the dictionary during the creation and print the members of
the dictionary in reverse order.
Expected Output:
Angola 244
Andorra 376
Algeria 213
Afghanistan 93
Albania 355
In reverse order:
Albania 355
Afghanistan 93
Algeria 213
Andorra 376
Angola 244
"""
from collections import OrderedDict
my_dict = dict(Afghanistan = 93,
Albania = 355,
Algeria = 213,
Andorra = 376,
Angola = 244)
# OrderedDict sorted by values in ascending order
my_ordered_dict = OrderedDict(sorted(my_dict.items(), key = lambda x: x[1]))
for k,v in my_ordered_dict.items():
print(k, v)
# OrderedDict sorted by values in descending (reverse) order
print("In reverse order:")
reversed_dict = OrderedDict(sorted(my_dict.items(), key=lambda x: x[1], reverse=True))
for k,v in reversed_dict.items():
print(k, v)
# or alternatively reversing without creating additional OrderedDict
print("In reverse order:")
for k in reversed(my_ordered_dict):
print(k, my_ordered_dict[k])
"""
10. Write a Python program to group a sequence of key-value pairs into a
dictionary of lists.
Example:
[('v', 1), ('vi', 2), ('v', 3), ('vi', 4), ('vii', 1)]
Expected output:
[('v', [1, 3]), ('vi', [2, 4]), ('vii', [1])]
"""
classes = [('v', 1), ('vi', 2), ('v', 3), ('vi', 4), ('vii', 1)]
my_dict = {}
for k,v in classes:
my_dict.setdefault(k, []).append(v)
print(my_dict)
"""
11. Write a Python program to compare two unordered lists (not sets).
Expected Output: False
"""
from collections import Counter
list_a = [1, 2, 5, 3, 4, 1, 2, 3]
list_b = [1, 3, 5, 2, 4, 3, 1, 2]
print(Counter(list_a) == Counter(list_b))
"""
12. Write a Python program to create an array contains six integers.
Also print all the members of the array.
Expected Output:
10
20
30
40
50
"""
import array
my_arr = array.array('i', [1, 2, 3, 4, 5, 6])
print(my_arr)
for i in my_arr:
print(i)
"""
13. Write a Python program to get the array size of types unsigned integer
and float.
Expected Output:
4
4
"""
import array
my_arr = array.array('I', [4, 4])
print(my_arr.itemsize)
my_arr = array.array('f', [4, 4])
print(my_arr.itemsize)
"""
14. Write a Python program to get an array buffer information.
Expected Output:
Array buffer start address in memory and number of elements.
(25855056, 2)
"""
import array
my_arr = array.array('I', [4, 4, 4])
print(my_arr.buffer_info())
"""
15. Write a Python program to get the length of an array.
Expected Output:
Length of the array is:
5
"""
import array
my_arr = array.array('I', [4, 4, 4, 5])
# solution 1
print(my_arr.buffer_info()[1])
# solution 2
print(len(my_arr))
"""
16. Write a Python program to convert an array to an ordinary list with the
same items.
Expected Output:
Original array:
array('b', [1, 2, 3, 4])
Array to list:
[1, 2, 3, 4]
"""
import array
my_arr = array.array('b', [1, 2, 3, 4])
# solution 1
my_list = list(my_arr)
print(my_list, type(my_list))
# solution 2
my_list1 = my_arr.tolist()
print(my_list1, type(my_list1))
"""
17. Write a Python program to convert an array to an array of machine values
and return the bytes representation.
Expected Output:
Original array:
A1: array('i', [1, 2, 3, 4, 5, 6])
Array of bytes: b'010000000200000003000000040000000500000006000000'
"""
import array
import binascii
A1 = array.array('i', [1, 2, 3, 4, 5, 6])
print(binascii.b2a_hex(A1.tobytes()))
"""
18. Write a Python program to read a string and interpreting the string as
an array of machine values.
Expected Output:
array1: array('i', [7, 8, 9, 10])
Bytes: b'0700000008000000090000000a000000'
array2: array('i', [7, 8, 9, 10])
"""
import array
A1 = array.array('i', [1, 2, 3, 4, 5, 6])
A2 = array.array('i')
A2.frombytes(A1.tobytes())
print(A2)
"""
19. Write a Python program to push three items into the heap and print
the items from the heap.
Expected Output:
('V', 1)
('V', 2)
('V', 3)
"""
import heapq
my_heap = []
heapq.heappush(my_heap, ('V', 1))
heapq.heappush(my_heap, ('V', 2))
heapq.heappush(my_heap, ('V', 3))
for i in my_heap:
print(i)
"""
20. Write a Python program to push three items into a heap and return the
smallest item from the heap. Also Pop and return the smallest item from the heap.
Expected Output:
Items in the heap:
('V', 1)
('V', 3)
('V', 2)
----------------------
The smallest item in the heap:
('V', 1)
----------------------
Pop the smallest item in the heap:
('V', 2)
('V', 3)
"""
import heapq
my_heap = []
heapq.heappush(my_heap, ('V', 0))
heapq.heappush(my_heap, ('V', 1))
heapq.heappush(my_heap, ('V', 1))
print("Items in the heap")
for i in my_heap:
print(i)
print("The smallest item in the heap:")
print(*heapq.nsmallest(1, my_heap, key=lambda x: x[1]))
# also like this:
print(my_heap[0])
print("The smallest item in the heap:")
print(heapq.heappop(my_heap))
"""
21. Write a Python program to push an item on the heap, then pop and return
the smallest item from the heap.
Expected Output:
Items in the heap:
('V', 1)
('V', 3)
('V', 2)
----------------------
Using heappushpop push item on the heap and return the smallest item.
('V', 2)
('V', 3)
('V', 6)
"""
import heapq
my_heap = [('V', 1), ('V', 1)]
print(heapq.heappushpop(my_heap, ('V', 1)))
print(heapq.heappushpop(my_heap, ('V', 2)))
print(heapq.heappushpop(my_heap, ('V', 3)))
"""
22. Write a Python program to create a heapsort, pushing all values onto
a heap and then popping off the smallest values one at a time.
Expected Output:
[10, 20, 20, 40, 50, 50, 60, 70, 80, 90, 100]
"""
import heapq
my_unsorted_list = [20, 10, 50, 70, 30, 10, 40, 80, 20, 60]
my_sorted_list = []
my_heap = []
for item in my_unsorted_list:
heapq.heappush(my_heap, item)
for x in range(len(my_heap)):
my_sorted_list.append(heapq.heappop(my_heap))
print(my_sorted_list)
"""
23. Write a Python program to get the two largest and three smallest items
from a dataset.
Expected Output:
[100, 90]
[10, 20, 20]
"""
import heapq
my_list = [20, 10, 50, 70, 30, 10, 40, 80, 20, 60]
print(heapq.nlargest(2, my_list))
print(heapq.nsmallest(3, my_list))
"""
24. Write a Python program to locate the left insertion point for a specified
value in sorted order.
"""
import bisect
my_list = [10, 10, 20, 20, 30, 50, 40, 80, 70, 60]
print(bisect.bisect_left(my_list, 25))
"""
25. Write a Python program to locate the right insertion point for a specified
value in sorted order.
"""
import bisect
my_list = [10, 10, 20, 20, 30, 50, 40, 80, 70, 60]
print(bisect.bisect_right(my_list, 20))
"""
26. Write a Python program to insert items into a list in sorted order.
Expected Output:
Original List:
[25, 45, 36, 47, 69, 48, 68, 78, 14, 36]
Sorted List:
[14, 25, 36, 36, 45, 47, 48, 68, 69, 78]
"""
import bisect
my_list = [25, 45, 36, 47, 69, 48, 68, 78, 14, 36]
my_sorted_list = []
for i in my_list:
bisect.insort_left(my_sorted_list, i)
print(my_sorted_list)
"""
27. Write a Python program to create a queue and display all the members and
size of the queue.
Expected Output:
Members of the queue:
0 1 2 3
Size of the queue:
4
"""
import queue
q = queue.Queue()
for i in range(4):
q.put(i)
print("Members of the queue:")
for elem in list(q.queue):
print(elem, end=' ')
print("\nSize of the queue:")
print(q.qsize())
"""
28. Write a Python program to find whether a queue is empty or not. Go to the editor
Expected Output:
True
False
"""
import queue
q = queue.Queue()
if q.empty():
print(True)
else:
print(False)
"""
29. Write a Python program to create a FIFO queue.
Expected Output:
0 1 2 3
"""
import queue
q = queue.Queue()
for i in range(4):
q.put(i)
while not q.empty():
print(q.get(), end=' ')
print('\n')
"""
30. Write a Python program to create a LIFO queue.
Expected Output:
3 2 1 0
"""
import queue
p = queue.LifoQueue()
for i in range(4):
p.put(i)
while not p.empty():
print(p.get(), end=' ')
print('\n')