Monthly Archives: January 2013

CodingBat: Java. Array-1, Part III

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

CodingBat: Java. Array-1, Part II

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

CodingBat: Java. Array-1, Part I

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

CodingBat: Java. String-1, Part IV

startWord:

public String startWord(String str, String word) {
		if (word.length() > str.length()) return "";
		if (str.substring(0, word.length()).equals(word)) return word;
		if (str.substring(1, word.length()).equals(word.substring(1)))
			return str.charAt(0) + word.substring(1);
		return "";
}

withoutX:

public String withoutX(String str) {
		if (str.length() == 0) return "";
		if (str.charAt(0) == 'x') str = str.substring(1);
		if (str.length() > 0 && str.charAt(str.length() - 1) == 'x')
			str = str.substring(0, str.length() - 1);
		return str;
}

withoutX2:

public String withoutX2(String str) {
		if (str.length() < 2) return "";

		String result = "";
		if (str.charAt(0) != 'x') result += str.charAt(0);
		if (str.charAt(1) != 'x') result += str.charAt(1);
		result += str.substring(2);
		return result;
}

The website said that “withoutX2” was a bit trickier than it looks like. However, the key is to not directly manipulate the input string but instead create a new one. Then this exercise is simpler than the preceding one.

CodingBat: Java. String-1, Part III

atFirst:

public String atFirst(String str) {
		if (str.length() == 0) return "@@";
		if (str.length() == 1) return str + "@";
		return str.substring(0, 2);
}

Let’s talk about style and conciseness for a moment. My solution of “atFirst” is probably a bit shorter than what some coding style guides would suggest. If you look at it, you’ll have no problem with the control flow, and it takes a mere three lines. Furthermore, there is no visual clutter.

On the other hand, some people tend to add if/else structures when they are unnecessary. This is a waste of time and screen space. But let’s just follow some “best practices” and keep the return statements on separate lines. The result is quite unsightly: Six lines instead of three, and two superfluous key words. Three versus six lines might not be a big absolute difference, but imagine you worked on a program that is several thousand lines long. You can easily waste a couple hundred lines like this.

public String atFirst(String str) {
		if (str.length() == 0)
			return "@@";
		else if (str.length() == 1)
			return str + "@";
		else
			return str.substring(0, 2);
}

If this wasn’t bad enough, you can of course add curly braces and end up with code that’s just painful to look at:

public String atFirst(String str) {
		if (str.length() == 0) {
			return "@@";
		} else if (str.length() == 1) {
			return str + "@";
		} else {
			return str.substring(0, 2);
		}
	}

This isn’t even the worst you could do. For instance, the so-called Allman style dictates that you put braces on separate lines.

lastChars:

public String lastChars(String a, String b) {
		if (a.length() == 0 && b.length() != 0)
			return "@" + "" + b.charAt(b.length() - 1);
		if (b.length() == 0 && a.length() != 0)
			return a.charAt(0) + "@";
		if (a.length() == 0 && b.length() == 0)
			return "@@";
		return a.charAt(0) + "" + b.charAt(b.length() - 1);
}

Without the empty string, the return value would be a numerical ASCII value.

conCat:

public String conCat(String a, String b) {
		if (a.length() != 0 && b.length() != 0
				&& a.charAt(a.length() - 1) == b.charAt(0))
			return a + b.substring(1);
		return a + b;
}

lastTwo:

public String lastTwo(String str) {
		if (str.length() < 2) return str;
		return str.substring(0, str.length()-2)
				+ str.charAt(str.length()-1) + str.charAt(str.length()-2);
}

seeColor:

public String seeColor(String str) {
		if (str.length() >= 3 && str.substring(0, 3).equals("red"))
			return "red";
		if (str.length() >= 4 && str.substring(0, 4).equals("blue"))
			return "blue";
		return "";
}

frontAgain:

public boolean frontAgain(String str) {
		int len = str.length();
		if (len < 2) return false;
		return str.substring(0, 2).equals(str.substring(len - 2));
}

minCat:

public String minCat(String a, String b) {
		int lenA = a.length();
		int lenB = b.length();
		if (lenA == lenB) return a + b;
		if (lenA > lenB) return a.substring(lenA - lenB) + b;
		return a + b.substring(lenB - lenA);
}

extraFront:

public String extraFront(String str) {
		String front = "";
		if (str.length() < 2) front = str;
		else front = str.substring(0, 2);
		return front + front + front;
}

without2:

public String without2(String str) {
		int len = str.length();
		if (len == 0 || len == 2) return "";
		if (len == 1) return str;
		if (str.substring(len - 2).equals(str.substring(0, 2)))
			return str.substring(2);
		return str;
}

deFront:

public String deFront(String str) {      
		if (str.charAt(0) == 'a' && str.charAt(1) != 'b')
			return str.charAt(0) + str.substring(2);
		if (str.charAt(0) != 'a' && str.charAt(1) == 'b')
			return str.substring(1);
		if (str.charAt(0) == 'a' && str.charAt(1) == 'b')
			return str;
		return str.substring(2);
}