def test_substitute(self):
     self.assertEqual(a.substitute(b, i_1), a)
     self.assertEqual(a.substitute(a, i_1), i_1)
     self.assertEqual(Plus(a, b).substitute(a, i_1), Plus(i_1, b))
     self.assertEqual(Plus(a, b).substitute(b, i_1), Plus(a, i_1))
     self.assertEqual(Equals(a, b).substitute(a, i_1), Equals(i_1, b))
     self.assertEqual(Equals(a, b).substitute(b, i_1), Equals(a, i_1))
 def test_associate(self):
     self.assertEqual(Int(12).associate_l_to_r(), Int(12))
     self.assertEqual(Int(12).associate_r_to_l(), Int(12))
     self.assertEqual(Var("a").associate_l_to_r(), Var("a"))
     self.assertEqual(Var("a").associate_r_to_l(), Var("a"))
     self.assertEqual(
         Plus(Int(1), Int(2)).associate_l_to_r(), Plus(Int(1), Int(2)))
     self.assertEqual(
         Times(Int(1), Int(2)).associate_l_to_r(), Times(Int(1), Int(2)))
Example #3
0
    "expr.remove_add_zeroes(): Removes all zeroes being added inside of expr"
]


def check_answer(a0):
    return lambda a: a == a0


def conv_q(expr1_str, expr2_str, af):
    return Question(
        "Convert the expression '{}' to '{}'".format(expr1_str, expr2_str), af)


questions = [
    conv_q("1 + (2 + (3 + 4))", "1 + ((2 + 3) + 4)",
           check_answer(Plus(Int(1), Plus(Plus(Int(2), Int(3)), Int(4))))),
    conv_q("1 + ((2 + 3) + 4)", "1 + (2 + (3 + 4))",
           check_answer(Plus(Int(1), Plus(Int(2), Plus(Int(3), Int(4)))))),
    conv_q("(2 + 1) + 3", "(1 + 2) + 3",
           check_answer(Plus(Plus(Int(1), Int(2)), Int(3)))),
    conv_q("1 + (2 + 3)", "(2 + 1) + 3",
           check_answer(Plus(Plus(Int(2), Int(1)), Int(3)))),
    conv_q("1 - 1", "0", check_answer(Int(0))),
    conv_q("2 + 0", "2", check_answer(Int(2))),
    conv_q("-6 + 6", "0", check_answer(Int(0))),
    conv_q("(0 + 2) + 0", "2", check_answer(Int(2))),
    conv_q("2 + (1 - 1)", "2", check_answer(Int(2))),
    conv_q("-1 + (2 + 1)", "0", check_answer(Int(2))),
    conv_q("(-1 + 2) + (1 - 2)", "0", check_answer(Int(0))),
    conv_q("(1 + 2) + (3 + -1)", "2 + 3", check_answer(Plus(Int(2), Int(3)))),
]
Example #4
0
    "expr.commute(): If expr looks like 'a + b', it is converted to 'b + a'.  You can replace '+' with '*'"
]


def check_answer(a0):
    return lambda a: a == a0


def conv_q(expr1_str, expr2_str, af):
    return Question(
        "Convert the expression '{}' to '{}'".format(expr1_str, expr2_str), af)


questions = [
    conv_q("1 * (2 * (3 * 4))", "((1 * 2) * 3) * 4",
           check_answer(Times(Times(Times(Int(1), Int(2)), Int(3)), Int(4)))),
    conv_q("5 + 6", "6 + 5", check_answer(Plus(Int(6), Int(5)))),
    conv_q("(2 + 3) + 1", "1 + (2 + 3)",
           check_answer(Plus(Int(1), Plus(Int(2), Int(3))))),
    conv_q("(2 + 3) + 1", "(3 + 2) + 1",
           check_answer(Plus(Plus(Int(3), Int(2)), Int(1)))),
    conv_q("-1 + 2", "2 - 1", check_answer(Minus(Int(2), Int(1)))),
    conv_q("(1 - 3) - 2", "(1 - 2) - 3",
           check_answer(Minus(Minus(Int(1), Int(2)), Int(3)))),
    conv_q("1 + (2 + 3)", "3 + (2 + 1)",
           check_answer(Plus(Int(3), Plus(Int(2), Int(1))))),
    conv_q("(-3 - 2) - 1", "(-1 - 2) - 3",
           check_answer(Minus(Minus(Negative(Int(1)), Int(2)), Int(3)))),
    conv_q("1 + (2 + (3 + 4))", "4 + (3 + (2 + 1))",
           check_answer(Plus(Int(4), Plus(Int(3), Plus(Int(2), Int(1))))))
]
Example #5
0
from Question import Question
from Expression import Int, Plus, Equals, Var, Minus


