Category Archives: CodingBat: Java

CodingBat: Java. Logic-1, Part II


For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.


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;
}
&#91;/sourcecode&#93;

<b>inOrderEqual:</b>
[sourcecode language="Java" gutter="false"]
public boolean inOrderEqual(int a, int b, int c, boolean equalOk) {
	if (equalOk) return a <= b && b <= c;
	return a < b && b < c;
}
&#91;/sourcecode&#93;






<b>lastDigit:</b>
[sourcecode language="Java" gutter="false"]
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;
}
&#91;/sourcecode&#93;

<b>redTicket:</b>
[sourcecode language="Java" gutter="false"]
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;
}

For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.


CodingBat: Java. Logic-1, Part I


For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.


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;
}
&#91;/sourcecode&#93;


<b>dateFashion:</b>
[sourcecode language="Java" gutter="false"]
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);
}
&#91;/sourcecode&#93;


<b>caughtSpeeding:</b>
[sourcecode language="Java" gutter="false"]
public int caughtSpeeding(int speed, boolean isBirthday) {
	if (isBirthday) speed -= 5;
	if (speed <= 60) return 0;
	return (speed > 60 && speed <= 80) ? 1 : 2;
}
&#91;/sourcecode&#93;

<b>sortaSum:</b>
[sourcecode language="Java" gutter="false"]
public int sortaSum(int a, int b) {
	return (a + b >= 10 && a + b <= 19) ? 20 : a + b;
}
&#91;/sourcecode&#93;


<b>alarmClock:</b>

[sourcecode language="Java" gutter="false"]
	if (vacation) return (day >= 1 && day <= 5) ? "10:00" : "off";
	return (day >= 1 && day <= 5) ? "7:00" : "10:00";
&#91;/sourcecode&#93;

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

&#91;sourcecode language="Java" gutter="false"&#93;
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";
}
&#91;/sourcecode&#93;

Ugly, isn't it?


<b>love6:</b>
[sourcecode language="Java" gutter="false"]
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; } [/sourcecode]nearTen:

public boolean nearTen(int num) {
return num % 10 >= 8 || num % 10 <= 2; } [/sourcecode]teenSum:

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


For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.


CodingBat: Java. Array-1, Part III


For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.


swapEnds:

public int[] swapEnds(int[] nums) {
	int first = nums[0];
	int last = nums[nums.length - 1];
	nums[0] = last;
	nums[nums.length - 1] = first;
	return nums;
}

midThree:

public int[] midThree(int[] nums) {
	int mid = nums.length / 2;
	int[] result = { nums[mid - 1], nums[mid], nums[mid + 1] };
	return result;
}

The purpose of the variable mid is to make the code a bit more concise.

maxTriple:

public int maxTriple(int[] nums) {
	int mid = nums.length / 2;
	int end = nums.length - 1;
	int max = nums[0];
	if (nums[mid] > max) max = nums[mid];
	if (nums[end] > max) max = nums[end];
	return max;
}

frontPiece:

public int[] frontPiece(int[] nums) {
	if (nums.length <= 1) return nums;
	int[] result = { nums[0], nums[1] };
	return result;
}

unlucky1:

public boolean unlucky1(int[] nums) {
	int len = nums.length;
	if (len <= 1) return false;

	for (int i = 0; i <= 1; i++) {
		if (nums[i] == 1 && nums[i + 1] == 3)
			return true;
		if (len < 3) break;
	}

	return nums[len - 2] == 1 && nums[len - 1] == 3;
}

make2:

public int[] make2(int[] a, int[] b) {
	int[] res = new int[2];
	if (a.length == 0) {
		res[0] = b[0];
		res[1] = b[1];
	} else if (a.length == 1) {
		res[0] = a[0];
		res[1] = b[0];
	} else {
		res[0] = a[0];
		res[1] = a[1];
	}
	return res;
}

front11:

public int[] front11(int[] a, int[] b) {
	int[] one = new int[1];
	int[] two = new int[2];
	if (a.length == 0 && b.length == 0) {
		return a;
	}
	if (a.length >= 1 && b.length == 0) {
		one[0] = a[0];
		return one;
	}
	if (a.length >= 1 && b.length >= 1) {
		two[0] = a[0];
		two[1] = b[0];
		return two;
	}
	if (a.length == 0 && b.length >= 1) {
		one[0] = b[0];
		return one;
	}
	return a;
}

For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.


CodingBat: Java. Array-1, Part II


For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.


makeEnds:

public int[] makeEnds(int[] nums) {
	int[] result = { nums[0], nums[nums.length - 1] };
	return result;
}

has23:

public boolean has23(int[] nums) {
	for (int i = 0; i < nums.length; i++) {
		if (nums[i] == 2 || nums[i] == 3)
			return true;
	}
	return false;
}

no23:

public boolean no23(int[] nums) {
	for (int i = 0; i < nums.length; i++) {
		if (nums[i] == 2 || nums[i] == 3)
			return false;
	}
	return true;
}

This is one of the examples where I wonder why Nick Parlante even bothered with the exercise, since it is analog to the previous one and therefore feels like busywork.

makeLast:

public int[] makeLast(int[] nums) {
	int[] result = new int[2 * nums.length];
	result[result.length - 1] = nums[nums.length - 1];
	return result;
}

double23:

public boolean double23(int[] nums) {
	int twoCount = 0;
	int threeCount = 0;
	for (int i = 0; i < nums.length; i++) {
		if (nums[i] == 2)
			twoCount++;
		if (nums[i] == 3)
			threeCount++;
	}
	return twoCount == 2 || threeCount == 2;
}

fix23:

public int[] fix23(int[] nums) {
	for (int i = 0; i < nums.length - 1; i++) {
		if (nums[i] == 2 && nums[i + 1] == 3)
			nums[i + 1] = 0;
	}
	return nums;
}

start1:

public int start1(int[] a, int[] b) {
	int count = 0;
	if (a.length > 0 && a[0] == 1)
		count++;
	if (b.length > 0 && b[0] == 1)
		count++;
	return count;
}

biggerTwo:

public int[] biggerTwo(int[] a, int[] b) {
	if (a[0] + a[1] < b[0] + b[1])
		return b;
	return a;
}

makeMiddle:

public int[] makeMiddle(int[] nums) {
	int[] result = { nums[nums.length / 2 - 1], nums[nums.length / 2] };
	return result;
}

plusTwo:

public int[] plusTwo(int[] a, int[] b) {
	int[] result = { a[0], a[1], b[0], b[1] };
	return result;
}

For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.


CodingBat: Java. Array-1, Part I


For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.


The Array-1 section of CodingBat contains 27 basic array exercises, most of which are very simple. In the following three posts I’ll present my solutions. Just like it was the case with the String-1 section, there isn’t much to comment on.

All solutions were successfully tested on 27 January 2013.

firstLast6:

public boolean firstLast6(int[] nums) {
	return nums[0] == 6 || nums[nums.length - 1] == 6;
}

sameFirstLast:

public boolean sameFirstLast(int[] nums) {
	return nums.length >= 1 && nums[0] == nums[nums.length - 1];
}

makePi:

public int[] makePi() {
	int[] pie = { 3, 1, 4 };
	return pie;
}

commonEnd:

public boolean commonEnd(int[] a, int[] b) {
	return a[0] == b[0] || a[a.length - 1] == b[b.length - 1];
}

sum3:

public int sum3(int[] nums) {
	return nums[0] + nums[1] + nums[2];
}

rotateLeft3:

public int[] rotateLeft3(int[] nums) {
	int[] rLeft = { nums[1], nums[2], nums[0] };
	return rLeft;
}

reverse3:

public int[] reverse3(int[] nums) {
	int[] newOrder = { nums[2], nums[1], nums[0] };
	return newOrder;
}

maxEnd3:

public int[] maxEnd3(int[] nums) {
	if (nums[2] > nums[0]) {
		nums[0] = nums[2];
		nums[1] = nums[2];
	} else {
		nums[1] = nums[0];
		nums[2] = nums[0];
	}
	return nums;
}

sum2:

public int sum2(int[] nums) {
	if (nums.length == 0)
		return 0;
	if (nums.length == 1)
		return nums[0];
	return nums[0] + nums[1];
}

middleWay:

public int[] middleWay(int[] a, int[] b) {
	int[] c = { a[1], b[1] };
	return c;
}

For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.