Skip to content

Commit

Permalink
Formatting cleanups.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwg4 committed Nov 15, 2023
1 parent 92066c6 commit f335125
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 49 deletions.
3 changes: 2 additions & 1 deletion tests/bruteforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

import numpy as np


# from the itertools module documentation
def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
return chain.from_iterable(combinations(s, r) for r in range(len(s) + 1))


def bruteforce(data):
Expand Down
70 changes: 38 additions & 32 deletions tests/problems.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# |xx| | |
# +--+--+--+


# this problem has 2 solutions
# (5, 13) and (6, 12)
def small_trimino_problem():
Expand All @@ -45,79 +46,84 @@ def small_trimino_problem():
solution_count=2,
)


def small_trimino_problem_from_file():
return dict(
data=np.load("tests/files/small_trimino_problem.npy"),
solution1=[5, 13],
solution_count=2,
)


# https://en.wikipedia.org/wiki/Exact_cover#Detailed_example
def detailed_wikipedia_problem():
sets = [
{1, 4, 7},
{1, 4}, # <- 1
{1, 4}, # <- 1
{4, 5, 7},
{3, 5, 6}, # <- 3
{3, 5, 6}, # <- 3
{2, 3, 6, 7},
{2, 7}, # <- 5
{2, 7}, # <- 5
]
return dict(
data=np.array(
[[1 if i in s else 0 for i in range(1, 8)] for s in sets],
dtype=DTYPE_FOR_ARRAY),
dtype=DTYPE_FOR_ARRAY,
),
solution1=[1, 3, 5],
solution_count=1,
)


def bruteforce_problem1():
to_cover = [
[1, 0, 0, 1, 0, 0, 1, 0], # <- sol1
[0, 1, 0, 0, 1, 0, 0, 1], # <- sol1
[0, 0, 1, 0, 0, 1, 0, 0], # <- sol1
[0, 0, 0, 1, 0, 0, 0, 0], # <- sol2
[1, 0, 1, 0, 1, 0, 0, 1], # <- sol2
[0, 1, 0, 0, 0, 1, 1, 0], # <- sol2

[1, 0, 0, 1, 0, 0, 1, 0], # <- sol1
[0, 1, 0, 0, 1, 0, 0, 1], # <- sol1
[0, 0, 1, 0, 0, 1, 0, 0], # <- sol1
[0, 0, 0, 1, 0, 0, 0, 0], # <- sol2
[1, 0, 1, 0, 1, 0, 0, 1], # <- sol2
[0, 1, 0, 0, 0, 1, 1, 0], # <- sol2
]
return dict(
data=np.array(to_cover, dtype=DTYPE_FOR_ARRAY),
solution1=[0, 1, 2],
solution_count=2,
)


def bruteforce_problem2():
to_cover = [
[1, 0, 0, 1, 0, 0, 1, 0], # <- sol1
[0, 1, 0, 0, 1, 0, 0, 1], # <- sol1
[0, 0, 1, 0, 0, 1, 0, 0], # <- sol1
[0, 0, 0, 1, 0, 0, 0, 0], # <- sol2
[1, 0, 1, 0, 1, 0, 0, 1], # <- sol2
[0, 1, 0, 0, 0, 1, 1, 0], # <- sol2
[1, 0, 0, 1, 0, 0, 1, 0], # <- sol1
[0, 1, 0, 0, 1, 0, 0, 1], # <- sol1
[0, 0, 1, 0, 0, 1, 0, 0], # <- sol1
[1, 0, 0, 1, 0, 0, 1, 0], # <- sol1
[0, 1, 0, 0, 1, 0, 0, 1], # <- sol1
[0, 0, 1, 0, 0, 1, 0, 0], # <- sol1
[0, 0, 0, 1, 0, 0, 0, 0], # <- sol2
[1, 0, 1, 0, 1, 0, 0, 1], # <- sol2
[0, 1, 0, 0, 0, 1, 1, 0], # <- sol2
[1, 0, 0, 1, 0, 0, 1, 0], # <- sol1
[0, 1, 0, 0, 1, 0, 0, 1], # <- sol1
[0, 0, 1, 0, 0, 1, 0, 0], # <- sol1
]
return dict(
data=np.array(to_cover, dtype=DTYPE_FOR_ARRAY),
solution1=[0, 1, 2],
solution_count=9,
)


