-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstarck.py
More file actions
53 lines (40 loc) · 1.06 KB
/
starck.py
File metadata and controls
53 lines (40 loc) · 1.06 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
class Stack:
def __init__(self):
self.stack = []
def push(self, ele):
self.stack.append(ele)
def pop(self):
return self.stack.pop()
def is_empty(self):
return not len(self.stack)
def get_top(self):
return self.stack[-1]
def __str__(self):
return str(self.stack)
def parenthesis_match(s):
parenthesis_match_dict = {']': '[', ')': '(', '}':'{'}
stack = Stack()
for ch in s:
if ch in ['(', '[', '{']:
stack.push(ch)
elif ch in [')', ']', '}']:
if stack.is_empty():
return False
if stack.get_top() != parenthesis_match_dict[ch]:
return False
else:
stack.pop()
if stack.is_empty():
return True
else:
return False
s1 = '(){([asdfsd])[]}[]'
s2 = '()]'
s3 = '(])['
print(parenthesis_match(s2))
# stack = Stack()
# stack.push(1)
# stack.push(2)
# stack.push(3)
# print(stack.pop())
# print(stack.pop())