import queue
class PaMatch():
def __init__(self):
super(PaMatch, self).__init__()
self.lst = queue.LifoQueue()
self.check = 0
def push(self, a):
self.lst.put(a)
def pop(self):
return self.lst.get()
def checkMatch(self, string):
for i in range(len(string)):
if string[i] == "(":
self.push(i)
elif string[i] == ")":
if self.lst.qsize() >= 1:
# 첫번째 출력이 아닐 경우 "," 추
if self.check > 0:
print(",", end="")
print("({},{})".format(self.pop(),i), end="")
self.check += 1
# parenthsis가 맞지 않을 경우 남은 자리를 보여주고 종료
else:
print("\nNot matched : {}".format(i), end="")
# parenthsis가 맞지 않을 경우 남은 자리를 보여주고 종료
for i in range(self.lst.qsize()):
print("\nNot matched : {}".format(self.lst.get()), end="")
print("")
# Good Matching Case
string = "((((A+B)-C)*D)/S*A)"
A = PaMatch()
A.checkMatch(string)
# Not Good Matching Case 1
string2 = "(((((((A+B)-C)*D)/S*A"
B = PaMatch()
B.checkMatch(string2)
# Not Good Matching Case 2
string3 = "((((A+B)-C)*D)))/S*A))))"
C = PaMatch()
C.checkMatch(string3)
(3,7),(2,10),(1,13),(0,18)
(6,10),(5,13),(4,16)
Not matched : 3
Not matched : 2
Not matched : 1
Not matched : 0
(3,7),(2,10),(1,13),(0,14)
Not matched : 15
Not matched : 20
Not matched : 21
Not matched : 22
Not matched : 23
댓글 없음:
댓글 쓰기