-
Notifications
You must be signed in to change notification settings - Fork 4
/
Task 2.py
90 lines (78 loc) · 2.25 KB
/
Task 2.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
82
83
84
85
86
87
88
89
90
# Task 2
class Node:
def __init__(self,data):
self.data = data
self.next = None
class linkedlist:
def __init__(self):
self.head = None
def push(self,element):
if self.head == None:
self.head = Node(element)
else:
n = self.head
while n.next is not None:
n = n.next
n.next = Node(element)
def pop(self):
global list_status
global available
if self.head == None:
list_status = "Empty"
available = None
else:
if self.head.next == None:
available = self.head.data
self.head = None
else:
n = self.head
while n.next.next is not None:
n = n.next
available = n.next.data
n.next = None
openingrackets = ["[",'{',"("]
closingrackets = ["]","}",")"]
def bracket_checking(a,b):
if a == "(" and b == ")":
return True
elif a == "{" and b == "}":
return True
elif a == "[" and b == "]":
return True
else:
return False
check = linkedlist()
string = input()
expression = False
index = []
t = 1
x = 0
y = ""
for v in string:
if v in openingrackets:
check.push(v)
index.append(t)
if v in closingrackets:
check.pop()
if available == None:
expression = False
x = t
y = v
break
else:
expression = bracket_checking(available,v)
if expression == True:
index.pop()
else:
x = index
y = available
t = t+1
if expression is True:
print('This expression is correct.')
else:
if isinstance(x,list): #The isinstance() function returns True if the specified object is of the specified type, otherwise False
print('This expression is NOT correct.')
print("Error at character # {0}. '{1}'- not closed.".format(x[-1], y))
else:
print('This expression is NOT correct.')
print("Error at character # {0}. '{1}'- not opened.".format(x, y))