HARD-P1

An answer from a yes/no helper

Practice 1 of 5Hard problems11 tests

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 sat a 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

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

11 tests, not run yet
Test results
#TestResultReads · time
1One variableNot run
2It must be FalseNot run
3No answerNot run
4No clausesNot run
5A chain of rulesNot run
6The figure's formulaNot run
7Every clause on three variablesNot run
8Choices that depend on each otherNot run
9Twenty variablesNot run
10Sixty variablesNot run
11Sixty variables, no answerNot run