I'm doing a CodingBat exercise and would like to learn to write code in the most efficient way. On this exercise, I was just wondering if there's a shorter way to write this code.
monkeyTrouble(true, true) → true
monkeyTrouble(false, false) → true
monkeyTrouble(true, false) → false
public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
if (aSmile && bSmile) {
return true;
}
if (!aSmile && !bSmile) {
return true;
}
return false;
}
-
\$\begingroup\$ there is "Show Solution" button that shows 3 solutions on the page you linked \$\endgroup\$Nikita U.– Nikita U.2014年01月09日 13:54:58 +00:00Commented Jan 9, 2014 at 13:54
2 Answers 2
Sometimes it is easy to forget that the simplest logical constructs like boolean are comparable with the ==
operator, and that, in Java, (false == false)
is true
.
With this in mind, your code could become:
public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
return aSmile == bSmile;
}
It may be easier to see how to get there if you first transform your original code into
public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
if ((aSmile && bSmile) || (!aSmile && !bSmile)) {
return true;
} else {
return false;
}
}
... which could become
public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
return (aSmile && bSmile) || (!aSmile && !bSmile);
}
From there, you may come to the realization that "both true or both false" is equivalent to "both the same".
Here is a verification of the output:
public static boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
return aSmile == bSmile;
}
private static void testTruth(boolean a, boolean b) {
System.out.printf("monkeyTrouble(%s, %s) = %s\n", a, b, monkeyTrouble(a, b));
}
public static void main(String[] args) {
testTruth(true, true);
testTruth(true, false);
testTruth(false, true);
testTruth(false, false);
}
This produces:
monkeyTrouble(true, true) = true
monkeyTrouble(true, false) = false
monkeyTrouble(false, true) = false
monkeyTrouble(false, false) = true
-
\$\begingroup\$ But there are two conditions there. The one states that if both monkeys are smiling or if neither of them are smiling, return 'true'. So wouldn't simply making aSmile == bSmile ignore those two conditions? \$\endgroup\$Warren van Rooyen– Warren van Rooyen2014年01月04日 13:38:19 +00:00Commented Jan 4, 2014 at 13:38
-
4\$\begingroup\$ if both are smiling then
aSmile == bSmile
istrue == true
, and that istrue
. If neither are smiling thenaSmile == bSmile
isfalse == false
, and that resolves totrue
. If only one is smiling, thenaSmile == bSmile
isfalse == true
, and that resolves tofalse
. \$\endgroup\$rolfl– rolfl2014年01月04日 13:45:10 +00:00Commented Jan 4, 2014 at 13:45 -
6\$\begingroup\$ Trust me, I'm a monkey, and I know
monkeyTrouble()
;-) \$\endgroup\$rolfl– rolfl2014年01月04日 13:51:45 +00:00Commented Jan 4, 2014 at 13:51 -
\$\begingroup\$ truth of false and false is true - doesn't that sound like monkey business?
(false && false) == false
returnstrue
, no? Then false and false is... false. \$\endgroup\$Mathieu Guindon– Mathieu Guindon2014年01月04日 15:54:18 +00:00Commented Jan 4, 2014 at 15:54 -
1\$\begingroup\$ @retailcoder : big difference between
false == false
andfalse && false
.... ;-) \$\endgroup\$rolfl– rolfl2014年01月04日 16:56:49 +00:00Commented Jan 4, 2014 at 16:56
This is the exclusive-or (or XOR) condition negated.
You can simply do this:
public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
return !(aSmile ^ bSmile);
}
or, as it is so simply, you can use it in your code without the function.
Explanation of XOR operator ^
:
a ^ b = c
1 0 1
0 1 1
0 0 0
1 1 0
-
4\$\begingroup\$ The negation of XOR is also known as XNOR. \$\endgroup\$200_success– 200_success2014年01月04日 17:32:19 +00:00Commented Jan 4, 2014 at 17:32
-
2\$\begingroup\$ Wrong precedence there. \$\endgroup\$user2357112– user23571122014年01月05日 01:11:40 +00:00Commented Jan 5, 2014 at 1:11
-
\$\begingroup\$ Shouldn't this be:
!(aSmile ^ bSmile)
? \$\endgroup\$Wayne Conrad– Wayne Conrad2014年01月05日 12:16:14 +00:00Commented Jan 5, 2014 at 12:16 -
\$\begingroup\$ @WayneConrad, you are right, Its corrected! It also works the other way, but this is the correct one, thanks. \$\endgroup\$António Almeida– António Almeida2014年01月05日 14:39:36 +00:00Commented Jan 5, 2014 at 14:39