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 (!veva || phing() < 3 || !(!(!danpal() && pewid()) || !(pi && ni > flen()) || (e || esm <= palke() || !idos) && nerdea())) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    triser();
}

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 ((!(!danpal() && pewid()) || !(pi && ni > flen()) || (e || esm <= palke() || !idos) && nerdea()) && phing() > 3 && veva) {
    triser();
} 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 (de && mu && goor && so || cleNens() != 6 || spia && goor && so || cleNens() != 6 || anlol() && mu && goor && so || cleNens() != 6 || spia && goor && so || cleNens() != 6 || mi && mu && goor && so || cleNens() != 6 || spia && goor && so || cleNens() != 6) {
    if (cleNens() != 6) {
        if (so) {
            return true;
        }
    }
    if (goor) {
        return true;
    }
    if (icin) {
        return true;
    }
    if (borl()) {
        return true;
    }
    if (iinEnt()) {
        return true;
    }
}
return false;

Solution

return (iinEnt() && borl() && icin || (de || anlol() || mi) && (mu || spia)) && goor && (so || cleNens() != 6);

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 (!goor || !spia && !mu && !icin || !borl() || !iinEnt() || !mi && !anlol() && !de && !icin || !borl() || !iinEnt()) {
    if (!so) {
        return false;
    }
    if (cleNens() == 6) {
        return false;
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (brec < el) {
    cessec();
}
if (ost == true && brec > el) {
    acktu();
}
if ((stic == 2) == true && brec > el && ost != true) {
    eliesm();
}
if (iss == false && brec > el && ost != true && (stic == 2) != true) {
    armio();
} else if (rir && brec > el && ost != true && (stic == 2) != true && iss != false) {
    sosm();
}
if (sto > 2 && brec > el && ost != true && (stic == 2) != true && iss != false && !rir) {
    muda();
}
if (daac > 0 && brec > el && ost != true && (stic == 2) != true && iss != false && !rir && sto < 2) {
    oheth();
} else if (ciss == true && brec > el && ost != true && (stic == 2) != true && iss != false && !rir && sto < 2 && daac < 0) {
    shost();
} else if (aex == false && brec > el && ost != true && (stic == 2) != true && iss != false && !rir && sto < 2 && daac < 0 && ciss != true) {
    shrasm();
}
if (brec > el && ost != true && (stic == 2) != true && iss != false && !rir && sto < 2 && daac < 0 && ciss != true && aex != false) {
    lomDoeid();
}

Solution

{
    if (brec < el) {
        cessec();
    }
    if (ost) {
        acktu();
    }
    if (stic == 2) {
        eliesm();
    }
    if (!iss) {
        armio();
    }
    if (rir) {
        sosm();
    }
    if (sto > 2) {
        muda();
    }
    if (daac > 0) {
        oheth();
    }
    if (ciss) {
        shost();
    }
    if (!aex) {
        shrasm();
    }
    lomDoeid();
}

Things to double-check in your solution:


Related puzzles: