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

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

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

Alex MyersI 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.

Gregor UlmPost authorThis 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.

MattFor scoresClump, only the following part is needed for the if statement as the scores are ordered:

if (scores[i+2]-scores[i]<=2){

Gregor UlmPost authorThanks for pointing this out. I’ll edit my solution.

RickygIn 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?

Michael PearsonFor scores increasing the for-loop has to have scores.increasing-1 not -2

Gregor UlmPost authorYou’re mistaken. I think you should spend some more time thinking about what the code does.

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

Shubhranshu MishraFor the scores program, I am wondering why it has to be scores.length – 2 instead of scores.length -1?

Gregor UlmPost authorWhat do you think will happen if you use “scores.length – 1”?

FrscoMy solution to scoresClump is slightly choppier and messier than yours, but it still works:

public boolean scoresClump(int[] scores) {

for (int i=0;i<scores.length-2;i++) {

if (scores[i+2] – scores[i+1] <= 2 && scores[i+2] – scores[i] <= 2) return true;

}

return false;

}

If there is a way to better optimize the flow of the code, let me know!