My student has code that seems fine to me, but the tests fail and state that the R, G, and B values are 1 or 2 pixels low. Can anyone figure out the problem?
Hi @michael.fiscus, Thank you for bringing your question to the forum. I’m trying to get to the bottom of the issue. I think I have it narrowed down to when your student puts all of the differences in an int array and then traverses it. I can’t quite say what the exact issue is, but when I put each difference in the if-else if statement the algorithm works:
Pixel[][] pixels = getPixelsFromImage();
for (int row = 1; row < pixels.length; row++) {
for (int col = 1; col < pixels[0].length; col++) {
Pixel currentPixel = pixels[row][col];
Pixel topLeft = pixels[row - 1][col - 1];
int tempR = Math.abs(currentPixel.getRed() - topLeft.getRed());
int tempG = Math.abs(currentPixel.getGreen() - topLeft.getGreen());
int tempB = Math.abs(currentPixel.getBlue() - topLeft.getBlue());
int maxDiff = 0;
if(tempR > tempG && tempR > tempB){
maxDiff = tempR;
}
else if(tempG > tempR && tempG > tempB){
maxDiff = tempG;
}
else{
maxDiff = tempB;
}
// int[] temp = {tempR, tempG, tempB};
// int maxDiff = 0;
// for (int t : temp) {
// if (t > maxDiff) {
// maxDiff = t;
// }
// }
maxDiff += 128;
currentPixel.setRed(maxDiff);
currentPixel.setGreen(maxDiff);
currentPixel.setBlue(maxDiff);
}
}
You can see that I commented out the array portion and substituted the if-else if statement which works. I’ll keep playing with it and try to determine why the enhanced for-loop isn’t working.
Hi @michael.fiscus and @sam_stafford ,
I dug into this and I think I found the issue. Michael, I believe your student’s code works as the algorithm was intended, and the answer key, and tests have an issue- frustrating, I know, but you can share with your student that they completed the level correctly! I will contact support@code.org about fixing this level.
Here is what I did.
I uncommented the enhanced for loop part and gave it a different variable name to find the maximum difference, maxDiff2. I then added a print statement to see what was different and where.
This gave me console output that looked like this:
I noticed that maxDiff2 was always bigger than maxDiff when they didn’t match. Since we are looking for the maximum value out of the 3 that made me realize your student’s code was finding the bigger number and the answer key was not always finding the bigger number.
The problem with the answer key’s algorithm for finding the biggest number is when tempR and tempG are the same, and bigger than tempB. for example if :
tempR = 200
tempG = 200
tempB = 50
The if else statements in the initial code don’t work when there are duplicates because it uses the strictly greater than symbol >
since 200 is not > 200 the else will store the 50 in maxDiff, which is not the intent of the algorithm.
I changed the > to >= in each of the if else if conditions and the maxDiff and maxDiff2 values always matched.
Here is the code I used to wrap my head around this answer key issue:
Thank you, Michael for sharing your student’s code! I will alert the Code team about this issue.
Best,
Lindsay
Thanks for your work to figure this out! It seems obvious now that you have explained it. Your strategy for discovering the problem is awesome, and I will share it with my class!