From 23c719da5bc1018e8a5bbca65c52ee80d400975c Mon Sep 17 00:00:00 2001 From: Jack Grahl Date: Tue, 1 Nov 2022 23:11:47 +0000 Subject: [PATCH] Add a way of calculating order. --- polyomino/order.py | 31 ++++++++++++++++++++++++++ tests/test_order.py | 54 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 polyomino/order.py create mode 100644 tests/test_order.py diff --git a/polyomino/order.py b/polyomino/order.py new file mode 100644 index 0000000..7aaf4bc --- /dev/null +++ b/polyomino/order.py @@ -0,0 +1,31 @@ +from polyomino.board import Rectangle +from polyomino.error import CantPlaceSinglePiece + + +def rectangles(size): + """ + >>> list(rectangles(20): + [(1, 20), (2, 10), (4, 5)] + >>> list(rectangles(16): + [(1, 16), (2, 8), (4, 6)] + """ + for i in range(1, size): + if size % i == 0: + yield i, size // i + + +def has_order_greater_than(tile, k): + n = len(tile) + + for i in range(1, k + 1): + for x, y in rectangles(n * i): + board = Rectangle(x, y) + problem = board.tile_with_many(tile) + try: + solution = problem.solve() + if solution: + return False, i, solution.tiling + except CantPlaceSinglePiece: + pass + + return True, None, None diff --git a/tests/test_order.py b/tests/test_order.py new file mode 100644 index 0000000..15a55d2 --- /dev/null +++ b/tests/test_order.py @@ -0,0 +1,54 @@ +from polyomino.order import has_order_greater_than + + +def test_rectangle(): + tile = [(0, 0), (0, 1), (0, 2)] + + predicate, n, tiling = has_order_greater_than(tile, 1) + assert not predicate + assert n == 1 + assert tiling == [tile] + + predicate, n, tiling = has_order_greater_than(tile, 2) + assert not predicate + assert n == 1 + assert tiling == [tile] + + predicate, n, tiling = has_order_greater_than(tile, 3) + assert not predicate + assert n == 1 + assert tiling == [tile] + + +def test_t_tetromino(): + tile = [(0, 0), (0, 1), (0, 2), (1, 1)] + + predicate, n, tiling = has_order_greater_than(tile, 1) + assert predicate + assert n is None + assert tiling is None + + predicate, n, tiling = has_order_greater_than(tile, 2) + assert predicate + assert n is None + assert tiling is None + + predicate, n, tiling = has_order_greater_than(tile, 3) + assert predicate + assert n is None + assert tiling is None + + predicate, n, tiling = has_order_greater_than(tile, 4) + assert not predicate + assert n == 4 + assert len(tiling) == 4 + + predicate, n, tiling = has_order_greater_than(tile, 5) + assert not predicate + assert n == 4 + assert len(tiling) == 4 + + predicate, n, tiling = has_order_greater_than(tile, 6) + assert not predicate + assert n == 4 + assert len(tiling) == 4