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

One thought on “CodingBat: Java. String-1, Part III

  1. Randal Egan

    I know this was all posted years ago, but I still find it very useful. I hope other people still find this as useful as I do, and I’d like to provide a simplified solution to the lastChars() problem. I wanted to share this since, after completing almost all of the condingbat problems, this is the first time I have come up with a simpler solution than what’s posted on here.
    Here’s what I came up with:
    public String lastChars(String a, String b) {
    String first = a.length()>0 ? a.substring(0,1) : “@”;
    String last = b.length()>0 ? b.substring(b.length()-1) : “@”;
    return first + last;
    }
    Uses the ternary operator to make the code a bit easier to follow than a block of if/else statements.

    Reply

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.