def get_eqn(self, s, eqn): try: if s == 'Del': if self.equation != '': self.equation = list(self.equation) del (self.equation[-1]) self.equation = ''.join(self.equation) eqn.set(self.equation) elif s == 'AC': self.equation = '' eqn.set(self.equation) elif s == '(': if self.equation == '' or self.equation[-1] in '+-(' or len( self.equation) > 1 and self.equation[-2] in '+-/*': self.equation += '(' eqn.set(self.equation) elif len(self.equation ) > 1 and self.equation[-2] not in '+-/*' or len( self.equation) == 1: self.equation = '{} {}'.format(self.equation, '* (') eqn.set(self.equation) elif s == '=': if '(' in self.equation: if brackets.validate(self.equation): self.equation = brackets.simplify(self.equation) else: raise ValueError reverse_polish = rpolish.infix_to_post(self.equation) ok = rpolish.postfix_solve(reverse_polish) self.equation = '' if 'No' in str(ok): raise ValueError eqn.set(ok) elif s in '+-/*': if len(self.equation) == 0 or len( self.equation) > 2 and self.equation[-2] in '+-*/': if s in '-+': self.equation += s else: self.equation += " {} ".format(s) eqn.set(self.equation) else: self.equation += s eqn.set(self.equation) except: eqn.set('Error')
def test_two_interspersed_round_pairs(self): s = "()" + self.open_multi + ")(" + self.close_multi + "()" self.assertFalse(validate(s))
def test_many_matched_square_and_curly(self): s = "()" + self.open_multi + "()" + self.close_multi + "()" self.assertTrue(validate(s))
def test_many_matched_round_pairs_then_stray_open(self): s = "" + self.open_round + self.close_round + "(" self.assertFalse(validate(s))
def test_many_unmatched_round_pairs(self): s = self.open_round + ")" + self.close_round self.assertFalse(validate(s))
def test_empty_string(self): self.assertTrue(validate(""))
def test_many_matched_round_pairs(self): s = self.open_round + "X" + self.close_round self.assertTrue(validate(s))
def test_single_bracket(self): self.assertFalse(validate("{"))
def test_switched_different_kinds(self): self.assertFalse(validate("[}{]"))
def test_one_outward_pair(self): self.assertFalse(validate(")()()()()()("))
def test_close_one_fewer_than_you_open(self): self.assertFalse(validate("((((())))"))
def test_consecutive_brackets_of_different_kinds(self): self.assertTrue(validate("{}()[]{}"))
def test_consecutive_brackets(self): self.assertTrue(validate("()()()()()"))
def test_nested_brackets_of_different_kinds(self): self.assertTrue(validate("{[()]}"))
def test_nested_brackets(self): self.assertTrue(validate("(((())))"))
def test_completely_wrong(self): s = "[[]" + self.open_round + "){{}}" + self.close_multi + "(][][])" self.assertFalse(validate(s))
def test_single_pair(self): self.assertTrue(validate("[]"))
def test_overlapping_different_kinds(self): self.assertFalse(validate("({)}"))