Exemplo n.º 1
0
 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')
Exemplo n.º 2
0
 def test_two_interspersed_round_pairs(self):
     s = "()" + self.open_multi + ")(" + self.close_multi + "()"
     self.assertFalse(validate(s))
Exemplo n.º 3
0
 def test_many_matched_square_and_curly(self):
     s = "()" + self.open_multi + "()" + self.close_multi + "()"
     self.assertTrue(validate(s))
Exemplo n.º 4
0
 def test_many_matched_round_pairs_then_stray_open(self):
     s = "" + self.open_round + self.close_round + "("
     self.assertFalse(validate(s))
Exemplo n.º 5
0
 def test_many_unmatched_round_pairs(self):
     s = self.open_round + ")" + self.close_round
     self.assertFalse(validate(s))
Exemplo n.º 6
0
 def test_empty_string(self):
     self.assertTrue(validate(""))
Exemplo n.º 7
0
 def test_many_matched_round_pairs(self):
     s = self.open_round + "X" + self.close_round
     self.assertTrue(validate(s))
Exemplo n.º 8
0
 def test_single_bracket(self):
     self.assertFalse(validate("{"))
Exemplo n.º 9
0
 def test_switched_different_kinds(self):
     self.assertFalse(validate("[}{]"))
Exemplo n.º 10
0
 def test_one_outward_pair(self):
     self.assertFalse(validate(")()()()()()("))
Exemplo n.º 11
0
 def test_close_one_fewer_than_you_open(self):
     self.assertFalse(validate("((((())))"))
Exemplo n.º 12
0
 def test_consecutive_brackets_of_different_kinds(self):
     self.assertTrue(validate("{}()[]{}"))
Exemplo n.º 13
0
 def test_consecutive_brackets(self):
     self.assertTrue(validate("()()()()()"))
Exemplo n.º 14
0
 def test_nested_brackets_of_different_kinds(self):
     self.assertTrue(validate("{[()]}"))
Exemplo n.º 15
0
 def test_nested_brackets(self):
     self.assertTrue(validate("(((())))"))
Exemplo n.º 16
0
 def test_completely_wrong(self):
     s = "[[]" + self.open_round + "){{}}" + self.close_multi + "(][][])"
     self.assertFalse(validate(s))
Exemplo n.º 17
0
 def test_single_pair(self):
     self.assertTrue(validate("[]"))
Exemplo n.º 18
0
 def test_overlapping_different_kinds(self):
     self.assertFalse(validate("({)}"))