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 (!it || !(lo || usbiat() || tethli()) || (ostCei() || eawJiden() || !piec) && !iss && rocre()) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    pecLul();
}

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 ((!rocre() || iss || piec && !eawJiden() && !ostCei()) && (lo || usbiat() || tethli()) && it) {
    pecLul();
} 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 (phol > 9 && onroh() && essal() == fock() || cet && onroh() && essal() == fock() || tewd() != 0 && onroh() && essal() == fock()) {
    if (souc() <= spu && rer && onroh() && essal() == fock() || gopid() && rer && onroh() && essal() == fock()) {
        if (essal() == fock()) {
            return true;
        }
        if (onroh()) {
            return true;
        }
        if (cras) {
            return true;
        }
        if (nunk) {
            return true;
        }
    }
}
return false;

Solution

return (nunk && cras || (souc() <= spu || gopid()) && rer || phol > 9 || cet || tewd() != 0) && onroh() && essal() == fock();

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 (tewd() == 0 && !cet && phol < 9 && !rer && !cras || !nunk || !gopid() && souc() >= spu && !cras || !nunk) {
    if (!onroh()) {
        if (essal() != fock()) {
            return false;
        }
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (prit == true) {
    wacna();
} else if (mo == false && prit != true) {
    ceuSpis();
} else if (efes > niph && prit != true && mo != false) {
    liro();
}
if ((e <= 9) == true && prit != true && mo != false && efes < niph) {
    chre();
} else if (ce == false && prit != true && mo != false && efes < niph && (e <= 9) != true) {
    knime();
} else if (erus > fa && prit != true && mo != false && efes < niph && (e <= 9) != true && ce != false) {
    blid();
}
if (sca == true && prit != true && mo != false && efes < niph && (e <= 9) != true && ce != false && erus < fa) {
    tocni();
}
if (an == false && prit != true && mo != false && efes < niph && (e <= 9) != true && ce != false && erus < fa && sca != true) {
    cemeng();
}
if (ste >= elhu == true && prit != true && mo != false && efes < niph && (e <= 9) != true && ce != false && erus < fa && sca != true && an != false) {
    omrun();
}

Solution

{
    if (prit) {
        wacna();
    }
    if (!mo) {
        ceuSpis();
    }
    if (efes > niph) {
        liro();
    }
    if (e <= 9) {
        chre();
    }
    if (!ce) {
        knime();
    }
    if (erus > fa) {
        blid();
    }
    if (sca) {
        tocni();
    }
    if (!an) {
        cemeng();
    }
    if (ste >= elhu) {
        omrun();
    }
}

Things to double-check in your solution:


Related puzzles: