| 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 |
| |
| else: |
| print("\nNot matched : {}".format(i), end="") |
| |
| for i in range(self.lst.qsize()): |
| print("\nNot matched : {}".format(self.lst.get()), end="") |
| |
| print("") |
| |
| |
| string = "((((A+B)-C)*D)/S*A)" |
| A = PaMatch() |
| A.checkMatch(string) |
| |
| |
| string2 = "(((((((A+B)-C)*D)/S*A" |
| B = PaMatch() |
| B.checkMatch(string2) |
| |
| |
| 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
댓글 없음:
댓글 쓰기