-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
32 lines (22 loc) · 879 Bytes
/
solver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from itertools import chain, combinations
"""
This module finds all combinations of variables that satisfy
a boolean expression.
It does this by brute force. It computes the powerset of
variables (just as if you were constructing an explicit
truth table on a chalkboard), then evaluates the expression
for each possible subset of variables being assigned to True.
"""
def powerset(s):
return chain.from_iterable(combinations(s, r) for r in range(len(s) + 1))
def braced(s):
return "{" + s + "}"
def stringify_solutions(solutions):
sorted_solutions = sorted(",".join(sorted(s)) for s in solutions)
return "".join(braced(s) for s in sorted_solutions)
def solutions(expr, variables):
"""
solutions(x | y, {"x", "y", "z"}) ==
"{x}{x,y}{x,y,z}{x,z}{y}{y,z}",
"""
return stringify_solutions(s for s in powerset(variables) if expr.eval(s))