For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.
scoresSpecial:
1 2 3 4 5 6 7 8 9 10 11 12 | public int scoresSpecial( int [] a, int [] b) { return largest(a) + largest(b); } public int largest( int [] array) { int result = 0 ; for ( int i = 0 ; i < array.length; i++) if (array[i] % 10 == 0 && array[i] > result) result = array[i]; return result; } |
sumHeights:
1 2 3 4 5 6 | public int sumHeights( int [] heights, int start, int end) { int sum = 0 ; for ( int i = start; i < end; i++) sum += Math.abs(heights[i] - heights[i + 1 ]); return sum; } |
sumHeights2:
1 2 3 4 5 6 7 8 9 | public int sumHeights2( int [] heights, int start, int end) { int sum = 0 ; for ( int i = start; i < end; i++) if (heights[i] < heights[i + 1 ]) sum += ( 2 * Math.abs(heights[i] - heights[i + 1 ])); else sum += Math.abs(heights[i] - heights[i + 1 ]); return sum; } |
bigHeights:
1 2 3 4 5 6 | public int bigHeights( int [] heights, int start, int end) { int count = 0 ; for ( int i = start; i < end; i++) if (Math.abs(heights[i] - heights[i + 1 ]) >= 5 ) count++; return count; } |
userCompare:
1 2 3 4 5 6 7 8 | public int userCompare(String aName, int aId, String bName, int bId) { if (aName.compareTo(bName) < 0 ) return - 1 ; if (aName.equals(bName)) { if (aId == bId) return 0 ; if (aId < bId) return - 1 ; } return 1 ; } |
mergeTwo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public String[] mergeTwo(String[] a, String[] b, int n) { String[] result = new String[n]; int indexResult = 0 ; int indexA = 0 ; int indexB = 0 ; while (indexResult < n) if (a[indexA].compareTo(b[indexB]) < 0 ) result[indexResult++] = a[indexA++]; else if (a[indexA].compareTo(b[indexB]) > 0 ) result[indexResult++] = b[indexB++]; else { // identical strings result[indexResult++] = a[indexA++]; indexB++; } return result; } |
commonTwo:
1 2 3 4 5 6 7 8 9 10 11 12 13 | public int commonTwo(String[] a, String[] b) { int count = 0 ; String lastChecked = null ; for ( int i = 0 ; i < a.length; i++) if (!a[i].equals(lastChecked)) for ( int j = 0 ; j < b.length; j++) if (a[i].equals(b[j])) { count++; lastChecked = a[i]; break ; } return count; } |
For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.
You solution to commonTwo does multiple passes through the b array. The instructions say to limit to a single pass. Here is a single pass solution:
//Done in C#… but easy to convert
int ca = 0, cb = 0, sum = 0;
bool flag = true;
while (flag)
{
if (a[ca] == b[cb]) { ca++; cb++; sum++; }
else if (a[ca] b[cb]) { cb++; }
if (ca == a.Length || cb == b.Length) { flag = false; }
}
return sum;
The above code got fowled by the site’s Bolding mechanism. I changed a and b to m and n to fix it. The correct code should be:
int ca = 0, cb = 0, sum = 0;
bool flag = true;
while (flag)
{
if (m[ca] == n[cb]) { ca++; cb++; sum++; }
else if (m[ca] n[cb]) { cb++; }
if (ca == m.Length || cb == n.Length) { flag = false; }
}
return sum;
Ahh the code still fowls — your editor has some serious flaws
See here for the correct code:
http://www.javaproblems.com/2013/11/java-ap-1-commontwo-codingbat-solution_18.html?showComment=1440960804261#c5485657438184916811
userCompare:
public int userCompare(String aName, int aId, String bName, int bId) {
if (aName.compareTo(bName) < 0) {
return -1;
}
if (Objects.equals(aName, bName)) {
return Integer.compare(aId, bId);
}
return 1;
}