Exemple #1
0
 def test_reverse_polish(self):
     expr = Addition(
         Subtraction(
             Division(
                 Multiplication(
                     Integer(3),
                     Integer(4),
                 ),
                 Multiplication(
                     Integer(2),
                     Integer(6),
                 ),
             ),
             Division(
                 Integer(3),
                 Addition(
                     Integer(5),
                     Integer(6),
                 ),
             ),
         ),
         Multiplication(
             Subtraction(
                 Addition(
                     Integer(4),
                     Integer(2),
                 ),
                 Subtraction(
                     Integer(2),
                     Integer(1),
                 ),
             ),
             Integer(6),
         ),
     )
     self.assertEqual(expr.to_reverse_polish(),
                      '3 4 * 2 6 * / 3 5 6 + / - '
                      '4 2 + 2 1 - - 6 * +')
Exemple #2
0
 def test_infix(self):
     expr = Addition(
         Subtraction(
             Division(
                 Multiplication(
                     Integer(3),
                     Integer(4),
                 ),
                 Multiplication(
                     Integer(2),
                     Integer(6),
                 ),
             ),
             Division(
                 Integer(3),
                 Addition(
                     Integer(5),
                     Integer(6),
                 ),
             ),
         ),
         Multiplication(
             Subtraction(
                 Addition(
                     Integer(4),
                     Integer(2),
                 ),
                 Subtraction(
                     Integer(2),
                     Integer(1),
                 ),
             ),
             Integer(6),
         ),
     )
     self.assertEqual(expr.to_string(),
                      '3 * 4 / (2 * 6) - 3 / (5 + 6) + '
                      '(4 + 2 - (2 - 1)) * 6')
Exemple #3
0
    def gen_addition_with_result(self, result, depth=0):
        """Generate a, b with a + b = result.

        Return None if generation failed.
        """
        pair = self._numgen.gen_numbers_with_sum(self._maxval,
                                                 result.numerator)
        if not pair:
            return None
        left = Fraction(pair[0], result.denominator)
        right = Fraction(pair[1], result.denominator)
        return Addition(
            self.gen_expr_with_result(left, depth + 1, [1, 1, 2, 2]),
            self.gen_expr_with_result(right, depth + 1, [1, 1, 2, 2]),
        )
Exemple #4
0
 def test_addition(self):
     expr = Addition(Integer(5), Integer(6))
     self.assertEqual(expr.evaluate(), 11)