Monthly Archives: January 2013

CodingBat: Java. String-1, Part II

left2:

public String left2(String str) {
		return str.substring(2) + str.substring(0, 2);
}

right2:

public String right2(String str) {
		int len = str.length();
		return str.substring(len - 2) + str.substring(0, len - 2);
}

theEnd:

public String theEnd(String str, boolean front) {
		if (front) return str.substring(0, 1);
		return str.substring(str.length() - 1);
}

withouEnd2:

public String withouEnd2(String str) {
		if (str.length() <= 2) return "";
		return str.substring(1, str.length() - 1);
}

middleTwo:

public String middleTwo(String str) {
		int len = str.length();
		return str.substring(len / 2 - 1, len / 2 + 1);
}

endsLy:

public boolean endsLy(String str) {
		int len = str.length();
		if (len < 2) return false;
		return (str.substring(len - 2).equals("ly"));
}

nTwice:

public String nTwice(String str, int n) {
		return str.substring(0, n) + str.substring(str.length() - n);
}

twoChar:

public String twoChar(String str, int index) {
		if (index < 0 || index + 2 > str.length())
			return str.substring(0, 2);
		return str.substring(index, index + 2);
}

middleThree:

public String middleThree(String str) {
		int len = str.length();
		if (len == 3) return str;
		return str.substring(len / 2 - 1, len / 2 + 2);
}

hasBad:

public boolean hasBad(String str) {
		if (str.length() <= 2) return false;
		if (str.length() == 3) return str.substring(0, 3).equals("bad");
		return str.substring(0, 3).equals("bad")
				|| str.substring(1, 4).equals("bad");
}

CodingBat: Java. String-1, Part I

In the String-1 section on CodingBat you have the chance to familiarize yourself with basic string operations. The exercises do get a bit repetitive, but you should be able to quickly go through all of them and move on to more challenging parts.

All solutions were successfully tested on 19 January 2013.

helloName:

public String helloName(String name) {
		return "Hello " + name + "!";
}

makeAbba:

public String makeAbba(String a, String b) {
		return a + b + b + a;
}

makeTags:

public String makeTags(String tag, String word) {
		return "<" + tag + ">" + word + "</" + tag + ">";
}

makeOutWord:

public String makeOutWord(String out, String word) {
		return out.substring(0,2) + word + out.substring(2);
}

extraEnd:

public String extraEnd(String str) {
		int len = str.length();
		return str.substring(len - 2) + str.substring(len - 2)
				+ str.substring(len - 2);
}

firstTwo:

public String firstTwo(String str) {
		if (str.length() < 2) return str;
		return str.substring(0, 2);
}

firstHalf:

public String firstHalf(String str) {
		return str.substring(0, str.length()/2);
}

withoutEnd:

public String withoutEnd(String str) {
		return str.substring(1, str.length() - 1);
}

comboString:

public String comboString(String a, String b) {  
		if (a.length() > b.length()) return b + a + b;
		return a + b + a;
}

nonStart:

public String nonStart(String a, String b) {
		return a.substring(1) + b.substring(1);
}

CodingBat: Java. Warmup-2, Part II

Here is the second part of the solutions to Warmup-2:

stringMatch:

public int stringMatch(String a, String b) {
		int count = 0;
		int minLength = Math.min(a.length(), b.length());
		for (int i = 0; i < minLength - 1; i++) {
			if (a.substring(i, i + 2).equals(b.substring(i, i + 2)))
				count++;
		}
		return count;
}

stringX:

public String stringX(String str) {
		String result = "";
		if (str.length() <= 2) return str;
		
		result += str.charAt(0);
		for (int i = 1; i < str.length() - 1; i++) {
			if (str.charAt(i) != 'x') result += str.charAt(i);
		}
		result += str.charAt(str.length() - 1);
		return result;
}

altPairs:

public String altPairs(String str) {
		String result = "";
		for (int i = 0; i < str.length(); i += 4) {
			result += str.charAt(i);
			if (i + 1 < str.length())
				result += str.charAt(i + 1);
		}
		return result;
}

stringYak:

public String stringYak(String str) {
		int posYak = str.indexOf("yak");
		while (posYak != -1) {
			str = str.substring(0, posYak) + str.substring(posYak + 3);
			posYak = str.indexOf("yak");
		}
		return str;
  }

There is a mistake in the description on the website, which says “Given a string, return a version where all the “yak” are removed, but the “a” can be any char.” All test cases use “yak”, though, which simplifies the solution.

Also, please note that the purpose of exercises such as this one is to familiarize yourself with loops. Using str.replace() would completely defeat this purpose.

array667:

public int array667(int[] nums) {
		int count = 0;
		for (int i = 0; i < nums.length - 1; i++)
			if (nums[i] == 6 && (nums[i + 1] == 6 || nums[i + 1] == 7))
				count++;
		return count;
}

noTriples:

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

has271:

public boolean has271(int[] nums) {
		for (int i = 0; i < nums.length - 2; i++)
			if (nums[i] == nums[i + 1] - 5
					&& (Math.abs((nums[i + 2] + 1) - nums[i]) <= 2))
				return true;
		return false;
}

CodingBat: Java. Warmup-2, Part I

The Warmup-2 section on CodingBat gently introduces string and array loops. There wasn’t much to comment since the solutions are all relatively straight-forward. All solutions were successfully tested on 13 January 2013.

stringTimes:

public String stringTimes(String str, int n) {
		String result = "";
		for (int i = 0; i < n; i++)
			result += str;
		return result;
}

frontTimes:

public String frontTimes(String str, int n) {
		String result = "";
		if (str.length() <= 3)
			for (int i = 0; i < n; i++)
				result += str;
		else
			for (int i = 0; i < n; i++)
				result += str.substring(0, 3);
		return result;
}

countXX:

int countXX(String str) {
		int count = 0;
		for (int i = 0; i < str.length() - 1; i++)
			if (str.substring(i, i + 2).equals("xx"))
				count++;
		return count;
}

doubleX:

boolean doubleX(String str) {
		int firstX = str.indexOf('x');
		if (firstX < str.length() - 1)
			return str.charAt(firstX + 1) == 'x';
		return false;
}

stringBits:

public String stringBits(String str) {
		String result = "";
		for (int i = 0; i < str.length(); i++) {
			if (i % 2 == 0)
				result += str.charAt(i);
		}
		return result;
}

stringSplosion:

public String stringSplosion(String str) {
		String result = "";
		for (int i = 0; i <= str.length(); i++) {
			result += str.substring(0, i);
		}
		return result;
}

last2:

public int last2(String str) {
		int count = 0;
		for (int i = 0; i < str.length() - 2; i++) {
			if (str.substring(i, i + 2).equals(
                                str.substring(str.length() - 2)))
				    count++;
		}
		return count;
}

arrayCount9:

public int arrayCount9(int[] nums) {
		int result = 0;
		for (int i = 0; i < nums.length; i++) {
			if (nums[i] == 9)
				result++;
		}
		return result;
}

arrayFront9:

public boolean arrayFront9(int[] nums) {
		int check = (nums.length < 4) ? nums.length : 4;
		for (int i = 0; i < check; i++) {
			if (nums[i] == 9) return true;
		}
		return false;
}

I use the ternary operator for the sake of brevity. You can of course expand the assignment operation into an if/else block.

array123:

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

CodingBat: Java. Warmup-1, Part III

Here are the final ten exercises from the Warmup-1 section of CodingBat.

mixStart:

	public boolean mixStart(String str) {
		return str.length() >= 3 && str.substring(1, 3).equals("ix");
	}

startOz:

	public String startOz(String str) {
		String result = "";
		if (str.length() > 0 && str.charAt(0) == 'o') result += 'o';
		if (str.length() > 1 && str.charAt(1) == 'z') result += 'z';
		return result;
	}

intMax:

	public int intMax(int a, int b, int c) {
		int max = a;
		if (b > max) max = b;
		if (c > max) max = c;		
		return max;
	}

The solution on the website first compares a and b and assigns the greater value to the variable max. This step can be condensed to assigning a to max right away and then check whether b is greater than max.

close10:

	public int close10(int a, int b) {
		if (Math.abs(10 - a) == Math.abs(10 - b)) return 0;
		if (Math.abs(10 - a) < Math.abs(10 - b)) return a;
		return b;
	}

in3050:

	public boolean in3050(int a, int b) {
		boolean condition1 = a >= 30 && a <= 40 && b >= 30 && b <= 40;
		boolean condition2 = a >= 40 && a <= 50 && b >= 40 && b <= 50;
		return condition1 || condition2;
	}

max1020:

	public int max1020(int a, int b) {
		if (b < a) {
			int temp = a;
			a = b;
			b = temp;
		}
		boolean aInRange = a >= 10 && a <= 20;
		boolean bInRange = b >= 10 && b <= 20;
		if (aInRange && bInRange || !aInRange && bInRange)
			return b;
		if (aInRange && !bInRange)
			return a;
		return 0;
	}

stringE:

	public boolean stringE(String str) {
		int count = 0;
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == 'e') count++;
		}
		return (count >= 1 && count <= 3);
	}

lastDigit:

	public boolean lastDigit(int a, int b) {
		return (Math.abs(a - b) % 2 == 0);
	}

endUp:

	public String endUp(String str) {
		if (str.length() <= 3) return str.toUpperCase();
		return str.substring(0, str.length() - 3)
				+ str.substring(str.length() - 3).toUpperCase();
	}

everyNth:

	public String everyNth(String str, int n) {
		String result = "";
		for (int i = 0; i < str.length(); i++) {
			if (i % n == 0) result += str.charAt(i);
		}
		return result;
	}