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 == 6 || !(!(pemid() || nen) && !(he == 4 && tolde() || (!(geatpi() && nubre() == 5) || di) && phephi() == on))) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    epeng();
}

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 (!(pemid() || nen) && !(he == 4 && tolde() || (!(geatpi() && nubre() == 5) || di) && phephi() == on) && u != 6) {
    epeng();
} 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 (od) {
    if (tros && necon() && manpe() || iocan()) {
        if (medder() && pa && manpe() || iocan() || !pei && pa && manpe() || iocan() || restsi() != scoCiu() && pa && manpe() || iocan()) {
            if (iocan()) {
                if (manpe()) {
                    return true;
                }
            }
            if (pa) {
                return true;
            }
            if (firwin()) {
                return true;
            }
        }
    }
}
return false;

Solution

return ((firwin() || medder() || !pei || restsi() != scoCiu()) && pa || tros && necon()) && (manpe() || iocan()) || od;

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 (!necon() && !pa || restsi() == scoCiu() && pei && !medder() && !firwin() || !tros && !pa || restsi() == scoCiu() && pei && !medder() && !firwin()) {
    if (!manpe()) {
        return false;
    }
    if (!iocan()) {
        return false;
    }
}
if (!od) {
    return false;
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (brem == false) {
    ospip();
} else if (eaer == true && brem != false) {
    brouc();
} else if (osh == true && brem != false && eaer != true) {
    codop();
}
if (te > e && brem != false && eaer != true && osh != true) {
    omeu();
} else if (ilo > 9 && brem != false && eaer != true && osh != true && te < e) {
    jepi();
} else if (o && brem != false && eaer != true && osh != true && te < e && ilo < 9) {
    urcled();
}
if (!diul && brem != false && eaer != true && osh != true && te < e && ilo < 9 && !o) {
    nebi();
} else if (lus == true && brem != false && eaer != true && osh != true && te < e && ilo < 9 && !o && diul) {
    cleAneirn();
} else if (erbe == true && brem != false && eaer != true && osh != true && te < e && ilo < 9 && !o && diul && lus != true) {
    sedesm();
}

Solution

{
    if (!brem) {
        ospip();
    }
    if (eaer) {
        brouc();
    }
    if (osh) {
        codop();
    }
    if (te > e) {
        omeu();
    }
    if (ilo > 9) {
        jepi();
    }
    if (o) {
        urcled();
    }
    if (!diul) {
        nebi();
    }
    if (lus) {
        cleAneirn();
    }
    if (erbe) {
        sedesm();
    }
}

Things to double-check in your solution:


Related puzzles: