Complete the code to define a simple DNF clause array.
<?php
$clause = [[1]];
?>The clause in DNF is a conjunction (AND) of literals, so 'A && B' is correct.
Complete the code to represent a DNF formula as an array of clauses.
<?php
$dnf = [
['A', 'B'],
[1]
];
?>Each clause is an array of literals combined with AND. So ['!A', 'C'] is a valid clause.
Fix the error in the code to correctly check if a literal is negated.
<?php $literal = '!A'; $isNegated = [1]; ?>
To check if the literal starts with '!', strpos must return 0 (start of string).
Fill both blanks to create a function that returns true if a clause contains a literal.
<?php
function containsLiteral(array $clause, string $literal): bool {
return in_array([1], [2]);
}
?>The function checks if the literal is in the clause array using in_array.
Fill all three blanks to build a DNF formula and check if it contains a negated literal.
<?php
$dnf = [
['A', 'B'],
['!C', 'D']
];
foreach ($dnf as $clause) {
foreach ($clause as [1]) {
if (strpos([2], '!') === 0) {
echo [3] . " is negated\n";
}
}
}
?>We loop over each literal in a clause, check if it starts with '!', and print the literal.