forked from HU-CS201-master/chap01
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bag.py
103 lines (88 loc) · 2.25 KB
/
bag.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
from uset import USet
# Adapted from Exercise 1.5 in the book.
class Bag:
def __init__(self):
'''Initializes member variables.
>>> bag = Bag()
'''
self.uset = USet() # this is the underlying data structure.
def __str__(self):
'''Returns a string representation of an object for printing.
>>> bag = Bag()
>>> print(bag) # prints bag.__str__()
'''
pass
def add(self, key, val):
'''Adds the pair (key, val) to the Bag.
>>> bag = Bag()
>>> bag.add(1, 10)
>>> bag.size()
1
>>> bag.add(1, 10)
>>> bag.size()
2
>>> bag.add(1, 20)
>>> bag.size()
3
>>> bag.add(2, 20)
>>> bag.size()
4
'''
pass
def remove(self, key):
'''Removes a pair with key from the Bag and returns it.
Returns None if no such pair exsits.
>>> bag = Bag()
>>> bag.add(1, 10)
>>> bag.add(1, 10)
>>> bag.add(1, 20)
>>> bag.add(2, 20)
>>> bag.remove(1)
(1,10) # could be any of the 3 pairs with key == 1.
>>> bag.remove(2)
(2,20)
>>> bag.remove(20)
>>>
'''
pass
def find(self, key):
'''Returns a pair from the Bag that contains key; None if no
such pair exists.
>>> bag = Bag()
>>> bag.add(1, 10)
>>> bag.add(1, 10)
>>> bag.add(1, 20)
>>> bag.add(2, 20)
>>> bag.find(1)
(1,10) # could be any of the 3 pairs with key == 1.
>>> bag.find(2)
(2,20)
>>> bag.find(20)
>>>
'''
pass
def size(self):
'''Returns the number of pairs currently in the Bag.
>>> bag = Bag()
>>> bag.size()
0
>>> bag.add(1, 10)
>>> bag.add(2, 20)
>>> bag.size()
2
>>> bag.add(2, 30)
>>> bag.size()
3
'''
pass
def keys(self):
'''Returns a list of keys in the Bag.
>>> bag = Bag()
>>> bag.add(1, 10)
>>> bag.add(1, 10)
>>> bag.add(1, 20)
>>> bag.add(2, 20)
>>> sorted(bag.keys())
[1, 2]
'''
pass