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)))))
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)))
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")))
def check_answer(a0): 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
from Expression import Int, Plus, Equals, Var, Minus, Times def check_q(i): return lambda a: a == i 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)]])),
"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)))))) ]
"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)))))) ]
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)], [Int(3), Int(30)], [Int(7), Int(42)], [Int(20), Int(81)], [Int(50), Int(171)]])), eval_in_for("45 - w", "2 * (45 - w)", "w", 1, Times(Int(2), Int(44))), eval_in("2 * 44", "2 * 44", Int(88)), eval_in_for("45 - w", "2 * (45 - w)", "w", 2, Plus(Int(21), Int(43))), eval_in("2 * 43", "2 * 43", Int(27)), eval_in_for("45 - w", "2 * (45 - w)", "w", 3, Plus(Int(21), Int(42))), eval_in("2 * 42", "2 * 42", Int(30)), eval_in_for("45 - w", "2 * (45 - w)", "w", 15, Plus(Int(21), Int(30))), eval_in("2 * 30", "2 * 30", Int(42)), eval_in_for("45 - w", "2 * (45 - w)", "w", 20, Plus(Int(21), Int(25))), eval_in("2 * 25", "2 * 25", Int(81)), eval_in_for("45 - w", "2 * (45 - w)", "w", 35, Plus(Int(21), Int(10))), eval_in("2 * 10", "2 * 10", Int(171)),
"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"))))), ]
"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)))) ]