CodingBat: Java. AP-1, Part III


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.


4 thoughts on “CodingBat: Java. AP-1, Part III

  1. Matt M.

    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;

    Reply
  2. Matt M.

    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;

    Reply
  3. Chris

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

    Reply

Leave a Reply to Matt M. Cancel 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.

This site uses Akismet to reduce spam. Learn how your comment data is processed.