Monthly Archives: February 2013

CodingBat: Java. Logic-1, Part III

greenTicket:

public int greenTicket(int a, int b, int c) {
	if (a != b && a != c && c != b) return 0;
	return (a == b && b == c) ? 20 : 10;
}

blueTicket:

public int blueTicket(int a, int b, int c) {
	if (a + b == 10 || a + c == 10 || c + b == 10)
		return 10;
	return (a - c == 10 || b - c == 10) ? 5 : 0;
}

If you’re not comfortable doing algebraic manipulations, then the solution looks like this:

public int blueTicket(int a, int b, int c) {
	if (a + b == 10 || a + c == 10 || c + b == 10)
		return 10;
	return (a + b - (c + b) == 10 || a + b - (a + c) == 10) ? 5 : 0;
}

shareDigit:

public boolean shareDigit(int a, int b) {
	return (a % 10 == b % 10 || a / 10 == b / 10 || 
			a % 10 == b / 10 || b % 10 == a / 10);
}

sumLimit:

public int sumLimit(int a, int b) {
	String sum = String.valueOf(a + b);
	String lengthA = String.valueOf(a);
	return (sum.length() == lengthA.length()) ? a + b : a;
}

CodingBat: Java. Logic-1, Part II

answerCell:

public boolean answerCell(boolean isMorning, boolean isMom, 
		boolean isAsleep) {
	if (isAsleep) return false;
	if (isMorning) return isMom;
	return true;
}

The sample solution on the website is less clean, though:

public boolean answerCell(boolean isMorning, boolean isMom, 
		boolean isAsleep) {
	if (isAsleep) {
		return false;
	}
	if (isMorning && !isMom) {
		return false;
	}
	return true;
}

teaParty:

public int teaParty(int tea, int candy) {
	if (candy < 5 || tea < 5)
		return 0;
	if (candy >= 5 && tea >= 5 && (candy >= 2 * tea || tea >= 2 * candy))
		return 2;
	return 1;
}

twoAsOne:

public boolean twoAsOne(int a, int b, int c) {
		return a + b == c || a + c == b || b + c == a;
}

inOrder:

public boolean inOrder(int a, int b, int c, boolean bOk) {
	if (bOk) return b < c;
	return a < b && b < c;
}

inOrderEqual:

public boolean inOrderEqual(int a, int b, int c, boolean equalOk) {
	if (equalOk) return a <= b && b <= c;
	return a < b && b < c;
}

lastDigit:

public boolean lastDigit(int a, int b, int c) {
	return a % 10 == b % 10 || a % 10 == c % 10 || b % 10 == c % 10;
}

lessBy10:

public boolean lessBy10(int a, int b, int c) {
	return Math.abs(a - c) >= 10 || Math.abs(a - b) >= 10
			|| Math.abs(b - c) >= 10;
}

withoutDoubles:

public int withoutDoubles(int die1, int die2, boolean noDoubles) {
	if (!noDoubles) return die1 + die2;
	if (die1 == die2) return (die1 != 6) ? die1 + die2 + 1 : die1 + 1;
	return die1 + die2;
}

maxMod5:

public int maxMod5(int a, int b) {
	if (a == b) return 0;
	if (a % 5 == b % 5) return (a < b) ? a : b;
	return (a < b) ? b : a;
}

redTicket:

public int redTicket(int a, int b, int c) {
	if (a == b && b == c) return (c == 2) ? 10 : 5;
	if (a != b && a != c) return 1;
	return 0;
}

CodingBat: Java. Logic-1, Part I

In the Logic-1 section of CodingBat the pace picks up a bit. None of the exercises should make you break a sweat, but it’s quite easy to write unnecessarily convoluted if/else statements. Many of my solutions make use of the ternary operator to save some vertical space and increase readability.

All 24 solutions were successfully tested on 4 February 2013.

cigarParty:

public boolean cigarParty(int cigars, boolean isWeekend) {
	if (isWeekend) return (cigars >= 40);
	return cigars >= 40 && cigars <= 60;
}

dateFashion:

public int dateFashion(int you, int date) {
	if (you <= 2 || date <= 2) return 0;
	return (you >= 8 && date >= 2 || date >= 8 && you >= 2) ? 2 : 1;
}

squirrelPlay:

public boolean squirrelPlay(int temp, boolean isSummer) {
	return (isSummer) ? (temp >= 60 && temp <= 100)
		: (temp >= 60 && temp <= 90);
}

caughtSpeeding:

public int caughtSpeeding(int speed, boolean isBirthday) {
	if (isBirthday) speed -= 5;
	if (speed <= 60) return 0;
	return (speed > 60 && speed <= 80) ? 1 : 2;
}

sortaSum:

public int sortaSum(int a, int b) {
	return (a + b >= 10 && a + b <= 19) ? 20 : a + b;
}

alarmClock:

	if (vacation) return (day >= 1 && day <= 5) ? "10:00" : "off";
	return (day >= 1 && day <= 5) ? "7:00" : "10:00";

If you think this is too concise, then compare it with a solution that avoids the ternary operator:

public String alarmClock(int day, boolean vacation) {
	if (vacation) {
		if (day >= 1 && day <= 5)
			return "10:00";
		return "off";
	}
	if (day >= 1 && day <= 5)
		return "7:00";
	return "10:00";
}

Ugly, isn’t it?

love6:

public boolean love6(int a, int b) {
	return a == 6 || b == 6 || a + b == 6 || Math.abs(a - b) == 6;
}

in1To10:

public boolean in1To10(int n, boolean outsideMode) {
	return (outsideMode) ? n <= 1 || n >= 10 : n >= 1 && n <= 10;
}

nearTen:

public boolean nearTen(int num) {
	return num % 10 >= 8 || num % 10 <= 2;
}

teenSum:

public int teenSum(int a, int b) {
	return (a >= 13 && a <= 19 || b >= 13 && b <= 19) ? 19 : a + b;
}