CodingBat: Java. Array-1, Part III

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

swapEnds:

public int[] swapEnds(int[] nums) {
int first = nums;
int last = nums[nums.length - 1];
nums = 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;
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, nums };
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;
if (a.length == 0) {
res = b;
res = b;
} else if (a.length == 1) {
res = a;
res = b;
} else {
res = a;
res = a;
}
return res;
}

front11:

public int[] front11(int[] a, int[] b) {
int[] one = new int;
int[] two = new int;
if (a.length == 0 && b.length == 0) {
return a;
}
if (a.length >= 1 && b.length == 0) {
one = a;
return one;
}
if (a.length >= 1 && b.length >= 1) {
two = a;
two = b;
return two;
}
if (a.length == 0 && b.length >= 1) {
one = b;
return one;
}
return a;
}

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

CodingBat: Java. Array-1, Part II

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

makeEnds:

public int[] makeEnds(int[] nums) {
int[] result = { nums, 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 == 1)
count++;
if (b.length > 0 && b == 1)
count++;
return count;
}

biggerTwo:

public int[] biggerTwo(int[] a, int[] b) {
if (a + a < b + b)
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, a, b, b };
return result;
}

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

CodingBat: Java. Array-1, Part I

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

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 == 6 || nums[nums.length - 1] == 6;
}

sameFirstLast:

public boolean sameFirstLast(int[] nums) {
return nums.length >= 1 && nums == 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 == b || a[a.length - 1] == b[b.length - 1];
}

sum3:

public int sum3(int[] nums) {
return nums + nums + nums;
}

rotateLeft3:

public int[] rotateLeft3(int[] nums) {
int[] rLeft = { nums, nums, nums };
return rLeft;
}

reverse3:

public int[] reverse3(int[] nums) {
int[] newOrder = { nums, nums, nums };
return newOrder;
}

maxEnd3:

public int[] maxEnd3(int[] nums) {
if (nums > nums) {
nums = nums;
nums = nums;
} else {
nums = nums;
nums = nums;
}
return nums;
}

sum2:

public int sum2(int[] nums) {
if (nums.length == 0)
return 0;
if (nums.length == 1)
return nums;
return nums + nums;
}

middleWay:

public int[] middleWay(int[] a, int[] b) {
int[] c = { a, b };
return c;
}

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

CodingBat: Java. String-1, Part IV

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

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; } [/sourcecode] 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.

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

CodingBat: Java. String-1, Part III

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

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);
}
&#91;/sourcecode&#93;

<b>seeColor:</b>

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));
}
&#91;/sourcecode&#93;

<b>minCat:</b>

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;
}
&#91;/sourcecode&#93;

<b>without2:</b>

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

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