コード例 #1
0
class BalancedParentheses:
    def __init__(self):
        self.myStack = Stack(100)

    def AreParanthesesBalanced(self, expression):
        for c in expression:
            if (c == '[' or c == '{' or c == '('):
                self.myStack.push(c)
            if (c == ']' or c == '}' or c == ')'):
                if (self.myStack.isEmpty()
                        or not self.ArePair(self.myStack.topElement(), c)):
                    return False
                else:
                    self.myStack.pop()

        if (self.myStack.isEmpty()):
            return True
        else:
            return False

    def ArePair(self, opening, closing):
        if (opening == '[' and closing == ']'):
            return True
        elif (opening == '{' and closing == '}'):
            return True
        elif (opening == '(' and closing == ')'):
            return True
        else:
            return False
コード例 #2
0
 def test_isNotEmpty(self):
     myStack = Stack(10)
     myStack.push(10)
     self.assertFalse(myStack.isEmpty())
コード例 #3
0
 def test_isEmpty(self):
     myStack = Stack(10)
     self.assertTrue(myStack.isEmpty())