0
0
PHPprogramming~10 mins

DNF types (Disjunctive Normal Form) in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a simple DNF clause array.

PHP
<?php
$clause = [[1]];
?>
Drag options to blanks, or click blank then click option'
A'A && B'
B'A || B'
C'A && !B'
D'!A || !B'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '||' instead of '&&' inside a clause.
2fill in blank
medium

Complete the code to represent a DNF formula as an array of clauses.

PHP
<?php
$dnf = [
    ['A', 'B'],
    [1]
];
?>
Drag options to blanks, or click blank then click option'
A['A && C']
B['A || C']
C['!A', 'C']
D['!A || C']
Attempts:
3 left
💡 Hint
Common Mistakes
Using combined strings with '||' or '&&' inside a clause.
3fill in blank
hard

Fix the error in the code to correctly check if a literal is negated.

PHP
<?php
$literal = '!A';
$isNegated = [1];
?>
Drag options to blanks, or click blank then click option'
Astrpos($literal, '!') === 0
Bstrpos($literal, '!') == 1
Cstrpos($literal, '!') === false
Dstrpos($literal, '!') > 0
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for position 1 or greater instead of 0.
4fill in blank
hard

Fill both blanks to create a function that returns true if a clause contains a literal.

PHP
<?php
function containsLiteral(array $clause, string $literal): bool {
    return in_array([1], [2]);
}
?>
Drag options to blanks, or click blank then click option'
A$literal
B$clause
C$literal[0]
D$clause[0]
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the arguments or using incorrect variables.
5fill in blank
hard

Fill all three blanks to build a DNF formula and check if it contains a negated literal.

PHP
<?php
$dnf = [
    ['A', 'B'],
    ['!C', 'D']
];

foreach ($dnf as $clause) {
    foreach ($clause as [1]) {
        if (strpos([2], '!') === 0) {
            echo [3] . " is negated\n";
        }
    }
}
?>
Drag options to blanks, or click blank then click option'
A$literal
B$clause
D$clause[0]
Attempts:
3 left
💡 Hint
Common Mistakes
Using the clause array instead of the literal variable inside the loop.