def bruteforce_problem3():
to_cover = [
[1, 0, 0, 1, 0, 0, 1, 0], # <- sol1
[0, 1, 0, 0, 1, 0, 0, 1], # <- sol1
[0, 0, 1, 0, 0, 1, 0, 0], # <- sol1
[0, 0, 0, 1, 0, 0, 0, 0], # <- sol2
[1, 0, 1, 0, 1, 0, 0, 1], # <- sol2
[0, 1, 0, 0, 0, 1, 1, 0], # <- sol2
[1, 0, 0, 1, 0, 0, 1, 0], # <- sol1
[0, 1, 0, 0, 1, 0, 0, 1], # <- sol1
[0, 0, 1, 0, 0, 1, 0, 0], # <- sol1
[0, 0, 0, 1, 0, 0, 0, 0], # <- sol2
[1, 0, 1, 0, 1, 0, 0, 1], # <- sol2
[0, 1, 0, 0, 0, 1, 1, 0], # <- sol2
[1, 0, 0, 1, 0, 0, 1, 0], # <- sol1
[0, 1, 0, 0, 1, 0, 0, 1], # <- sol1
[0, 0, 1, 0, 0, 1, 0, 0], # <- sol1
[0, 0, 0, 1, 0, 0, 0, 0], # <- sol2
[1, 0, 1, 0, 1, 0, 0, 1], # <- sol2
[0, 1, 0, 0, 0, 1, 1, 0], # <- sol2
[1, 0, 0, 1, 0, 0, 1, 0], # <- sol1
[0, 1, 0, 0, 1, 0, 0, 1], # <- sol1
[0, 0, 1, 0, 0, 1, 0, 0], # <- sol1
[0, 0, 0, 1, 0, 0, 0, 0], # <- sol2
[1, 0, 1, 0, 1, 0, 0, 1], # <- sol2
[0, 1, 0, 0, 0, 1, 1, 0], # <- sol2
]
return dict(
data=np.array(to_cover, dtype=DTYPE_FOR_ARRAY),
Expand Down
30 changes: 19 additions & 11 deletions tests/test_solution_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .problems import detailed_wikipedia_problem
from .problems import bruteforce_problem1, bruteforce_problem2, bruteforce_problem3


def test_solution_count():
data = np.array([[1, 0, 0], [0, 1, 0], [0, 1, 1], [0, 0, 1]], dtype=DTYPE_FOR_ARRAY)
result = get_solution_count(data)
Expand All @@ -20,36 +21,43 @@ def test_solution_count_single_solution():


def test_solution_count_no_solutions():
data = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 1, 1, 0], [0, 0, 1, 0]], dtype=DTYPE_FOR_ARRAY)
data = np.array(
[[1, 0, 0, 0], [0, 1, 0, 0], [0, 1, 1, 0], [0, 0, 1, 0]], dtype=DTYPE_FOR_ARRAY
)
result = get_solution_count(data)
assert result == 0


def test_small_trimino_problem():
problem = small_trimino_problem()
data = problem['data']
data = problem["data"]
result = get_solution_count(data)
assert result == problem['solution_count']
assert result == problem["solution_count"]


def test_detailed_wikipedia_problem():
problem = detailed_wikipedia_problem()
data = problem['data']
data = problem["data"]
result = get_solution_count(data)
assert result == problem['solution_count']
assert result == problem["solution_count"]


def test_bruteforce_problem1():
problem = bruteforce_problem1()
data = problem['data']
data = problem["data"]
result = get_solution_count(data)
assert result == problem['solution_count']
assert result == problem["solution_count"]


def test_bruteforce_problem2():
problem = bruteforce_problem2()
data = problem['data']
data = problem["data"]
result = get_solution_count(data)
assert result == problem['solution_count']
assert result == problem["solution_count"]


def test_bruteforce_problem3():
problem = bruteforce_problem3()
data = problem['data']
data = problem["data"]
result = get_solution_count(data)
assert result == problem['solution_count']
assert result == problem["solution_count"]
13 changes: 8 additions & 5 deletions tests/test_trimino_based.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
# the exact cover matrix built manually
small_trimino_problem,
# the same store in a file
small_trimino_problem_from_file)
small_trimino_problem_from_file,
)


def input1():
return small_trimino_problem()['data']
return small_trimino_problem()["data"]


def input2():
return small_trimino_problem_from_file()['data']
return small_trimino_problem_from_file()["data"]


def test_inputs_are_equal():
m1 = input1()
Expand All @@ -36,10 +39,10 @@ def run_on_input(array, expected):


def test_input1():
expected = small_trimino_problem()['solution1']
expected = small_trimino_problem()["solution1"]
run_on_input(input1(), expected)


def test_input2():
expected = small_trimino_problem_from_file()['solution1']
expected = small_trimino_problem_from_file()["solution1"]
run_on_input(input2(), expected)

0 comments on commit f335125

Please sign in to comment.