CodingBat: Java. Warmup-1, Part II

This post continues where the last one left off. It contains solutions to another ten exercises, most of which are straightforward.

frontBack:

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

front3:

	public String front3(String str) {
		int length = str.length();
		if (length <= 3) return str + str + str;
		String front = str.substring(0, 3);
		return front + front + front;
	}

backAround:

	public String backAround(String str) {
		char add = str.charAt(str.length() - 1);
		return add + str + add;
	}

front22:

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

startHi:

	public boolean startHi(String str) {
		if (str.length() < 2)
			return false;
		return str.substring(0, 2).equals("hi");
	}

icyHot:

	public boolean icyHot(int temp1, int temp2) {
		return (temp1 < 0 && temp2 > 100 || temp1 > 100 && temp2 < 0);
	}

in1020:

	public boolean in1020(int a, int b) {
		return (a >= 10 && a <= 20 || b >= 10 && b <= 20);
	}

hasTeen:

	public boolean hasTeen(int a, int b, int c) {
		return (a >= 13 && a <= 19 ||
				b >= 13 && b <= 19 ||
				c >= 13 && c <= 19);
	}

loneTeen:

	public boolean loneTeen(int a, int b) {
		int count = 0;
		if (a >= 13 && a <= 19) count++;
		if (b >= 13 && b <= 19) count++;
		return count == 1;
	}

This solution might strike you as odd at first. My goal was to avoid a cumbersome boolean expression. Thus, I maintain a counter that increases by one if one of the integers is in the specified range. If the value of count is “1”, then the boolean XOR condition is fulfilled. You’ll probably agree that this approach is slightly more elegant than the solution on the website:

	public boolean loneTeen(int a, int b) {
		boolean aTeen = (a >= 13 && a <= 19);
		boolean bTeen = (b >= 13 && b <= 19);
		return (aTeen && !bTeen) || (!aTeen && bTeen);
		// Translation: one or the other, but not both.
		// Alternately could use the Java XOR operator, but it's obscure.
	}

The XOR operator (^) wasn’t mentioned on the CodingBat website. If you use it, the solution shrinks down to just one line:

	public boolean loneTeen(int a, int b) {
		return (a >= 13 & a <= 19) ^ (b >= 13 && b <= 19);
	}

delDel:

	public String delDel(String str) {
		if (str.length() > 3 && str.substring(1,4).equals("del"))
			return str.charAt(0) + str.substring(4);
		return str;
	}

3 thoughts on “CodingBat: Java. Warmup-1, Part II

  1. Ryan Memmott

    For front3 I would not store str.length () in a variable since it is only called once. In addition since we only care about the first 3 existing place of the string, we can recycle the existing variable str to only contain the part of the string we need. This also saves a line of code.
    if (str.length() >= 3) str=str.substring(0, 3);
    return str + str + str;

    Reply
  2. John Doe

    Could someone explain me the loneTeen problem?

    I mean if the teen number is in the variable c instead of the other ones how does it get checked if it’s in there?

    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.