def test_expression_tree_puzzle_fail_fast_false() -> None: """Test ExpressionTreePuzzle.fail_fast on a solvable puzzle.""" exp_t = ExprTree('+', [ExprTree('a', []), ExprTree('b', [])]) puz = ExpressionTreePuzzle(exp_t, 7) puz.variables['a'] = 2 assert puz.fail_fast() is False
def test_expression_tree_puzzle_is_solved_doctest() -> None: """Test ExpressionTreePuzzle.is_solved on the provided doctest""" exp_t = ExprTree('+', [ExprTree('a', []), ExprTree('b', [])]) puz = ExpressionTreePuzzle(exp_t, 7) assert puz.is_solved() is False puz.variables['a'] = 7 assert puz.is_solved() is False puz.variables['a'] = 5 puz.variables['b'] = 2 assert puz.is_solved() is True
def test_expression_tree_puzzle_str_doctest() -> None: """Test ExpressionTreePuzzle.__str__ on the provided doctest""" exp_t = ExprTree('+', [ExprTree('*', [ExprTree('a', []), ExprTree('+', [ExprTree('b', []), ExprTree(6, []), ExprTree(6, []), ])]), ExprTree(5, [])]) puz = ExpressionTreePuzzle(exp_t, 61) assert str(puz) == "{'a': 0, 'b': 0}\n((a * (b + 6 + 6)) + 5) = 61"
def test_et_dfs_solver_example() -> None: """Test DfsSolver.solve on a ExpressionTreePuzzle.""" # This SudokuPuzzle is a more filled-in version of the one in the # example from the handout. example = construct_from_list([["+"], ["*", "*"], ["a", "a"], ["c", 6]]) s = ExpressionTreePuzzle(example, 51) solver = BfsSolver() flag = True actual = solver.solve(s)[-1] flag = False assert flag is False
def _setup_puzzle(self) -> None: """ Generate a random expression tree puzzle and create the image for it. This helper is called inside the __init__ and creates several of the private instance attributes. It is called again any time the puzzle to be solved is changed (the new button is pressed). """ self._tree, lookup = generate_random_expression_tree() for k in lookup: lookup[k] = randint(1, 9) target = self._tree.eval(lookup) for k in lookup: lookup[k] = 0 self._puzzle = ExpressionTreePuzzle(self._tree, target) self._redraw_puzzle()
def test_expression_tree_puzzle_extensions_doctest() -> None: """Test ExpressionTreePuzzle.extensions on the provided doctest""" exp_t = ExprTree('a', []) puz = ExpressionTreePuzzle(exp_t, 7) exts_of_puz = puz.extensions() assert len(exts_of_puz) == 9 exts_of_an_ext = exts_of_puz[0].extensions() assert len(exts_of_an_ext) == 0 exp_t = ExprTree('+', [ExprTree('a', []), ExprTree('b', [])]) puz = ExpressionTreePuzzle(exp_t, 8) exts_of_puz = puz.extensions() assert len(exts_of_puz) == 18
# [["1", " ", " ", "2"], # [" ", " ", " ", "1"], # [" ", " ", " ", " "], # [" ", " ", "2", " "]], # {"1", "2", "3", "4"}), # WordLadderPuzzle("same", "cost"), # WordLadderPuzzle("perked", "manors"), # WordLadderPuzzle("stashes", "cheesed"), # WordLadderPuzzle("clearing", "scuffing"), # ExpressionTreePuzzle(construct_from_list([['*'], ['a', 3]]), 15) # ] tree1 = construct_from_list([['*'], ['a', 3]]) tree2 = construct_from_list([['+'], ['+', '+'], [5, '+'], ['a', 8, 3], ['c', 6, '+'], ['a', 1, 'b']]) puzzles = [ ExpressionTreePuzzle(tree1, 15), ExpressionTreePuzzle(tree2, 44), ExpressionTreePuzzle( construct_from_list([['+'], [3, '*', 'a', '+'], ['a', '+'], ['b', 9], [2, 9]]), 129), ExpressionTreePuzzle( construct_from_list([['+'], [3, '*', 'a', '+'], ['a', '+'], ['b', 'c'], [2, 9]]), 129), ExpressionTreePuzzle( construct_from_list([['+'], [3, '*', 'a', '+'], ['a', '+'], ['b', 'c'], [2, 'd']]), 129) ] headers = ["Puzzle Type", "Solver", "len(sol)", "time"] print("|".join(headers)) print("|".join(["-" * len(header) for header in headers]))