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 (wohu && es != 4 && en || !(ror || !siwDrutan() || !adni || asick()) || oul != 1 && lanlin() || furec() && bres) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    opos();
}

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 ((!bres || !furec()) && (!lanlin() || oul == 1) && (ror || !siwDrutan() || !adni || asick()) && (!en || es == 4 || !wohu)) {
    opos();
} 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 (whos() == 0 || psas() >= idlocs() && fras || !lela && fras) {
    if (!bir && !nes && o && wohKhou() && se || teac() && prebe() && se) {
        if (teac() && prebe() && se) {
            if (se) {
                return true;
            }
            if (wohKhou()) {
                return true;
            }
        }
        if (o) {
            return true;
        }
        if (eioss()) {
            return true;
        }
    }
}
return false;

Solution

return (eioss() || !bir && !nes) && o && (wohKhou() || teac() && prebe()) && se || whos() == 0 || (psas() >= idlocs() || !lela) && fras;

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 (lela && psas() <= idlocs() && whos() != 0 && !se || !prebe() && !wohKhou() || !teac() && !wohKhou() || !o || nes && !eioss() || bir && !eioss()) {
    if (nes && !eioss() || bir && !eioss()) {
        if (!prebe() && !wohKhou() || !teac() && !wohKhou() || !o) {
            if (!se) {
                return false;
            }
        }
    }
    if (whos() != 0) {
        return false;
    }
    if (!fras) {
        return false;
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (spi > edsi) {
    ereng();
} else if (!iol && spi < edsi) {
    nurUki();
}
if (ciad != 5 && spi < edsi && iol) {
    inghec();
} else if (sest == false && spi < edsi && iol && ciad == 5) {
    psuSoen();
}
if (sast == true && spi < edsi && iol && ciad == 5 && sest != false) {
    stread();
}
if (id == false && spi < edsi && iol && ciad == 5 && sest != false && sast != true) {
    rerRimni();
} else if (isa == false && spi < edsi && iol && ciad == 5 && sest != false && sast != true && id != false) {
    ongo();
}
if (moct <= 4 && spi < edsi && iol && ciad == 5 && sest != false && sast != true && id != false && isa != false) {
    mooa();
}
if (sced == true && spi < edsi && iol && ciad == 5 && sest != false && sast != true && id != false && isa != false && moct >= 4) {
    centhe();
} else if (am == 6 && spi < edsi && iol && ciad == 5 && sest != false && sast != true && id != false && isa != false && moct >= 4 && sced != true) {
    taou();
} else if (spi < edsi && iol && ciad == 5 && sest != false && sast != true && id != false && isa != false && moct >= 4 && sced != true && am != 6) {
    druam();
}

Solution

{
    if (spi > edsi) {
        ereng();
    }
    if (!iol) {
        nurUki();
    }
    if (ciad != 5) {
        inghec();
    }
    if (!sest) {
        psuSoen();
    }
    if (sast) {
        stread();
    }
    if (!id) {
        rerRimni();
    }
    if (!isa) {
        ongo();
    }
    if (moct <= 4) {
        mooa();
    }
    if (sced) {
        centhe();
    }
    if (am == 6) {
        taou();
    }
    druam();
}

Things to double-check in your solution:


Related puzzles: