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 (sior() && la && nite() != thrid() && rac && !(!il || gracro() != 7) || !(creaha() >= a) || abon < 8 || !jo || i && !(avus() != 3)) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    blord();
}

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 ((avus() != 3 || !i) && jo && abon > 8 && creaha() >= a && (!il || gracro() != 7 || !rac || nite() == thrid() || !la || !sior())) {
    blord();
} 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 (ha && ionse() && felDresca() && ro && pemp || swiar() || chuon() || paeSharci() && !e && gler && pemp || swiar() || chuon() || !phe && ro && pemp || swiar() || chuon() || paeSharci() && !e && gler && pemp || swiar() || chuon()) {
    if (!phe && ro && pemp || swiar() || chuon() || paeSharci() && !e && gler && pemp || swiar() || chuon()) {
        if (paeSharci() && !e && gler && pemp || swiar() || chuon()) {
            if (swiar() || chuon()) {
                if (pemp) {
                    return true;
                }
            }
            if (ro) {
                return true;
            }
        }
        if (felDresca()) {
            return true;
        }
    }
    if (eskEbi() != 4) {
        return true;
    }
}
return false;

Solution

return (eskEbi() != 4 || ha && ionse()) && (felDresca() || !phe) && (ro || paeSharci() && !e && gler) && (pemp || swiar() || chuon());

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 (!gler && !ro || e && !ro || !paeSharci() && !ro || phe && !felDresca() || !ionse() && eskEbi() == 4 || !ha && eskEbi() == 4) {
    if (!pemp) {
        return false;
    }
    if (!swiar()) {
        return false;
    }
    if (!chuon()) {
        return false;
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (ul != es) {
    iree();
}
if (pe == false && ul == es) {
    insac();
}
if (ec == true && ul == es && pe != false) {
    cirth();
}
if (orar < rire && ul == es && pe != false && ec != true) {
    ilce();
} else if (el < nuss && ul == es && pe != false && ec != true && orar > rire) {
    oueAbla();
}
if (qion == false && ul == es && pe != false && ec != true && orar > rire && el > nuss) {
    glina();
} else if (tesm == true && ul == es && pe != false && ec != true && orar > rire && el > nuss && qion != false) {
    glun();
} else if (oosm == true && ul == es && pe != false && ec != true && orar > rire && el > nuss && qion != false && tesm != true) {
    plirpe();
}
if (ta != gont && ul == es && pe != false && ec != true && orar > rire && el > nuss && qion != false && tesm != true && oosm != true) {
    onmi();
} else if (phal == true && ul == es && pe != false && ec != true && orar > rire && el > nuss && qion != false && tesm != true && oosm != true && ta == gont) {
    imio();
} else if (in == true && ul == es && pe != false && ec != true && orar > rire && el > nuss && qion != false && tesm != true && oosm != true && ta == gont && phal != true) {
    snos();
}

Solution

{
    if (ul != es) {
        iree();
    }
    if (!pe) {
        insac();
    }
    if (ec) {
        cirth();
    }
    if (orar < rire) {
        ilce();
    }
    if (el < nuss) {
        oueAbla();
    }
    if (!qion) {
        glina();
    }
    if (tesm) {
        glun();
    }
    if (oosm) {
        plirpe();
    }
    if (ta != gont) {
        onmi();
    }
    if (phal) {
        imio();
    }
    if (in) {
        snos();
    }
}

Things to double-check in your solution:


Related puzzles: