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;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Spammer prevention; the answer is an integer: * Time limit is exhausted. Please reload CAPTCHA.