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 (!i || !ial || !ihic || pahed() || (glagh() == pa || brup()) && pedce() && !iec && (ma == meho || fi >= 1 || ow)) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    qishar();
}

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 ((!ow && fi <= 1 && ma != meho || iec || !pedce() || !brup() && glagh() != pa) && !pahed() && ihic && ial && i) {
    qishar();
} 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 (el && paou && stio() || seshec() != al && paou && stio() || ciac == af && stio()) {
    if (tapo && ou == 0 && ulaf && stio() || !orso && stio() || cilPodong() != en && ulaf && stio() || !orso && stio() || !celi && stio()) {
        if (!celi && stio()) {
            if (cilPodong() != en && ulaf && stio() || !orso && stio()) {
                if (!orso && stio()) {
                    if (stio()) {
                        return true;
                    }
                    if (ulaf) {
                        return true;
                    }
                }
                if (ou == 0) {
                    return true;
                }
            }
        }
        if (!we) {
            return true;
        }
    }
}
return false;

Solution

return ((!we || tapo) && ((ou == 0 || cilPodong() != en) && (ulaf || !orso) || !celi) || (el || seshec() != al) && paou || ciac == af) && stio();

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 (ciac != af && !paou && celi && orso && !ulaf || cilPodong() == en && ou != 0 || !tapo && we || seshec() == al && !el && celi && orso && !ulaf || cilPodong() == en && ou != 0 || !tapo && we) {
    if (!stio()) {
        return false;
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (ces == true) {
    hijec();
} else if (pana == true && ces != true) {
    nirPai();
}
if (ie == 2 && ces != true && pana != true) {
    naeon();
} else if (ste == true && ces != true && pana != true && ie != 2) {
    piot();
}
if (tist == true && ces != true && pana != true && ie != 2 && ste != true) {
    breColne();
}
if (oed == false && ces != true && pana != true && ie != 2 && ste != true && tist != true) {
    panpsi();
} else if (ost == true && ces != true && pana != true && ie != 2 && ste != true && tist != true && oed != false) {
    rirIsste();
} else if (cii != 3 && ces != true && pana != true && ie != 2 && ste != true && tist != true && oed != false && ost != true) {
    spac();
}
if (di == true && ces != true && pana != true && ie != 2 && ste != true && tist != true && oed != false && ost != true && cii == 3) {
    berheh();
}
if (ac == false && ces != true && pana != true && ie != 2 && ste != true && tist != true && oed != false && ost != true && cii == 3 && di != true) {
    plesep();
}
if (ces != true && pana != true && ie != 2 && ste != true && tist != true && oed != false && ost != true && cii == 3 && di != true && ac != false) {
    ciss();
}

Solution

{
    if (ces) {
        hijec();
    }
    if (pana) {
        nirPai();
    }
    if (ie == 2) {
        naeon();
    }
    if (ste) {
        piot();
    }
    if (tist) {
        breColne();
    }
    if (!oed) {
        panpsi();
    }
    if (ost) {
        rirIsste();
    }
    if (cii != 3) {
        spac();
    }
    if (di) {
        berheh();
    }
    if (!ac) {
        plesep();
    }
    ciss();
}

Things to double-check in your solution:


Related puzzles: