-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStacks.py
More file actions
314 lines (212 loc) · 7.54 KB
/
Copy pathStacks.py
File metadata and controls
314 lines (212 loc) · 7.54 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
# 12. Stacks
# A stack is a linear data structure that follows the Last In, First Out (LIFO) principle. This means that the last element added to the stack is the first one to be removed. Stacks are used in various applications, such as function call management in programming, expression evaluation, and backtracking algorithms.
# 12.1 Stack Data Structure
# Characteristics:
# LIFO (Last In, First Out): The last element added is the first to be removed.
# Operations:
# Push: Add an element to the top of the stack.
# Pop: Remove the top element from the stack.
# Peek/Top: Retrieve the top element without removing it.
# isEmpty: Check if the stack is empty.
# Real-Life Example:
# A stack of plates in a cafeteria is a real-life example of a stack data structure. The last plate placed on the stack is the first one to be removed.
# 12.2 Stack in Python
# Python provides a simple way to implement a stack using a list.
# Code Example:
class Stack:
def __init__(self):
self.stack = []
def push(self, data):
self.stack.append(data)
def pop(self):
if not self.is_empty():
return self.stack.pop()
return None
def peek(self):
if not self.is_empty():
return self.stack[-1]
return None
def is_empty(self):
return len(self.stack) == 0
def size(self):
return len(self.stack)
def print_stack(self):
print(self.stack)
# Example usage:
s = Stack()
s.push(1)
s.push(2)
s.push(3)
s.print_stack() # Output: [1, 2, 3]
print(s.pop()) # Output: 3
print(s.peek()) # Output: 2
print(s.is_empty()) # Output: False
# 12.3 Linked List Implementation of Stack in Python
# Using a linked list to implement a stack can provide better performance in some cases, as it avoids the overhead of dynamic array resizing.
# Code Example:
class Node:
def __init__(self, data):
self.data = data
self.next = None
class Stack:
def __init__(self):
self.head = None
def push(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def pop(self):
if self.is_empty():
return None
popped_node = self.head
self.head = self.head.next
return popped_node.data
def peek(self):
if self.is_empty():
return None
return self.head.data
def is_empty(self):
return self.head is None
def print_stack(self):
current = self.head
while current:
print(current.data, end=" -> ")
current = current.next
print("None")
# Example usage:
s = Stack()
s.push(1)
s.push(2)
s.push(3)
s.print_stack() # Output: 3 -> 2 -> 1 -> None
print(s.pop()) # Output: 3
print(s.peek()) # Output: 2
print(s.is_empty()) # Output: False
# 12.4 Stack Applications
# Function Call Management: The call stack keeps track of function calls in programming languages.
# Expression Evaluation: Stacks are used in the evaluation and conversion of expressions (infix, prefix, postfix).
# Backtracking: Algorithms such as maze solving and puzzle solving use stacks to remember paths.
# Browser History: Navigating back and forth in a web browser.
# 12.5 Check for Balanced Parentheses in Python
# One common application of stacks is checking for balanced parentheses in an expression. This involves ensuring that every opening parenthesis has a corresponding closing parenthesis in the correct order.
# Code Example:
def is_balanced(expression):
stack = Stack()
for char in expression:
if char in "([{":
stack.push(char)
elif char in ")]}":
if stack.is_empty():
return False
top_element = stack.pop()
if not is_matching_pair(top_element, char):
return False
return stack.is_empty()
def is_matching_pair(opening, closing):
pairs = { '(': ')', '[': ']', '{': '}' }
return pairs.get(opening) == closing
# Example usage:
expression = "{[(a+b)*(c+d)]}"
print(is_balanced(expression)) # Output: True
expression = "{[(a+b)*(c+d)]"
print(is_balanced(expression)) # Output: False
# Detailed Breakdown of is_balanced:
def is_balanced(expression):
stack = Stack() # Initialize a stack to keep track of opening brackets
for char in expression:
if char in "([{": # If the character is an opening bracket, push it onto the stack
stack.push(char)
elif char in ")]}": # If the character is a closing bracket
if stack.is_empty(): # If the stack is empty, return False (unmatched closing bracket)
return False
top_element = stack.pop() # Pop the top element from the stack
if not is_matching_pair(top_element, char): # Check if the popped element matches the closing bracket
return False
return stack.is_empty() # If the stack is empty, all brackets were matched; otherwise, return False
def is_matching_pair(opening, closing):
pairs = { '(': ')', '[': ']', '{': '}' } # Dictionary of matching pairs
return pairs.get(opening) == closing # Return True if the opening and closing brackets match
# Initialize a Stack:
stack = Stack()
# This creates an empty stack to keep track of opening brackets.
# Iterate through the Expression:
# for char in expression:
# Loop through each character in the given expression.
# Push Opening Brackets:
# if char in "([{":
# stack.push(char)
# If the character is an opening bracket, push it onto the stack.
# Handle Closing Brackets:
# elif char in ")]}":
# if stack.is_empty():
# return False
# top_element = stack.pop()
# if not is_matching_pair(top_element, char):
# return False
# If the character is a closing bracket:
# Check if the stack is empty. If it is, return False because there's no matching opening bracket.
# Pop the top element from the stack.
# Check if the popped element matches the closing bracket using the is_matching_pair function. If it doesn't, return False.
# Check for Remaining Opening Brackets:
# return stack.is_empty()
# If the stack is empty at the end, all brackets were matched; otherwise, return False.
class Stack:
def __init__(self):
self.items = []
def push(self, data):
self.items.append(data)
def pop(self):
return self.items.pop()
def is_empty(self):
if self.items == []:
return True
else:
return False
def get_stack(self):
return self.items
def reverse_string(stack, inp_str):
for i in range(len(inp_str)):
stack.push(inp_str[i])
the_str = ""
while not stack.is_empty():
the_str += stack.pop()
return the_str
st = Stack()
# st.push(1)
# st.push(2)
# st.push(3)
# st.push(4)
# print(st.get_stack())
inp_str = 'Hello'
# print(inp_str[::-1])
reve = reverse_string(st, inp_str)
print(reve)
class Stack:
def __init__(self):
self.set = []
def push(self, data):
self.set.append(data)
def pop(self):
return self.set.pop()
def is_empty(self):
if self.set == []:
return True
return False
def peek(self):
return self.set[-1]
def print(self):
return self.set
def int_to_bin(stack, num):
while num // 2 != 0:
prev_num = num
num = num // 2
stack.push(prev_num % 2)
stack.push(num % 2)
new_num = ''
while not stack.is_empty():
new_num += str(stack.pop())
return new_num
st = Stack()
num = 242
ss = int_to_bin(st, num)
print(ss)