def check_sub_q(a):
    return a.interpret() == True


questions = [
    Question("If a = 1, what is another way of writing 'b'?",
             lambda a: a == Var("b")),
    Question("If x = 3, what is another way of writing 'x + y?'",
             lambda a: a == Plus(Int(3), Var("y"))),
    Question(
        "Use substitute to make '11 + a = 17' True // copy-paste Equals(Plus(Int(11), Var(\"a\")), Int(17))",
        check_sub_q),
    Question(
        "Use substitute and Minus to make '44 - b = 20' True // copy-paste Equals(Minus(Int(44), Var(\"b\")), Int(20))",
        check_sub_q),
    Question(
        "Use substitute to make '30 + 50 = x' True // copy-paste Equals(Plus(Int(30), Int(50)), Var(\"x\"))",
        check_sub_q),
    Question(
        "Use substitute to make 'y - 7 = 33' True // copy-paste Equals(Minus(Var(\"y\"), Int(7)), Int(33))",
        check_sub_q),
    Question(
        "Use substitute to make 'n + 50 = 140' True // copy-paste Equals(Plus(Var(\"n\"), Int(50)), Int(140))",
        check_sub_q),
    Question(
        "Use substitute to make '11 + m = 31' True // copy-paste Equals(Plus(Int(11), Var(\"m\")), Int(31))",
        check_sub_q),
Example #6
0
from Question import Question
from Expression import Int, Plus, Equals, Var

a = Var("a")

questions = [
    Question("How do you write '10' using Int?", lambda a: a == Int(10)),
    Question("How do you write '812' using Int?", lambda a: a == Int(812)),
    Question("How do you write '-812' using Int?", lambda a: a == Int(-812)),
    Question("How do you write '1,000' using Int?", lambda a: a == Int(1000)),
    Question("How do you write '4 + 5' using Plus?",
             lambda a: a == Plus(Int(4), Int(5))),
    Question("How do you write '4 + -5' using Plus?",
             lambda a: a == Plus(Int(4), Int(-5))),
    Question("How do you write '(1 + 2) + 3' using Plus?",
             lambda a: a == Plus(Plus(Int(1), Int(2)), Int(3))),
    Question("What is '(1 + 2) + 3' equal to?", lambda a: a == 6),
    Question("How do you write '1 + (2 + 3)' using Plus?",
             lambda a: a == Plus(Int(1), Plus(Int(2), Int(3)))),
    Question("What is '1 + (2 + 3)' equal to?", lambda a: a == 6),
    Question("How do you write '1 = 2' using Equals?",
             lambda a: a == Equals(Int(1), Int(2))),
    Question("Is 1 = 2 True or False?", lambda a: a == False),
    Question("How do you write '1 = 1' using Equals?",
             lambda a: a == Equals(Int(1), Int(1))),
    Question("Is 1 = 1 True or False?", lambda a: a),
    Question(
        "How do you write '(1 + 2) + 3 = 1 + (2 + 3)' using Equals?",
        lambda a: a == Equals(Plus(Plus(Int(1), Int(2)), Int(3)),
                              Plus(Int(1), Plus(Int(2), Int(3))))),
    Question("Is (1 + 2) + 3 = 1 + (2 + 3) True or False?", lambda a: a),
    "expr.comm(): same as expr.commute()",
    "expr.sub_alr(sub_expr): Changes sub_expr in expr using associate_l_to_r()",
    "expr.sub_arl(sub_expr): Changes sub_expr in expr using associate_r_to_l()",
    "expr.sub_comm(sub_expr): Changes sub_expr in expr using commute()",
    "expr.minus_to_zero(): If expr looks like 'n - n', it is converted to '0'",
    "expr.mz(): same as expr.minus_to_zero()",
    "expr.sub_mz(): Changes sub_expr in expr using minus_to_zero()",
    "expr.remove_add_zeroes(): Removes all zeroes being added inside of expr",
    # "expr.plus_to_times(): Changes all subexpressions of the form 'n + n + ... + n' to 'm * n'",
    # "expr.both_sides_plus(expr_to_add): Adds expr_to_add to both sides of the equal sign",
    # "expr.both_sides_minus(expr_to_add): Adds expr_to_add to both sides of the equal sign"
]

quesitons = [
    Question("What is the result of '3 - 5'.pn() using Expressions?",
             check_answer(Plus(Int(3), Negative(Int(5))))),
    Question("What is the result of calling '3 + -5'.pn() using Expressions?",
             check_answer(Plus(Int(3), Negative(Int(5))))),
    Question(
        "What is the result of calling '(2 - 3) + 6'.comm() using Expressions?",
        check_answer(Plus(Int(6), Minus(Int(2), Int(3))))),
    Question(
        "What is the result of calling '(2 - 3) + 6'.mz() using Expressions?",
        check_answer(Plus(Minus(Int(2), Int(3)), Int(6)))),
    Question(
        "What is the result of calling '(2 - 3) + 6'.sub_comm('2 - 3') using Expressions?",
        check_answer(Plus(Minus(Int(2), Int(3)), Int(6)))),
    Question(
        "What is the result of calling '(2 - 2) + 6'.mz() using Expressions?",
        check_answer(Plus(Minus(Int(2), Int(3)), Int(6)))),
    Question(
Example #8
0
    "expr.to_pos_neg(): If expr looks like 'a - b', it is converted to 'a + -b'",
    "expr.pos_neg_to_minus(): If expr looks like 'a + -b', it is converted to 'a - b'",
    "expr.associate_l_to_r(): If expr looks like '(a + b) + c', it is converted to 'a + (b + c)'.  You can replace '+' with '*'.",
    "expr.associate_r_to_l(): Converts an expression that looks like 'a + (b + c)' to '(a + b) + c'.  You can replace '+' with '*'",
    "expr.rewrite_subexpression(sub_expr, equal_expr): Rewrites every expression equal to subexpr inside of expr to equal_expr, but only if equal_expr means the same thing as sub_expr."
]

# I need to teach Henri the associative property at some point, but I won't today.
# Actually, I might need to.  Teaching it would be simplified if Henri had the option to convert all subtractions to
# +-s.  Fpr example, (11 + n) - n would become (11 + n) + -n.  The associative property can then be used to turn the
# expression into (11)

questions = [
    # 1 + (2  - 3) --> 1 + (2 + -3) --> 1 + (2 - 3)
    conv_q("1 + (2 - 3)", "(1 + 2) - 3",
           check_answer(Minus(Plus(Int(1), Int(2)), Int(3)))),
    # (1 * 2) * 3 --> 1 * (2 * 3)
    conv_q("(1 * 2) * 3", "1 * (2 * 3)",
           check_answer(Times(Int(1), Times(Int(2), Int(3))))),
    # (1 * -2) * 3 --> 1 * (-2 * 3)
    conv_q("(1 * -2) * 3", "1 * (-2 * 3)",
           check_answer(Times(Int(1), Times(Negative(Int(2)), Int(3))))),
    # 1 * ((2 * 3) * 4) --> (1 * (2 * 3)) * 4
    conv_q("1 * ((2 * 3) * 4)", "(1 * (2 * 3)) * 4",
           check_answer(Times(Times(Int(1), Times(Int(2), Int(3))), Int(4)))),
    # 1 * (2 * (3 * 4)) --> 1 * ((2 * 3) * 4) --> (1 * (2 * 3)) * 4
    conv_q("1 * (2 * (3 * 4))", "(1 * (2 * 3)) * 4",
           check_answer(Times(Times(Int(1), Times(Int(2), Int(3))), Int(4)))),
    # 1 * (2 * (3 * 4)) --> 1 * ((2 * 3) * 4) --> (1 * (2 * 3)) * 4 --> ((1 * 2) * 3) * 4
    conv_q("1 * (2 * (3 * 4))", "((1 * 2) * 3) * 4",
           check_answer(Times(Times(Times(Int(1), Int(2)), Int(3)), Int(4))))
    return lambda a: a == a0


def check_str_no_ws(a0):
    return lambda a: remove_all_whitespace(a) == remove_all_whitespace(a0)


def conv_q(expr1_str, expr2_str, af):
    return Question(
        "Convert the expression '{}' to '{}'".format(expr1_str, expr2_str), af)


extra_stuff = [
    Times(Times(Var("n"), Var("n")), Var("n")),
    Minus(Int("n"), Int("n")),
    Plus(Int(11), Minus(Var("n"), Var("n"))),
    Plus(Plus(Int(3), Int(1)), Var("d")),
]

documentation = [
    "Expression.to_pos_neg(): Converts an expression that looks like 'a - b' to 'a + -b'",
    "Expression.pos_neg_to_minus(): Converts an expression that looks like 'a + -b' to 'a - b'",
    "Expression.associate_l_to_r(): Converts an expression that looks like '(a + b) + c' to 'a + (b + c)'.  You can replace '+' with '*'",
    "Expression.associate_r_to_l(): Converts an expression that looks like 'a + (b + c)' to '(a + b) + c'.  You can replace '+' with '*'",
]

# I need to teach Henri the associative property at some point, but I won't today.
# Actually, I might need to.  Teaching it would be simplified if Henri had the option to convert all subtractions to
# +-s.  Fpr example, (11 + n) - n would become (11 + n) + -n.  The associative property can then be used to turn the
# expression into (11)
 def test_evaluate_subexpression(self):
     i_P_10_12 = Plus(Int(10), Int(12))
     # Replace subexpression fails no var replacement (should use substitute)
     # with self.assertRaises(Exception):
     #     Var("a").rewrite_subexpression(Var("a"), Int(2))
     # Atomic replace subexpression within atomic expression
     self.assertEqual(
         Int(12).rewrite_subexpression(Int(12), Int(12)), Int(12))
     # Atomic replace subexpression doesn't exist within atomic expression
     self.assertEqual(
         Int(12).rewrite_subexpression(Int(13), Int(12)), Int(12))
     # Atomic replace subexpression with unequivalent within atomic expression
     self.assertEqual(
         Int(12).rewrite_subexpression(Int(12), Int(13)), Int(12))
     # Atomic replace subexpression with non-atomic expression
     self.assertEqual(
         Int(12).rewrite_subexpression(Int(12), Plus(Int(3), Int(9))),
         Plus(Int(3), Int(9)))
     # Atomic replace subexpression within non-atomic expression
     self.assertEqual(
         Plus(Int(10),
              Int(12)).rewrite_subexpression(Int(12), Plus(Int(3), Int(9))),
         Plus(Int(10), Plus(Int(3), Int(9))))
     # Non-atomic replace subexpression
     self.assertEqual(i_P_10_12.rewrite_subexpression(i_P_10_12, Int(22)),
                      Int(22))
     # Non-atomic replace subexpression doesn't exist within non-atomic expression
     self.assertEqual(i_P_10_12.rewrite_subexpression(Int(13), Int(13)),
                      i_P_10_12)
     # Non-atomic replace subexpression with unequivalent within non-atomic expression
     self.assertEqual(
         Plus(Int(10),
              Plus(Int(11),
                   Int(12))).rewrite_subexpression(Plus(Int(11), Int(12)),
                                                   Int(22)),
         Plus(Int(11), Int(12)))
import unittest
from Expression import Var, Int, Plus, Minus, Equals, Times, Negative

i_1, i_2, i_n2, i_3, i_4, i_5, i_6, i_7 = Int(1), Int(2), Int(-2), Int(3), Int(
    4), Int(5), Int(6), Int(7)
a, b, c, x, y, z, cool = Var("a"), Var("b"), Var("c"), Var("x"), Var("y"), Var(
    "z"), Var("cool")
i_P_1_2 = Plus(i_1, i_2)
i_P_n2_3 = Plus(i_n2, i_3)
i_P_nested = Plus(i_P_1_2, i_P_n2_3)
b_2_E_3 = Equals(i_2, i_3)
b_P_a_3_E_P_n2_3 = Equals(Plus(a, i_3), i_P_n2_3)


class TestRepresentation(unittest.TestCase):
    def test_simple(self):
        self.assertEqual(i_1.__repr__(), "1")
        self.assertEqual(i_n2.__repr__(), "-2")
        self.assertEqual(a.__repr__(), "a")
        self.assertEqual(cool.__repr__(), "cool")

    def test_plus(self):
        self.assertEqual(i_P_1_2.__repr__(), "1 + 2")
        self.assertEqual(i_P_nested.__repr__(), "(1 + 2) + (-2 + 3)")

    def test_equals(self):
        self.assertEqual(b_2_E_3.__repr__(), "2 = 3")
        self.assertEqual(b_P_a_3_E_P_n2_3.__repr__(), "a + 3 = -2 + 3")

    def test_substitute(self):
        self.assertEqual(a.substitute(b, i_1), a)
Example #12
0
    return lambda a: a == table


def eval_in_for(se, e, v, n, answer):
    return Question(
        "Rewrite the expression '{}' in '{}' for {} = {}".format(se, e, v, n),
        check_q(answer))


def eval_in(se, e, answer):
    return Question("Rewrite the expression '{}' in '{}'".format(se, e),
                    check_q(answer))


questions = [
    eval_in_for("3 * r", "21 + (3 * r)", "r", 1, Plus(Int(21), Int(3))),
    eval_in("21 + 3", "21 + 3", Int(24)),
    eval_in_for("3 * r", "21 + (3 * r)", "r", 2, Plus(Int(21), Int(6))),
    eval_in("21 + 6", "21 + 6", Int(27)),
    eval_in_for("3 * r", "21 + (3 * r)", "r", 3, Plus(Int(21), Int(9))),
    eval_in("21 + 9", "21 + 9", Int(30)),
    eval_in_for("3 * r", "21 + (3 * r)", "r", 7, Plus(Int(21), Int(21))),
    eval_in("21 + 21", "21 + 21", Int(42)),
    eval_in_for("3 * r", "21 + (3 * r)", "r", 20, Plus(Int(21), Int(60))),
    eval_in("21 + 60", "21 + 60", Int(81)),
    eval_in_for("3 * r", "21 + (3 * r)", "r", 50, Plus(Int(21), Int(150))),
    eval_in("21 + 150", "21 + 150", Int(171)),
    Question(
        "Make a dictionary of '21 + (3 * r)' for r = 1, 2, 3, 7, 20, and 50",
        check_q_table(Plus(Int(21), Times(Int(3), Var("r"))),
                      [[Int(1), Int(24)], [Int(2), Int(27)],
Example #13
0
    "expr.remove_add_zeroes(): Removes all zeroes being added inside of expr",
    "expr.combine_like_terms(n): Changes all subexpressions of the form 'n + n + ... + n' to 'm * n'",
    # "expr.both_sides_plus(expr_to_add): Adds expr_to_add to both sides of the equal sign",
    # "expr.both_sides_minus(expr_to_add): Adds expr_to_add to both sides of the equal sign"
]

def check_or_answer(a1, a2):
    return lambda a: a == a1 or a == a2

def check_answer(a0):
    return lambda a: a == a0

def conv_q(expr1_str, expr2_str, af):
    return Question("Convert the expression '{}' to '{}'".format(expr1_str, expr2_str), af)

def simp_q(expr_str, af):
    return Question("Simplify the expression '{}'".format(expr_str), af)

def solve_q(start_str, v, af):
    return Question("Solve for '{}' in '{}'".format(v, start_str), af)

questions = [
    conv_q("n + n", "2 * n", check_answer(Times(Int(2), Var("n")))),
    conv_q("1 + 1", "2 * 1", check_answer(Times(Int(2), Int(1)))),
    conv_q("n + (n + n)", "3 * n", check_answer(Times(Int(3), Var("n")))),
    conv_q("(n + n) + n", "3 * n", check_answer(Times(Int(3), Var("n")))),
    conv_q("a + (4 * a)", "5 * a", check_answer(Times(Int(5), Var("a")))),
    conv_q("a + (a * 4)", "5 * a", check_answer(Times(Int(5), Var("a")))),
    conv_q("(3 * a) + (a * 4)", "7 * a", check_answer(Times(Int(7), Var("a")))),
    conv_q("(3 + a) + (a * 4)", "3 + (5 * a)", check_answer(Plus(Int(3), Times(Int(5), Var("a"))))),
]
Example #14
0
    return Question("Convert the expression '{}' to '{}'".format(expr1_str, expr2_str), af)

documentation = [
    "expr.to_pos_neg(): If expr looks like 'a - b', it is converted to 'a + -b'",
    "expr.pos_neg_to_minus(): If expr looks like 'a + -b', it is converted to 'a - b'",
    "expr.associate_l_to_r(): If expr looks like '(a + b) + c', it is converted to 'a + (b + c)'.  You can replace '+' with '*'.",
    "Expression.associate_r_to_l(): Converts an expression that looks like 'a + (b + c)' to '(a + b) + c'.  You can replace '+' with '*'",
    "Expression.rewrite_subexpression(sub_expr, equal_expr): "
]

# I need to teach Henri the associative property at some point, but I won't today.
# Actually, I might need to.  Teaching it would be simplified if Henri had the option to convert all subtractions to
# +-s.  Fpr example, (11 + n) - n would become (11 + n) + -n.  The associative property can then be used to turn the
# expression into (11)

questions = [
    # (1 + 2) + 3 --> 1 + (2 + 3)
    conv_q("(1 + 2) + 3", "1 + (2 + 3)", check_answer(Plus(Int(1), Plus(Int(2), Int(3))))),
    # 9 - 4 --> 9 + -4
    conv_q("9 - 4", "9 + -4", check_answer(Plus(Int(9), Negative(Int(4))))),
    # (a + -4) + -8 --> (a - 4) - 8
    conv_q("(a + -4) + -8", "(a - 4) - 8", check_answer(Minus(Minus(Var("a"), Int(4)), Int(8)))),
    # 1 + (2  - 3) --> 1 + (2 + -3) --> 1 + (2 - 3)
    conv_q("1 + (2 - 3)", "(1 + 2) - 3", check_answer(Minus(Plus(Int(1), Int(2)), Int(3)))),
    # (1 - 2) - 3 --> (1 + -2) + -3 --> 1 + (-2 + -3) --> 1 + (-2 - 3)
    conv_q("(1 - 2) - 3", "1 + (-2 - 3)", check_answer(Plus(Int(1), Minus(Negative(Int(2)), Int(3))))),
    # (a + 4) + (3 - 2) --> (a + 4) + (3 + -2) --> ((a + 4) + 3) + -2 --> ((a + 4) + 3) - 2
    conv_q("(a + 4) + (3 - 2)", "((a + 4) + 3) - 2)", check_answer(Minus(Plus(Plus(Var("a"), Int(4)), Int(3)), Int(2)))),
    # 1 + ((2 + 3) + 4) --> 1 + (2 + (3 + 4))
    conv_q("1 + ((2 + 3) + 4)", "1 + (2 + (3 + 4))", check_answer(Plus(Int(1), Plus(Int(2), Plus(Int(3), Int(4))))))
]
 def test_to_pos_neg(self):
     self.assertEqual(Int(12).to_pos_neg(), Int(12))
     self.assertEqual(Var("a").to_pos_neg(), Int("a"))
     self.assertEqual(Negative(Int(12).to_pos_neg()), Negative(Int(12)))
     self.assertEqual(
         Plus(Int(3), Int(5)).to_pos_neg(), Plus(Int(3), Int(5)))
     self.assertEqual(
         Times(Int(3), Int(5)).to_pos_neg(), Times(Int(3), Int(5)))
     self.assertEqual(
         Minus(Int(3), Int(5)).to_pos_neg(), Plus(Int(3), Negative(Int(5))))
     self.assertEqual(
         Negative(Minus(Int(3), Int(5))).to_pos_neg(),
         Negative(Plus(Int(3), Negative(Int(5)))))
     self.assertEqual(
         Times(Minus(Int(3), Int(5)), Minus(Int(6), Int(7))).to_pos_neg(),
         Times(Plus(Int(3), Negative(Int(5))), Plus(Int(6),
                                                    Negative(Int(7)))))
     self.assertEqual(
         Plus(Minus(Int(3), Int(5)), Minus(Int(6), Int(7))).to_pos_neg(),
         Plus(Plus(Int(3), Negative(Int(5))), Plus(Int(6),
                                                   Negative(Int(7)))))
     self.assertEqual(
         Equals(Minus(Int(3), Int(5)), Minus(Int(6), Int(7))).to_pos_neg(),
         Equals(Plus(Int(3), Negative(Int(5))),
                Plus(Int(6), Negative(Int(7)))))
Example #16
0
def check_q_table(expr, pairs):
    table = dict(pairs)
    return lambda a: a == table


questions = [
    Question(
        "Make a dictionary of 'k * 4' for k = 1, 2, 3, 8, 40, and 70",
        check_q_table(Times(Var("k"), Int(4)),
                      [[Int(1), Int(4)], [Int(2), Int(8)],
                       [Int(3), Int(12)], [Int(8), Int(32)],
                       [Int(40), Int(160)], [Int(70), Int(280)]])),
    Question(
        "Make a dictionary of '(6 * m) - 3' for m = 1, 2, 3, 6, 50, and 100",
        check_q_table(Minus(Times(Int(6), Var("m")), Int(3)),
                      [[Int(1), Int(3)], [Int(2), Int(9)],
                       [Int(3), Int(15)], [Int(6), Int(33)],
                       [Int(50), Int(297)], [Int(100), Int(597)]])),
    Question(
        "Make a dictionary of '21 + (3 * r)' for r = 1, 2, 3, 7, 20, and 50",
        check_q_table(Plus(Int(21), Times(Int(3), Var("r"))),
                      [[Int(1), Int(24)], [Int(2), Int(27)],
                       [Int(3), Int(30)], [Int(7), Int(42)],
                       [Int(20), Int(81)], [Int(50), Int(171)]])),
    Question(
        "Make a dictionary of '2 * (45 - w)' for k = 1, 2, 3, 15, 20, and 35",
        check_q_table(Plus(Int(21), Times(Int(3), Var("r"))),
                      [[Int(1), Int(88)], [Int(2), Int(86)],
                       [Int(3), Int(84)], [Int(15), Int(60)],
                       [Int(20), Int(50)], [Int(35), Int(20)]]))
]
Example #17
0
    "expr.associate_r_to_l(): Converts an expression that looks like 'a + (b + c)' to '(a + b) + c'.  You can replace '+' with '*'",
    "expr.rw_se(sub_expr, equal_expr): Rewrites every expression equal to subexpr inside of expr to equal_expr, but only if equal_expr means the same thing as sub_expr.",
    "expr.commute(): If expr looks like 'a + b', it is converted to 'b + a'.  You can replace '+' with '*'"
]


def check_answer(a0):
    return lambda a: a == a0


def conv_q(expr1_str, expr2_str, af):
    return Question(
        "Convert the expression '{}' to '{}'".format(expr1_str, expr2_str), af)


questions = [
    #conv_q("1 + (2 + 3)", "1 + (3 + 2)", check_answer(Plus(Int(1), Plus(Int(3), Int(2))))),
    #conv_q("(2 * 3) * a", "(c * b) * a", check_answer(Times(Times(Var("c"), Var("b")), Var("a")))),
    conv_q("(1 * 2) + (2 * 3)", "(2 * 1) + (3 * 2)",
           check_answer(Plus(Times(Int(2), Int(1)), Times(Int(3), Int(2))))),
    conv_q("1 + ((2 + 3) + 4)", "1 + ((3 + 4) + 2)",
           check_answer(Plus(Int(1), Plus(Plus(Int(3), Int(4)), Int(2))))),
    conv_q("(1 - 3) - 2", "(1 - 2) - 3",
           check_answer(Minus(Minus(Int(1), Int(2)), Int(3)))),
    conv_q("1 + (2 + 3)", "3 + (2 + 1)",
           check_answer(Plus(Int(3), Plus(Int(2), Int(1))))),
    conv_q("(-3 - 2) - 1", "(-1 - 2) - 3",
           check_answer(Minus(Minus(Negative(Int(1)), Int(2)), Int(3)))),
    conv_q("1 + (2 + (3 + 4))", "4 + (3 + (2 + 1))",
           check_answer(Plus(Int(4), Plus(Int(3), Plus(Int(2), Int(1))))))
]
Example #18
0
from henri_4_8_21 import questions, documentation
from QuestionMachine import QuestionMachine
from Expression import Int, Var, Plus, Minus, Times, Equals, Negative

qm = QuestionMachine(questions)

qm.ask()

expr = Plus(Var("n"), Var("n"))
qm.check(expr.combine_like_terms(Var("n")))
qm.check(Plus(Int(1), Int(1)).combine_like_terms(Int(1)))
expr = Plus(Var("n"), Plus(Var("n"), Var("n")))
qm.check(expr.combine_like_terms(Var("n")))
expr = Plus(Plus(Var("n"), Var("n")), Var("n"))
qm.check(expr.combine_like_terms(Var("n")))
qm.check(Plus(Var("a"), Times(Int(4), Var("a"))).combine_like_terms(Var("a")))
expr = Plus(Var("a"), Times(Var("a"), Int(4)))
qm.check(expr.sub_comm(Times(Var("a"), Int(4))).combine_like_terms(Var("a")))
expr = Plus(Times(Int(3), Var("a")), Times(Var("a"), Int(4)))
qm.check(expr.sub_comm(Times(Var("a"), Int(4))).combine_like_terms(Var("a")))
expr = Plus(Plus(Int(3), Var("a")), Times(Var("a"), Int(4)))
qm.check(
    expr.sub_comm(Times(Var("a"), Int(4))).alr().combine_like_terms(Var("a")))
from Question import Question
from Expression import Int, Plus, Equals, Var

questions = [
    Question("How do you write '1 = 1' using Equals?",
             lambda a: a == Equals(Int(1), Int(1))),
    Question("Is 1 = 1 True or False?", lambda a: a),
    Question(
        "How do you write '(1 + 2) + 3 = 1 + (2 + 3)' using Equals?",
        lambda a: a == Equals(Plus(Plus(Int(1), Int(2)), Int(3)),
                              Plus(Int(1), Plus(Int(2), Int(3))))),
    Question("Is (1 + 2) + 3 = 1 + (2 + 3) True or False?", lambda a: a),
    Question("How do you write 'x' using Var?", lambda a: a == Var("x")),
    Question("How do you write 'y' using Var?", lambda a: a == Var("y")),
    Question("How do you write 'y = x + 19' using Var?",
             lambda a: a == Equals(Var("y"), Plus(Var("x"), Int(19)))),
    Question("If a = 1, what is another way of writing 'a'?",
             lambda a: a == Int(1)),
    Question("If a = 1, what is another way of writing 'b'?",
             lambda a: a == Var("b")),
    Question("If x = 3, what is another way of writing 'x + y?'",
             lambda a: a == Plus(Int(3), Var("y"))),
    Question("Use substitute to make '11 + a = 17' True",
             lambda a: a == Equals(Plus(Int(11), Int(6)), Int(17))),
    Question("Use substitute to make '30 + 50 = x' True",
             lambda a: a == Equals(Plus(Int(30), Int(50)), Int(80)))
]