CodingBat: Java. AP-1, Part I

After the more demanding Array-3 section, AP-1 on CodingBat is much more relaxing as it reviews basic programming concepts, with a strong focus on array processing.

All solutions were successfully tested on 16 March 2013.

scoresIncreasing:

public boolean scoresIncreasing(int[] scores) {
	for (int i = 0; i <= scores.length - 2; i++)
		if (scores[i] > scores[i + 1]) return false;
	return true;
}

scores100:

public boolean scores100(int[] scores) {  
	for (int i = 0; i <= scores.length - 2; i++)
		if (scores[i] == 100 && scores[i + 1] == 100) return true;
	return false;
}

scoresClump:

public boolean scoresClump(int[] scores) {
	if (scores.length > 2) {
		for (int i = 0; i < scores.length - 2; i++) {
			if (scores[i + 2] - scores[i] <= 2)
				return true;
		}
	}
	return false;
}

scoresAverage:

public int scoresAverage(int[] scores) {
	int first = average(scores, 0, scores.length / 2);
	int second = average(scores, scores.length / 2, scores.length);
	return Math.max(first, second);
}

private int average(int[] scores, int start, int end) {
	int sum = 0;
	int count = 0;
	for (int i = start; i < end; i++) {
		sum += scores[i];
		count++;
	}
	return sum / count;
}

wordsCount:

public int wordsCount(String[] words, int len) {
	int count = 0;
	for (int i = 0; i < words.length; i++)
		if (words[i].length() == len) count++;
	return count;
}

wordsFront:

public String[] wordsFront(String[] words, int n) {
	String[] newWords = new String[n];
	for (int i = 0; i < n; i++)
		newWords[i] = words[i];
	return newWords;
}

wordsWithoutList:

public List wordsWithoutList(String[] words, int len) {
	List result = new ArrayList();
	for (int i = 0; i < words.length; i++)
		if (words[i].length() != len)
			result.add(words[i]);
	return result;
}

8 thoughts on “CodingBat: Java. AP-1, Part I

  1. Alex Myers

    I have a question about the scoresIncreasing. I tried doing it the other way and said
    if(scores[i] < scores[i + 1])
    return true;
    and put return false outside of the loop. However when i did this some of the outputs were wrong.
    Why is this?

    thank you.

    Reply
    1. Gregor Ulm Post author

      This doesn’t work because you will then return ‘true’ as soon as you’ve found one (!) instance where the scores are increasing. You’re supposed to check whether the scores are increasing in the entire array, though.

      Reply
  2. Matt

    For scoresClump, only the following part is needed for the if statement as the scores are ordered:
    if (scores[i+2]-scores[i]<=2){

    Reply
      1. Rickyg

        In ScoresClump, the if statement is redundant. Isn’t it? If the length is less than 3, it won’t get into for statement and return false anyway, right?

        Reply
  3. Rickyg

    On scores average problem, what do you think of eliminating the counter (count) and replacing it with (end-start) on return statement?

    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.