The problem
A formula has variables x₁ … xₙ and a list of clauses. A clause is a list of literals: k means “xₖ is True” and -k means “xₖ is False”, and a clause holds when at least one of its literals holds. You are given a helper sat(formula) that answers only True or False: can some assignment make every clause of formula hold?
Return an assignment as a list of n values, answer[k - 1] for xₖ, that makes every clause hold, or None if there is none. Call sat at most n + 1 times.
find_assignment(2, [[1, 2], [-1]], sat) # [False, True]
find_assignment(1, [[1], [-1]], sat) # None
Each test reports two facts: whether your answer is right ('satisfied', or 'no assignment' for None), and whether sat was called at most n + 1 times. Any right assignment passes.
Limits
- Up to 60 variables, so trying all 2ⁿ assignments (n twos multiplied together) is impossible.
- Give
sata list of clauses, each a list of literals from 1 to n or −1 to −n.
Builds on HARD-1 P, NP and NP-complete.
Continue on a laptop
Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.
Need a hint?0 of 3 opened
- Hint 1Nudge
- Hint 2Approach Open hint 1 first
- Hint 3Pseudocode Open hint 2 first
- Worked solutionlast resort Opens after all 3 hints
Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.
| # | Test | Result | Reads · time |
|---|---|---|---|
| 1 | One variable | Not run | |
| 2 | It must be False | Not run | |
| 3 | No answer | Not run | |
| 4 | No clauses | Not run | |
| 5 | A chain of rules | Not run | |
| 6 | The figure's formula | Not run | |
| 7 | Every clause on three variables | Not run | |
| 8 | Choices that depend on each other | Not run | |
| 9 | Twenty variables | Not run | |
| 10 | Sixty variables | Not run | |
| 11 | Sixty variables, no answer | Not run |