Skip to content

Commit

Permalink
Add a way of calculating order.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwg4 committed Nov 2, 2022
1 parent d199777 commit 23c719d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
31 changes: 31 additions & 0 deletions polyomino/order.py
Original file line number Diff line number Diff line change
@@ -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
54 changes: 54 additions & 0 deletions tests/test_order.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 23c719d

Please sign in to comment.