Problem:
Given three ints, a b c, return true if they are in strict increasing order, such as 2 5 11, or 5 6 7, but not 6 5 7 or 5 5 7. However, with the exception that if "equalOk" is true, equality is allowed, such as 5 5 7 or 5 5 5.
inOrderEqual(2, 5, 11, false) → true
inOrderEqual(5, 7, 6, false) → false
inOrderEqual(5, 5, 7, true) → true
Solution:
public boolean inOrderEqual(int a, int b, int c, boolean equalOk) { if (!equalOk && a < b && b < c) return true; if (equalOk && a <= b && b <= c) return true; else return false; }
return (!equalOk) ? a < b && b < c : a <= b && b <= c;
ReplyDeleteGood compromise of ease of understanding logic and verbosity -
ReplyDeleteif (equalOk) {
return c >= b && b >= a;
} else {
return c > b && b > a;
}
return (equalOk && a <= b && b <= c) || (!equalOk && a < b && b < c);
ReplyDeletepublic boolean inOrderEqual(int a, int b, int c, boolean equalOk) {
ReplyDeletereturn (a < b && b < c) != (equalOk && a <= b && b <= c);
}
if((a % 10 == b % 10)||(a % 10 == c % 10) || (b % 10 == c % 10)) return true;
ReplyDeleteelse return false;
return((c>b&&b>a)||(c>=b&&b>=a&&equalOk));
ReplyDelete