-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbracket_checking.py
81 lines (72 loc) · 2.13 KB
/
bracket_checking.py
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
#Bracket maching Program using Python
#Class to create a node
class Node:
#constructer for field of node
def __init__(self,data):
self.data=data
self.next=None
#declaration of a user defined class named stack
class stack:
#constructer
def __init__(self):
self.top=None
#Push into the stack
def push(self,data):
a=Node(data) #creating a node
if self.top is None: #check whether stack is empty
self.top=a
else:
a.next=self.top
self.top=a
#Function to pop
def pop(self):
if self.top is None:
return
else:
self.top=self.top.next
#check the emptyness of stack
def isempty(self):
if self.top is None:
return True
else:
return False
# Declaration of main()
def main():
s=stack()
#taking input of barcket strng from user
string=input("Enter the string of Brackets::")
for i in range(len(string)):
if string[i] is '(' or string[i] is '{' or string[i] is '[' :
s.push(string[i])
elif string[i] is '}' or string[i] is ']' or string[i] is ')':
if s.top is None:
print("Wrong bracket statement!!!")
quit()
elif string[i] is ')':
if s.top.data is '(':
s.pop()
else:
print("Wrong bracket statement!!!")
quit()
elif string[i] is '}':
if s.top.data is '{':
s.pop()
else:
print("Wrong bracket statement!!!")
quit()
elif string[i] is ']':
if s.top.data is '[':
s.pop()
else:
print("Wrong bracket statement!!!")
quit()
else:
print("Wrong bracket statement!!!")
quit()
if s.isempty() is True:
print("Right bracket statement!!!")
quit()
else:
print("Wrong bracket statement!!!")
quit()
main()