当前位置: 首页>>代码示例>>Python>>正文


Python Sudoku.is_consistent方法代码示例

本文整理汇总了Python中sudoku.Sudoku.is_consistent方法的典型用法代码示例。如果您正苦于以下问题:Python Sudoku.is_consistent方法的具体用法?Python Sudoku.is_consistent怎么用?Python Sudoku.is_consistent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sudoku.Sudoku的用法示例。


在下文中一共展示了Sudoku.is_consistent方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_sudoku_class

# 需要导入模块: from sudoku import Sudoku [as 别名]
# 或者: from sudoku.Sudoku import is_consistent [as 别名]

#.........这里部分代码省略.........
    puzzles_inconsistent.append(
        "005079003"  # first box inconsistent
        "280000000"
        "348000000"
        "050680000"
        "070204080"
        "000013020"
        "000000471"
        "000000006"
        "800790300"
    )
    puzzles_inconsistent.append(
        "005079003"  # middle box inconsistent
        "200000000"
        "348000000"
        "050680000"
        "070264080"
        "000013020"
        "000000471"
        "000000006"
        "800790300"
    )
    puzzles_inconsistent.append(
        "005079003"  # last row inconsistent
        "200000000"
        "348000000"
        "050680000"
        "070204080"
        "000013020"
        "000000471"
        "000000006"
        "800790308"
    )
    puzzles_inconsistent.append(
        "005079003"  # last col inconsistent
        "200000000"
        "348000000"
        "050680000"
        "070204080"
        "000013026"
        "000000471"
        "000000006"
        "800790300"
    )

    srtestpuzzle = (  # puzzle for the subregion test
        "665679663" "266666666" "348666666" "656686666" "676264686" "666613626" "666666471" "666666666" "866796366"
    )

    for puzzle in puzzles_corrupt:  # corrupt puzzles test
        try:
            Sudoku(instr=puzzle)
        except SudokuInputError:
            pass
        else:
            raise Exception("Bad input accepted")

    for incons in puzzles_inconsistent:  # inconsistent puzzles test
        sudoku = Sudoku(instr=incons)
        sudoku.solve()
        assert not sudoku.is_consistent()

    sudoku = Sudoku(instr=validpuzzle)  # OK, let's see a valid puzzle
    print sudoku
    sudoku.solve()
    print sudoku
    assert sudoku.is_consistent()
    print "human readable: \n{!s}".format(sudoku)
    print "repr: \n {!r}".format(sudoku)
    print "sudoku.regions: \n{}".format(sudoku.regions)

    print "OK. Let's see how fast we can solve some puzzle collections."

    # 50 easy, 95 hard
    benchmarklist = [
        ("50 puzzles from Project Euler", "puzzles/euler_puzzles_50.txt", "puzzles/euler_solutions_50.txt"),
        ("95 hard puzzles", "puzzles/hard_puzzles_95.txt", "puzzles/hard_solutions_95.txt"),
    ]

    # 5 hard
    # benchmarklist = [("5 hard puzzles", "puzzles/hard_puzzles_5.txt", "puzzles/hard_solutions_5.txt") ] # for quick profiling

    # 50 easy
    # benchmarklist = [ ("50 puzzles from Project Euler", "puzzles/euler_puzzles_50.txt", "puzzles/euler_solutions_50.txt")]

    for collection_name, path_puzzle, path_solution in benchmarklist:
        print collection_name
        with open(path_puzzle) as puzzles, open(path_solution, "w") as solutions:
            before = time.clock()
            collection = SudokuCollection(puzzles)
            collection.solve_all(solutions, verbose=True)  # TODO make verbosity a command line parameter
            puzzleno = len(collection.sudokus)  # number of sudokus, TODO: implement API
            after = time.clock()
            elapsed = after - before
            average = elapsed / puzzleno
            print "Solving {!s} puzzles took {!s} secs, avg: {!s} sec".format(
                puzzleno, elapsed, average
            )  # TODO check results
    print "Tests succesful!"
    return True
开发者ID:larion,项目名称:sudoku,代码行数:104,代码来源:sudoku_tests.py


注:本文中的sudoku.Sudoku.is_consistent方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。