-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounting_stack.py
More file actions
54 lines (38 loc) · 1.35 KB
/
Copy pathcounting_stack.py
File metadata and controls
54 lines (38 loc) · 1.35 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
'''
Your task is to create a Stack class and extend it's behavior in such a way so that the class is able to count all the elements that are pushed and popped.
Follow the hints:
- Introduce a property designed to count push and pop operations and name it in a way which guarantees it is hidden;
- Initialize it to zero inside the constructor;
- Provide a method which returns the value currently assigned to the counter
'''
# Writing the python code
class Stack:
def __init__(self):
__stk = []
def push(self, value):
self.__stk.append(value)
def pop(self, value):
if len(self.__stk) > 0:
val = self.__stk[-1]
del self.__stk[-1]
return val
else:
print("The stack is empty.")
class CountingStack(Stack):
def __init__(self):
Stack.__init__(self)
self.count_push = 0
self.count_pop = 0
def count_push(self, val):
self.Stack.push(val)
self.count_push += 1
return self.count_push
def pop(self):
Stack.pop()
self.count_pop += 1
return self.count_pop
test = Stack()
myStack = CountingStack(test)
for i in range(5):
total_pushes = myStack.push(i)
print(total_pushes)