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 (!(wurmo() && (ce > echu || !(eal == hudic()) || coprod() != pror || !ird)) || (ca || ta) && a == vebe || !phe || hi) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    nahir();
}

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 (!hi && phe && (a != vebe || !ta && !ca) && wurmo() && (ce > echu || !(eal == hudic()) || coprod() != pror || !ird)) {
    nahir();
} 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 (kasse() == 7 && sixi && hoec || mual || bup <= 3 && !pept && dism || paer() && sixi && hoec || mual || bup <= 3 && !pept && dism || eess && sixi && hoec || mual || bup <= 3 && !pept && dism) {
    if (mual || bup <= 3 && !pept && dism) {
        if (hoec) {
            return true;
        }
        if (sixi) {
            return true;
        }
    }
    if (!be) {
        return true;
    }
}
if (diali()) {
    return true;
}
return false;

Solution

return diali() && (!be || kasse() == 7 || paer() || eess) && (sixi && hoec || mual || bup <= 3 && !pept && dism);

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 (!eess && !paer() && kasse() != 7 && be || !diali()) {
    if (pept && !mual && !hoec || !sixi || bup >= 3 && !mual && !hoec || !sixi) {
        if (!sixi) {
            if (!hoec) {
                return false;
            }
        }
        if (!mual) {
            return false;
        }
        if (!dism) {
            return false;
        }
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (saes == 7) {
    dinmo();
}
if (mido == false && saes != 7) {
    febir();
}
if (!cas && saes != 7 && mido != false) {
    gleComiam();
}
if (co == false && saes != 7 && mido != false && cas) {
    reld();
} else if (pa <= 8 && saes != 7 && mido != false && cas && co != false) {
    ossNerpra();
} else if (seng == true && saes != 7 && mido != false && cas && co != false && pa >= 8) {
    abul();
} else if (ru == 5 && saes != 7 && mido != false && cas && co != false && pa >= 8 && seng != true) {
    mafSuuss();
}
if (mue == true && saes != 7 && mido != false && cas && co != false && pa >= 8 && seng != true && ru != 5) {
    fothhu();
}
if (se >= 3 && saes != 7 && mido != false && cas && co != false && pa >= 8 && seng != true && ru != 5 && mue != true) {
    cifos();
}
if (saes != 7 && mido != false && cas && co != false && pa >= 8 && seng != true && ru != 5 && mue != true && se <= 3) {
    biuac();
}

Solution

{
    if (saes == 7) {
        dinmo();
    }
    if (!mido) {
        febir();
    }
    if (!cas) {
        gleComiam();
    }
    if (!co) {
        reld();
    }
    if (pa <= 8) {
        ossNerpra();
    }
    if (seng) {
        abul();
    }
    if (ru == 5) {
        mafSuuss();
    }
    if (mue) {
        fothhu();
    }
    if (se >= 3) {
        cifos();
    }
    biuac();
}

Things to double-check in your solution:


Related puzzles: