-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
97 lines (73 loc) · 1.81 KB
/
stack.py
File metadata and controls
97 lines (73 loc) · 1.81 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
"""
1. push() = insert/push element into top of stack
2. pop() = returns the top most element from stack reducing its size
3. top() = only returns the value of top most element without modifying stack
"""
# Initialise Global Variables
STACK_SIZE = 6
stack = [""] * STACK_SIZE
top_of_stack = -1
"""Function to PUSH an element into Stack"""
def push(element):
global top_of_stack
if top_of_stack < STACK_SIZE - 1:
top_of_stack += 1
stack[top_of_stack] = element
"""Function to POP an element into Stack"""
def pop():
global top_of_stack
if top_of_stack >= 0:
element = stack[top_of_stack]
top_of_stack -= 1
return element
else:
return None
"""Function to TOP an element into Stack"""
def top():
global top_of_stack
if top_of_stack >= 0:
element = stack[top_of_stack]
return element
else:
return None
def is_valid(s):
# PUSH Logic: If opening brackets are identified, push them into stack
for i in s:
if i == "{" or i == "(" or i == "[":
push(i)
# POP Logic: If closing brackets are identified, check for it's opening
elif i == "}" or i == ")" or i == "]":
check = top()
if check is None:
return False
# Parenthesis
elif i == "}":
if top() == "{":
pop()
else:
return False
# Regular Brackets
elif i == ")":
if top() == "(":
pop()
else:
return False
# Square Brackets
elif i == "]":
if top() == "[":
pop()
else:
return False
else:
print("Please enter brackets.")
return False
if top_of_stack == -1:
return True
else:
return False
def main():
string = input("Input s: ")
result = is_valid(string)
print("Output:", result)
if __name__ == "__main__":
main()