0
0
PHPprogramming~20 mins

DNF types (Disjunctive Normal Form) in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DNF Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of PHP code with DNF type evaluation

What is the output of this PHP code that evaluates a simple DNF expression?

PHP
<?php
function evaluateDNF(array $clauses, array $values): bool {
    foreach ($clauses as $clause) {
        $clauseResult = true;
        foreach ($clause as $var => $negate) {
            $val = $values[$var] ?? false;
            if ($negate) {
                $val = !$val;
            }
            $clauseResult = $clauseResult && $val;
            if (!$clauseResult) {
                break;
            }
        }
        if ($clauseResult) {
            return true;
        }
    }
    return false;
}

$dnf = [
    ['A' => false, 'B' => true],
    ['C' => false]
];
$values = ['A' => true, 'B' => false, 'C' => true];

var_export(evaluateDNF($dnf, $values));
?>
Atrue
B1
Cnull
Dfalse
Attempts:
2 left
💡 Hint

Check each clause and see if any clause evaluates to true.

🧠 Conceptual
intermediate
1:30remaining
Understanding DNF clause structure in PHP arrays

In PHP, a DNF formula is represented as an array of clauses, where each clause is an associative array with variable names as keys and booleans indicating negation as values. Which of the following best describes the meaning of the boolean value in each clause?

ATrue means the variable is set to true, false means set to false
BTrue means the variable is included, false means it is excluded from the clause
CTrue means the variable is negated, false means it is not negated
DTrue means the variable is optional, false means mandatory
Attempts:
2 left
💡 Hint

Think about how negation is represented in boolean logic.

🔧 Debug
advanced
2:00remaining
Identify the error in DNF evaluation function

Given the following PHP function to evaluate a DNF formula, what error will occur when running it?

PHP
<?php
function evalDNF(array $dnf, array $vals): bool {
    foreach ($dnf as $clause) {
        $result = true;
        foreach ($clause as $var => $neg) {
            $val = $vals[$var];
            if ($neg) {
                $val = !$val;
            }
            $result = $result && $val;
        }
        if ($result) {
            return true;
        }
    }
    return false;
}

$dnf = [['X' => false]];
$vals = [];
evalDNF($dnf, $vals);
?>
AUndefined index notice for 'X'
BTypeError due to invalid argument
CReturns false without error
DSyntax error due to missing semicolon
Attempts:
2 left
💡 Hint

Check what happens when accessing an array key that does not exist.

📝 Syntax
advanced
1:30remaining
Which PHP code snippet correctly defines a DNF clause?

Which of the following PHP code snippets correctly defines a DNF clause representing (A OR NOT B)?

A$clause = ['A' => true, 'B' => false];
B$clause = ['A' => false, 'B' => true];
C$clause = ['A' => 1, 'B' => 0];
D$clause = ['A' => 'false', 'B' => 'true'];
Attempts:
2 left
💡 Hint

Recall that true means negated, false means not negated.

🚀 Application
expert
2:30remaining
Count how many clauses evaluate to true in a DNF formula

Given the PHP function below and the inputs, what is the number of clauses that evaluate to true?

PHP
<?php
function countTrueClauses(array $dnf, array $vals): int {
    $count = 0;
    foreach ($dnf as $clause) {
        $clauseTrue = true;
        foreach ($clause as $var => $neg) {
            $val = $vals[$var] ?? false;
            if ($neg) {
                $val = !$val;
            }
            if (!$val) {
                $clauseTrue = false;
                break;
            }
        }
        if ($clauseTrue) {
            $count++;
        }
    }
    return $count;
}

$dnf = [
    ['A' => false, 'B' => true],
    ['B' => false, 'C' => false],
    ['A' => true, 'C' => true]
];
$vals = ['A' => true, 'B' => false, 'C' => true];

echo countTrueClauses($dnf, $vals);
?>
A2
B1
C3
D0
Attempts:
2 left
💡 Hint

Evaluate each clause carefully with given values and negations.