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 (!(strus() && se > 6 && cedOlmu() || grel() || eacLooa()) && !misi && (doee || cide() && de) && mera()) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    pneto();
}

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 (!mera() || (!de || !cide()) && !doee || misi || strus() && se > 6 && cedOlmu() || grel() || eacLooa()) {
    pneto();
} 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 (meiGenist() && ofaesm() >= 4 && pline() && edu == 8 || trunt() && ve || spac() || qolUou() != 1 && ofaesm() >= 4 && pline() && edu == 8 || trunt() && ve || spac()) {
    if (sces() && sehe) {
        if (sehe) {
            return true;
        }
        if (elci != epe) {
            return true;
        }
    }
}
return false;

Solution

return (elci != epe || sces()) && sehe || (meiGenist() || qolUou() != 1) && ofaesm() >= 4 && pline() && (edu == 8 || trunt() && ve || spac());

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 (!pline() && !sehe || !sces() && elci == epe || ofaesm() <= 4 && !sehe || !sces() && elci == epe || qolUou() == 1 && !meiGenist() && !sehe || !sces() && elci == epe) {
    if (!trunt() && edu != 8 && !sehe || !sces() && elci == epe) {
        if (!sces() && elci == epe) {
            if (!sehe) {
                return false;
            }
        }
        if (edu != 8) {
            return false;
        }
        if (!ve) {
            return false;
        }
    }
    if (!spac()) {
        return false;
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (ad == false) {
    nofim();
} else if (ce == true && ad != false) {
    estick();
} else if (i == false && ad != false && ce != true) {
    haplit();
} else if (tu == true && ad != false && ce != true && i != false) {
    folel();
} else if (ol == true && ad != false && ce != true && i != false && tu != true) {
    mespi();
}
if (briw == true && ad != false && ce != true && i != false && tu != true && ol != true) {
    stess();
}
if (al && ad != false && ce != true && i != false && tu != true && ol != true && briw != true) {
    estpi();
}
if (pra == true && ad != false && ce != true && i != false && tu != true && ol != true && briw != true && !al) {
    wadgir();
}
if (siar > 1 && ad != false && ce != true && i != false && tu != true && ol != true && briw != true && !al && pra != true) {
    ilsi();
}
if (!es && ad != false && ce != true && i != false && tu != true && ol != true && briw != true && !al && pra != true && siar < 1) {
    wedna();
}

Solution

{
    if (!ad) {
        nofim();
    }
    if (ce) {
        estick();
    }
    if (!i) {
        haplit();
    }
    if (tu) {
        folel();
    }
    if (ol) {
        mespi();
    }
    if (briw) {
        stess();
    }
    if (al) {
        estpi();
    }
    if (pra) {
        wadgir();
    }
    if (siar > 1) {
        ilsi();
    }
    if (!es) {
        wedna();
    }
}

Things to double-check in your solution:


Related puzzles: