-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOOP-Stack.py
More file actions
210 lines (158 loc) · 5.2 KB
/
OOP-Stack.py
File metadata and controls
210 lines (158 loc) · 5.2 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
########## OOP - STACK ##########
# A stack is a structure developed to store data in a very specific way.
# The alternative name for a stack (but only in IT terminology) is LIFO = Last In First Out
# A stack is an object with two elementary operations, conventionally named push (when a new element is put on the top) and pop (when an existing element is taken away from the top)
## PROCEDURAL approach
# 1- decide how to store the values which will arrive onto the stack
# a list [] is the best option
stack = []
# 2- define a function that puts a value onto the stack
def push(val):
stack.append(val)
# 3- define a unction to take a value off the stack
def pop():
val = stack[-1]
del stack[-1]
return val
# to summarize:
stack = []
def push(val):
stack.append(val)
def pop():
val = stack[-1]
del stack[-1]
return val
push(3) # stack = [3]
push(2) # stack = [3,2]
push(1) # stack = [3,2,1]
print(pop())
1
print(pop())
2
print(pop())
3
## OBJECT approach
# how the object stack begins, class [name]:
# we have to install a list inside each object of the class
# we want the list to be hidden from the class users' sight
# we have to add a specific statement or instruction
# The properties have to be added to the class manually
# we will equip the class with a specific function – its specificity is dual:
# it has to be named in a strict way
# it is invoked implicitly, when the new object is created
# Such a function is called a constructor, as its general purpose is to construct a new object
# The constructor should know everything about the object's structure,
# and must perform all the needed initializations.
class Stack:
def __init__ (self):
print("Hi!")
stack_object = Stack()
# Hi!
class Stack:
def __init__(self):
self.name = [1,2]
stack_object = Stack()
print(len(stack_object.name))
2
# we've used dot notation, just like when invoking methods; this is the general convention
# for accessing an object's properties
# we need to name the object, put a dot (.) after it, and specify the desired property's name
# we don't use parentheses! we do not invoke a method but a property;
## STACK approach
class Stack:
def __init__(self):
self.__stack_list = []
def push(self, val):
self.__stack_list.append(val)
def pop(self):
val = self.__stack_list[-1]
del self.__stack_list[-1]
return val
stack_object = Stack()
stack_object.push(3) # stack_object = [3]
stack_object.push(2) # stack_object = [3,2]
stack_object.push(1) # stack_object = [3,2,1]
print(stack_object.pop())
1
print(stack_object.pop())
2
print(stack_object.pop())
3
# IF we start functions names by 2*_ or more, we have a problem, as explained above
class Stack:
def __init__(self):
self.__stack_list = []
def __push(self, val):
self.__stack_list.append(val)
def __pop(self):
val = self.__stack_list[-1]
del self.__stack_list[-1]
return val
stack_object = Stack()
stack_object.__push(3) # stack_object = [3]
stack_object.__push(2) # stack_object = [3,2]
stack_object.__push(1) # stack_object = [3,2,1]
print(stack_object.__pop())
print(stack_object.__pop())
print(stack_object.__pop())
# AttributeError: 'Stack' object has no attribute 'push'
# 1*_ is ok
class Stack:
def __init__(self):
self.__stack_list = []
def _push(self, val):
self.__stack_list.append(val)
def _pop(self):
val = self.__stack_list[-1]
del self.__stack_list[-1]
return val
stack_object = Stack()
stack_object._push(3) # stack_object = [3]
stack_object._push(2) # stack_object = [3,2]
stack_object._push(1) # stack_object = [3,2,1]
print(stack_object._pop())
print(stack_object._pop())
print(stack_object._pop())
1
2
3
# Here, both functions have a parameter named "self" at the first position of the parameters list
# it is NEEDED
# All methods have to have this parameter. It plays the same role as the first constructor parameter
# It allows the method to access entities (properties and activities/methods) carried out by the actual object
# Every time Python invokes a method, it implicitly sends the current object as the first argument
# This means that a method must have at least one parameter, which is used by Python itself
# two stacks created from the same base class
class Stack:
def __init__(self):
self.__stack_list = []
def push(self, val):
self.__stack_list.append(val)
def pop(self):
val = self.__stack_list[-1]
del self.__stack_list[-1]
return val
stack_object_1 = Stack()
stack_object_2 = Stack()
stack_object_1.push(3) # = [3]
stack_object_2.push(stack_object_1.pop()) # = [3]
print(stack_object_2.pop()) # = [3]
3
# three ...
class Stack:
def __init__(self):
self.__stack_list = []
def push(self, val):
self.__stack_list.append(val)
def pop(self):
val = self.__stack_list[-1]
del self.__stack_list[-1]
return val
little_stack = Stack() # = []
another_stack = Stack() # = []
funny_stack = Stack() # = []
little_stack.push(1) # = [1]
another_stack.push(little_stack.pop() + 1) # = [2]
funny_stack.push(another_stack.pop() - 2) # = [0]
print(funny_stack.pop())
# 0