Category Archives: CodingBat: Java

CodingBat: Java. Update 2013.2


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


The Array-2 section on Coding Bat saw an update as well. Those four exercises gradually build up to a variation of the well-known FizzBuzz problem.

All solutions were successfully tested on 29 March 2013.

fizzArray:

public int[] fizzArray(int n) {
	int[] result = new int[n];
	for (int i = 0; i < n; i++)
		result[i] = i;
	return result;
}

fizzArray2:

public String[] fizzArray2(int n) {
	String[] result = new String[n];
	for (int i = 0; i < n; i++)
		result[i] = String.valueOf(i);
	return result;
}

fizzArray3:

public int[] fizzArray3(int start, int end) {
	int n = end - start;
	int[] result = new int[n];

	for (int i = 0; i < n; i++)
		result[i] = start++;
	return result;
}

fizzBuzz:

public String[] fizzBuzz(int start, int end) {
	int n = end - start;
	String[] result = new String[n];

	int pos = 0;
	for (int i = start; i < end; i++) {
		boolean fizz = i % 3 == 0;
		boolean buzz = i % 5 == 0;

		if (fizz && buzz) result[pos] = "FizzBuzz";
		else if (fizz) result[pos] = "Fizz";
		else if (buzz) result[pos] = "Buzz";
		else result[pos] = String.valueOf(i);
		pos++;
	}
	return result;
}

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


CodingBat: Java. Update 2013.1


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


As I thought I was done with publishing my solutions to the entire Coding Bat: Java library of exercises, with a total of over 200, Nick Parlante updated his website and added some more exercises. Apparently, his aim was to thoroughly prepare students for “FizzBuzz” interview questions.

The first exercise was added to Warmup-1, the others to Logic-1.

All solutions were successfully tested on 28 March 2013.

or35:

public boolean or35(int n) {
	return n % 3 == 0 || n % 5 == 0;
}

more20:

public boolean more20(int n) {
	return n % 20 == 1 || n % 20 == 2;
}

old35:

public boolean old35(int n) {
	return n % 3 == 0 ^ n % 5 == 0;
}

specialEleven:

public boolean specialEleven(int n) {
	return n % 11 == 0 || n % 11 == 1;
}

less20:

public boolean less20(int n) {
	return (n + 1) % 20 == 0 || (n + 2) % 20 == 0;
}

fizzString:

public String fizzString(String str) {
	boolean fizz = str.charAt(0) == 'f';
	boolean buzz = str.charAt(str.length() - 1) == 'b';

	if (fizz && buzz) return "FizzBuzz";
	if (fizz) return "Fizz";
	if (buzz) return "Buzz";
	return str;
}

fizzString2:

public String fizzString2(int n) {
	boolean fizz = n % 3 == 0;
	boolean buzz = n % 5 == 0;

	if (fizz && buzz) return "FizzBuzz!";
	if (fizz) return "Fizz!";
	if (buzz) return "Buzz!";
	return n + "!";
}

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


CodingBat: Java. Recursion-2


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


Finally we’ve reached the last section of Coding Bat: Recursion-2. Those nine exercises are all very similar. Once you’ve figured out one you can probably solve the others immediately.

I’ll just walk you through the general strategy, using the first exercise as an example: The method “groupSum” takes three arguments. The first, “start”, is the index of an array of integers, the second is the array of integers in question, and the third, “target” is the target value. Now, “start” increases by one with each recursive call. If this value is greater than or equal to the length of the array, then the entire array has been processed. At this point, only the evaluation step has to be done, which consists of checking whether the target value is zero.

“Why zero?”, you may ask. If “target” equals zero, then it is possible to pick a group of integers from the array that sum up to the target value. The calculation is done by selecting each value of the array and in one case subtracting it from “target”, while in the other leaving “target” unchanged. Thus, all possible combinations will eventually be checked. Finally, the method returns “true” once one of the results is “true”, which is a property of short-circuit evaluation of logical disjunctions.

All solutions were successfully tested on 28 March 2013.

groupSum:

public boolean groupSum(int start, int[] nums, int target) {
	if (start >= nums.length) return target == 0;
	return groupSum(start + 1, nums, target - nums[start])
			|| groupSum(start + 1, nums, target);
}

groupSum6:

public boolean groupSum6(int start, int[] nums, int target) {
	if (start >= nums.length) return target == 0;
	if (nums[start] == 6)
		return groupSum6(start + 1, nums, target - nums[start]);
	return groupSum6(start + 1, nums, target - nums[start])
			|| groupSum6(start + 1, nums, target);
}

groupNoAdj:

public boolean groupNoAdj(int start, int[] nums, int target) {
	if (start >= nums.length) return target == 0;
	return groupNoAdj(start + 2, nums, target - nums[start])
			|| groupNoAdj(start + 1, nums, target);
}

groupSum5:

public boolean groupSum5(int start, int[] nums, int target) {
	if (start >= nums.length) return target == 0;
	if (nums[start] % 5 == 0) {
		if (start < nums.length - 1 && nums&#91;start + 1&#93; == 1)
			return groupSum5(start + 2, nums, target - nums&#91;start&#93;);
		return groupSum5(start + 1, nums, target - nums&#91;start&#93;);
	}
	return groupSum5(start + 1, nums, target - nums&#91;start&#93;)
			|| groupSum5(start + 1, nums, target);
}
&#91;/sourcecode&#93;





<b>groupSumClump:</b>
[sourcecode language="Java" gutter="true"]
public boolean groupSumClump(int start, int[] nums, int target) {
	if (start >= nums.length) return target == 0;

	int sum = nums[start];
	int count = 1;
	for (int i = start + 1; i < nums.length; i++)
		if (nums&#91;i&#93; == nums&#91;start&#93;) {
			sum += nums&#91;i&#93;;
			count++;
		}
	return groupSumClump(start + count, nums, target - sum)
			|| groupSumClump(start + count, nums, target);
}
&#91;/sourcecode&#93;








<b>splitArray:</b>
[sourcecode language="Java" gutter="true"]
public boolean splitArray(int[] nums) {
	return helper(0, nums, 0, 0);
}

private boolean helper(int start, int[] nums, int sum1, int sum2) {
	if (start >= nums.length) return sum1 == sum2;
	return helper(start + 1, nums, sum1 + nums[start], sum2)
			|| helper(start + 1, nums, sum1, sum2 + nums[start]);
}

splitOdd10:

public boolean splitOdd10(int[] nums) {
	return helper(0, nums, 0, 0);
}

private boolean helper(int start, int[] nums, int sum1, int sum2) {
	if (start >= nums.length)
		return sum1 % 10 == 0 && sum2 % 2 == 1 || sum1 % 2 == 1
				&& sum2 % 10 == 0;
	return helper(start + 1, nums, sum1 + nums[start], sum2)
			|| helper(start + 1, nums, sum1, sum2 + nums[start]);
}

split53:

public boolean split53(int[] nums) {
	return helper(0, nums, 0, 0);
}

private boolean helper(int start, int[] nums, int sum1, int sum2) {
	if (start >= nums.length) return sum1 == sum2;
	if (nums[start] % 5 == 0)
		return helper(start + 1, nums, sum1 + nums[start], sum2);
	if (nums[start] % 3 == 0)
		return helper(start + 1, nums, sum1, sum2 + nums[start]);

	return helper(start + 1, nums, sum1 + nums[start], sum2)
			|| helper(start + 1, nums, sum1, sum2 + nums[start]);
}

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


CodingBat: Java. Recursion-1, Part III


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


countPairs:

public int countPairs(String str) {
	if (str.length() < 3) return 0;
	if (str.charAt(0) == str.charAt(2))
		return 1 + countPairs(str.substring(1));
	return countPairs(str.substring(1));
}

countAbc:

public int countAbc(String str) {
	if (str.length() < 3) return 0;
	if (str.substring(0, 3).equals("abc") 
			|| str.substring(0, 3).equals("aba"))
		return 1 + countAbc(str.substring(1));
	return countAbc(str.substring(1));
}

count11:

public int count11(String str) {
	if (str.length() < 2) return 0;
	if (str.substring(0, 2).equals("11"))
		return 1 + count11(str.substring(2));
	return count11(str.substring(1));
}

stringClean:

public String stringClean(String str) {
	if (str.length() < 2) return str;
	if (str.charAt(0) == str.charAt(1))
		return stringClean(str.substring(1));
	return str.charAt(0) + stringClean(str.substring(1));
}

countHi2:

public int countHi2(String str) {
	if (str.length() < 2) return 0;
	if (str.substring(0, 2).equals("hi"))
		return 1 + countHi2(str.substring(2));
	if (str.charAt(0) == 'x' && str.length() >= 3) {
		if (str.substring(1, 3).equals("hi"))
			return countHi2(str.substring(3));
		return countHi2(str.substring(1));
	}
	return countHi2(str.substring(1));
}

parenBit:

public String parenBit(String str) {
	if (!str.substring(0, 1).equals("("))
		return parenBit(str.substring(1));
	return (str.substring(0, str.indexOf(")") + 1));
}

nestParen:

public boolean nestParen(String str) {
	if (str.equals("") || str.equals("()")) return true;
	if (str.charAt(0) == '(' && str.charAt(str.length() - 1) == ')')
		return nestParen(str.substring(1, str.length() - 1));
	return false;
}

strCount:

public int strCount(String str, String sub) {
	if (str.length() < sub.length()) return 0;
	if (str.substring(0, sub.length()).equals(sub))
		return 1 + strCount(str.substring(sub.length()), sub);
	return strCount(str.substring(1), sub);
}

strCopies:

public boolean strCopies(String str, String sub, int n) {
	if (str.length() < sub.length()) return (n <= 0);
	if (str.substring(0, sub.length()).equals(sub))
		return strCopies(str.substring(1), sub, n - 1);
	return strCopies(str.substring(1), sub, n);
}

strDist:

public int strDist(String str, String sub) {
	if (str.indexOf(sub) == -1) return 0;
	if (str.substring(0, sub.length()).equals(sub)
			&& str.substring(str.length() - sub.length())
			.equals(sub))
		return str.length();
	if (!str.substring(0, sub.length()).equals(sub))
		return strDist(str.substring(1), sub);
	// case: (!str.substring(str.length()-sub.length()).equals(sub))
	return strDist(str.substring(0, str.length() - 1), sub);
}

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


CodingBat: Java. Recursion-1, Part II


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


countHi:

public int countHi(String str) {
	if (str.length() < 2) return 0;
	if (str.substring(0, 2).equals("hi"))
		return 1 + countHi(str.substring(2));
	return countHi(str.substring(1));
}

changeXY:

public String changeXY(String str) {
	if (str.length() == 0) return str;
	if (str.charAt(0) == 'x') return "y" + changeXY(str.substring(1));
	return str.charAt(0) + changeXY(str.substring(1));
}

changePi:

public String changePi(String str) {
	if (str.length() < 2) return str;
	if (str.substring(0, 2).equals("pi"))
		return "3.14" + changePi(str.substring(2));
	return str.charAt(0) + changePi(str.substring(1));
}

noX:

public String noX(String str) {
	if (str.length() == 0) return "";
	if (str.charAt(0) == 'x') return noX(str.substring(1));
	return str.charAt(0) + noX(str.substring(1));
}

array6:

public boolean array6(int[] nums, int index) {
	if (nums.length == 0) return false;
	if (index == nums.length - 1) return nums[index] == 6;
	if (nums[index] == 6) return true;
	return array6(nums, index + 1);
}

array11:

public int array11(int[] nums, int index) {
	if (index == nums.length) return 0;
	if (nums[index] == 11) return 1 + array11(nums, index + 1);
	return array11(nums, index + 1);
}

array220:

public boolean array220(int[] nums, int index) {
	if (nums.length < 2 || index == nums.length - 1) return false;
	if (nums[index + 1] == nums[index] * 10) return true;
	return array220(nums, index + 1);
}

allStar:

public String allStar(String str) {
	if (str.length() <= 1) return str;
	return str.charAt(0) + "*" + allStar(str.substring(1));
}

pairStar:

public String pairStar(String str) {
	if (str.length() < 2) return str;
	if (str.charAt(0) == str.charAt(1))
		return str.charAt(0) + "*" + pairStar(str.substring(1));
	return str.charAt(0) + pairStar(str.substring(1));
}

endX:

public String endX(String str) {
	if (str.length() == 0) return str;
	if (str.charAt(0) == 'x')
		return endX(str.substring(1)) + "x";
	return str.charAt(0) + endX(str.substring(1));
}

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