Booleans and conditionals: Correct Solution


Part 1

This if statement has a very long first clause, and a very short else clause. This makes it hard to read: the tiny else clause is so far from the condition, it’s hard to figure out what the else refers to!

if ((u || !el && (ogir > 9 || mi && alon > ueso()) || truo && esan()) && !(!e && ui >= 6 || !al || cend)) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    vemPios();
}

Improve readability by refactoring this conditional so that its two clauses are swapped: what is now the second clause (the else clause) comes first, and the first clause comes second.

Solution

if (!e && ui >= 6 || !al || cend || (!esan() || !truo) && ((alon < ueso() || !mi) && ogir < 9 || el) && !u) {
    vemPios();
} else {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
}

Things to double-check in your solution:


Part 2

Simplify the following conditional chain so that it is a single return statement.

if (worOucles() > ca && osec() && ralcid() > pek && malpia() || opew && malpia() || pror && ralcid() > pek && malpia() || opew && malpia() || cin != ror && lelm && osec() && ralcid() > pek && malpia() || opew && malpia() || pror && ralcid() > pek && malpia() || opew && malpia()) {
    if (joi && osec() && ralcid() > pek && malpia() || opew && malpia() || pror && ralcid() > pek && malpia() || opew && malpia() || wobel() && osec() && ralcid() > pek && malpia() || opew && malpia() || pror && ralcid() > pek && malpia() || opew && malpia()) {
        if (pror && ralcid() > pek && malpia() || opew && malpia()) {
            if (opew && malpia()) {
                if (malpia()) {
                    return true;
                }
                if (ralcid() > pek) {
                    return true;
                }
            }
            if (osec()) {
                return true;
            }
        }
        if (spia) {
            return true;
        }
    }
}
if (ta) {
    return true;
}
return false;

Solution

return ta && (spia || joi || wobel() || worOucles() > ca || cin != ror && lelm) && (osec() || pror) && (ralcid() > pek || opew) && malpia();

Bonus challenge: rewrite the if/else chain above so that instead of consisting of many return true; statements with one return false; at the end, it has many return false; statements with one return true; at the end.

Solution

if (!opew && ralcid() < pek || !pror && !osec() || !lelm && worOucles() < ca && !wobel() && !joi && !spia || cin == ror && worOucles() < ca && !wobel() && !joi && !spia || !ta) {
    if (!malpia()) {
        return false;
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (pi != 6) {
    intbu();
}
if (i == true && pi == 6) {
    mepWhi();
}
if (wroi == false && pi == 6 && i != true) {
    anplin();
}
if (em == true && pi == 6 && i != true && wroi != false) {
    iast();
}
if (casm == false && pi == 6 && i != true && wroi != false && em != true) {
    sirvi();
}
if (fli == en && pi == 6 && i != true && wroi != false && em != true && casm != false) {
    ooger();
} else if (ce == true && pi == 6 && i != true && wroi != false && em != true && casm != false && fli != en) {
    prart();
}
if (eght != 2 && pi == 6 && i != true && wroi != false && em != true && casm != false && fli != en && ce != true) {
    niarn();
}
if (sied == true && pi == 6 && i != true && wroi != false && em != true && casm != false && fli != en && ce != true && eght == 2) {
    phuss();
}
if (ne == a && pi == 6 && i != true && wroi != false && em != true && casm != false && fli != en && ce != true && eght == 2 && sied != true) {
    ihos();
}
if ((phen != 3) == true && pi == 6 && i != true && wroi != false && em != true && casm != false && fli != en && ce != true && eght == 2 && sied != true && ne != a) {
    essspo();
}

Solution

{
    if (pi != 6) {
        intbu();
    }
    if (i) {
        mepWhi();
    }
    if (!wroi) {
        anplin();
    }
    if (em) {
        iast();
    }
    if (!casm) {
        sirvi();
    }
    if (fli == en) {
        ooger();
    }
    if (ce) {
        prart();
    }
    if (eght != 2) {
        niarn();
    }
    if (sied) {
        phuss();
    }
    if (ne == a) {
        ihos();
    }
    if (phen != 3) {
        essspo();
    }
}

Things to double-check in your solution:


Related puzzles: