-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdupelessstack.py
151 lines (120 loc) · 3.72 KB
/
dupelessstack.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
"""
This module implements the Dupeless Stack data structure.
Dupeless Stack is a stack without duplicates.
The order of the items of the stack is maintained.
"""
class DupelessStack:
"""Dupeless Stack class"""
def __init__(self, items=[]):
self._items = []
for item in items:
if item not in self._items:
self._items.append(item)
def __contains__(self, item):
return item in self._items
def __eq__(self, other):
return sorted(self._items) == sorted(other._items)
def __repr__(self):
out = "DupelessStack(["
if self.is_empty():
return "{}])".format(out)
for item in self._items:
if item != self._items[-1]:
out += "{}, ".format(item)
out += "{}])".format(item)
return out
def __hash__(self):
hashval = 0
for item in self._items:
hashval ^= hash(item)
return hashval
def size(self):
"""Return the size of the Dupeless Stack"""
return len(self._items)
def top(self):
"""Return the top item of the Dupeless Stack"""
if self.is_empty():
raise RuntimeError("Attempt to get a top of the empty stack!")
return self._items[-1]
def push(self, item):
"""Push an item to the Dupeless Stack"""
if item not in self._items:
self._items.append(item)
def pop(self):
"""Pop the last item from the Dupeless Stack"""
if self.is_empty():
raise RuntimeError("Attempt to pop the empty stack!")
item = self.top()
self._items = self._items[:-1]
return item
def clear(self):
"""Clear the Dupeless Stack"""
self._items = []
def is_empty(self):
"""Check the emptiness of the Dupeless Stack"""
return self._items == []
def main():
"""Testing DupelessStack datatype"""
# Testing DupelessStack creation
dls = DupelessStack([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
if dls:
print("Test 0 passed")
else:
print("Test 0 failed")
# Testing magic method '__contains__'
if (3 in dls) is True:
print("Test 1 passed")
else:
print("Test 1 failed")
# Testing magic method '__eq__'
new_dls = DupelessStack([1, 5, 3, 2, 4, 12, 5])
one_more_dls = DupelessStack([5, 4, 5, 5, 1, 2, 4, 12, 3])
if new_dls == one_more_dls:
print("Test 2 passed")
else:
print("Test 2 failed")
# Testing magic method '__repr__'
if str(dls) == "DupelessStack([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])":
print("Test 3 passed")
else:
print("Test 3 failed")
# Testing magic method '__hash__'
if {DupelessStack([0, 1, 2]): 0}:
print("Test 4 passed")
else:
print("Test 4 failed")
# Testing method 'size'
if dls.size() == 10:
print("Test 5 passed")
else:
print("Test 5 failed")
# Testing method 'top'
if dls.top() == 9:
print("Test 6 passed")
else:
print("Test 6 failed")
# Testing method 'push'
dls.push(10)
if dls == DupelessStack([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]):
print("Test 7 passed")
else:
print("Test 7 failed")
# Testing method 'pop'
if dls.pop() == 10:
if dls == DupelessStack([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]):
print("Test 8 passed")
else:
print("Test 8 failed")
# Testing method 'clear'
dls.clear()
if dls == DupelessStack([]):
print("Test 9 passed")
else:
print("Test 9 failed")
# Testing method 'is_empty'
if dls.is_empty() is True:
print("Test 10 passed")
else:
print("Test 10 failed")
if __name__ == "__main__":
main()