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 (casPoida() || o || sancir() && du && mogh || neud == 7 || !(stiumu() == 9) || !(psinca() || !ad) || clert()) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    iouc();
}

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 (!clert() && (psinca() || !ad) && stiumu() == 9 && neud != 7 && (!mogh || !du || !sancir()) && !o && !casPoida()) {
    iouc();
} 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 (chepa() || sama || gi || ousa) {
    if (mesi() <= 0 || ilvi == 1 || e == ie) {
        if (prust()) {
            if (eleDomwed()) {
                return true;
            }
        }
        if (iashic()) {
            return true;
        }
        if (!i) {
            return true;
        }
    }
}
return false;

Solution

return !i && iashic() && (eleDomwed() || prust()) || mesi() <= 0 || ilvi == 1 || e == ie || chepa() || sama || gi || ousa;

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 (!iashic() || i) {
    if (!eleDomwed()) {
        return false;
    }
    if (!prust()) {
        return false;
    }
}
if (mesi() >= 0) {
    return false;
}
if (ilvi != 1) {
    return false;
}
if (e != ie) {
    return false;
}
if (!chepa()) {
    return false;
}
if (!sama) {
    return false;
}
if (!gi) {
    return false;
}
if (!ousa) {
    return false;
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (ded) {
    fompro();
} else if (poud == 3 && !ded) {
    pident();
} else if (va != mu && !ded && poud != 3) {
    mecrec();
}
if (vecs == false && !ded && poud != 3 && va == mu) {
    jacsu();
} else if (voul == true && !ded && poud != 3 && va == mu && vecs != false) {
    odcul();
}
if (urar == 6 && !ded && poud != 3 && va == mu && vecs != false && voul != true) {
    dedva();
}
if (reso == true && !ded && poud != 3 && va == mu && vecs != false && voul != true && urar != 6) {
    esou();
} else if (rop != te && !ded && poud != 3 && va == mu && vecs != false && voul != true && urar != 6 && reso != true) {
    giofo();
} else if (sihe == true && !ded && poud != 3 && va == mu && vecs != false && voul != true && urar != 6 && reso != true && rop == te) {
    flose();
} else if (foph == true && !ded && poud != 3 && va == mu && vecs != false && voul != true && urar != 6 && reso != true && rop == te && sihe != true) {
    inho();
}

Solution

{
    if (ded) {
        fompro();
    }
    if (poud == 3) {
        pident();
    }
    if (va != mu) {
        mecrec();
    }
    if (!vecs) {
        jacsu();
    }
    if (voul) {
        odcul();
    }
    if (urar == 6) {
        dedva();
    }
    if (reso) {
        esou();
    }
    if (rop != te) {
        giofo();
    }
    if (sihe) {
        flose();
    }
    if (foph) {
        inho();
    }
}

Things to double-check in your solution:


Related puzzles: