Пример #1
0
        False
        >>> w2 = WordLadderPuzzle("yo", "yo", {"yi", "yo"})
        >>> w2.is_solved()
        True
        """
        return self._from_word == self._to_word


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    from puzzle_tools import breadth_first_solve, depth_first_solve
    from time import time

    with open("words", "r") as words:
        word_set = set(words.read().split())
    w = WordLadderPuzzle("same", "cost", word_set)
    start = time()
    sol = breadth_first_solve(w)
    end = time()
    print("Solving word ladder from same->cost")
    print("...using breadth-first-search")
    print("Solutions: {} took {} seconds.".format(sol, end - start))
    start = time()
    sol = depth_first_solve(w)
    end = time()
    print("Solving word ladder from same->cost")
    print("...using depth-first-search")
    print("Solutions: {} took {} seconds.".format(sol, end - start))
Пример #2
0
        """
        Return whether MNPuzzle self is solved.

        @type self: MNPuzzle
        @rtype: bool
        """
        return self.from_grid == self.to_grid


if __name__ == "__main__":
    import doctest
    doctest.testmod()
    target_grid = (
        ("1", "2", "3", '4'),
        ("5", "6", "7", "*"),
    )
    start_grid = (
        ("4", "1", "6", "7"),
        ("3", "*", "2", "5"),
    )
    from puzzle_tools import breadth_first_solve, depth_first_solve
    from time import time
    start = time()
    solution = breadth_first_solve(MNPuzzle(start_grid, target_grid))
    end = time()
    print("BFS solved: \n\n{} \n\nin {} seconds".format(solution, end - start))
    start = time()
    solution = depth_first_solve((MNPuzzle(start_grid, target_grid)))
    end = time()
    print("DFS solved: \n\n{} \n\nin {} seconds".format(solution, end - start))
Пример #3
0
    # a configuration is solved when from_grid is the same as to_grid


    # override extensions
    # legal extensions are configurations that can be reached by swapping one
    # symbol to the left, right, above, or below "*" with "*"

    # TODO



if __name__ == "__main__":
    import doctest
    doctest.testmod()
    target_grid = (("1", "2", "3"), ("4", "5", "*"))
    start_grid = (("*", "2", "3"), ("1", "4", "5"))
    from puzzle_tools import breadth_first_solve, depth_first_solve
    from time import time
    start = time()
    solution = breadth_first_solve(MNPuzzle(start_grid, target_grid))
    test = MNPuzzle(start_grid, target_grid)
    end = time()
    print("BFS solved: \n\n{} \n\nin {} seconds".format(
        solution, end - start))
    start = time()
    solution = depth_first_solve((MNPuzzle(start_grid, target_grid)))
    end = time()
    print("DFS solved: \n\n{} \n\nin {} seconds".format(
        solution, end - start))

Пример #4
0
        >>> b = WordLadderPuzzle("same", "same", word_set)
        >>> b.is_solved()
        True
        """
        # Check that from and to word are the same and that both are legal words
        return self._from_word == self._to_word and \
               (self._from_word and self._from_word) in self._word_set


if __name__ == '__main__':
    import doctest
    doctest.testmod()

    from puzzle_tools import breadth_first_solve, depth_first_solve
    from time import time
    with open("words", "r") as words:
        word_set = set(words.read().split())
    w = WordLadderPuzzle("same", "cost", word_set)
    start = time()
    sol = breadth_first_solve(w)
    end = time()
    print("Solving word ladder from same->cost")
    print("...using breadth-first-search")
    print("Solutions: {} took {} seconds.".format(sol, end - start))
    start = time()
    sol = depth_first_solve(w)
    end = time()
    print("Solving word ladder from same->cost")
    print("...using depth-first-search")
    print("Solutions: {} took {} seconds.".format(sol, end - start))
    sol = depth_first_solve(s)
    while sol.children:
        sol = sol.children[0]
    end = time()
    print("time to solve 9x9 using depth_first: {} seconds\n".format(end -
                                                                     start))
    print(sol)

    s = SudokuPuzzle(9, [
        "5", "6", "*", "*", "*", "7", "*", "*", "9", "*", "7", "*", "*", "4",
        "8", "*", "3", "1", "*", "*", "*", "*", "*", "*", "*", "*", "*", "4",
        "3", "*", "*", "*", "*", "*", "*", "*", "*", "8", "*", "*", "*", "*",
        "*", "9", "*", "*", "*", "*", "*", "*", "*", "*", "2", "6", "*", "*",
        "*", "*", "*", "*", "*", "*", "*", "1", "9", "*", "3", "6", "*", "*",
        "7", "*", "7", "*", "*", "1", "*", "*", "*", "4", "2"
    ], {"1", "2", "3", "4", "5", "6", "7", "8", "9"})

    print("solving 4-star sudoku from \"That's Puzzling\", "
          "November 14th 2015\n\n{}\n\n".format(s))
    start = time()
    sol = depth_first_solve(s)

    while sol.children:
        sol = sol.children[0]
    end = time()
    print("time to solve 9x9 using depth_first: {} seconds\n".format(end -
                                                                     start))
    sol2 = breadth_first_solve(s)
    print(sol2)
    print(sol)