# sum13 challenge

Hi,

I am working on below challenge
http://codingbat.com/prob/p127384
I wrote my code as below

``````public int sum13(int[] nums) {
int sum=0;
for(int n:nums){
if(n!=13&&n!=12&&n!=14)
sum=sum+n;
}

return sum;
}
``````
I am not passing all tests

Expected      Run
sum13([1, 2, 2, 1]) → 6      6      OK
sum13([1, 1]) → 2      2      OK
sum13([1, 2, 2, 1, 13]) → 6      6      OK
sum13([1, 2, 13, 2, 1, 13]) → 4      6      X
sum13([13, 1, 2, 13, 2, 1, 13]) → 3      6      X
sum13([]) → 0      0      OK
sum13([13]) → 0      0      OK
sum13([13, 13]) → 0      0      OK
sum13([13, 0, 13]) → 0      0      OK
sum13([13, 1, 13]) → 0      1      X      //why expected 0 i thought this is expected 1??
sum13([5, 7, 2]) → 14      14      OK
sum13([5, 13, 2]) → 5      7      X
sum13([0]) → 0      0      OK
sum13([13, 0]) → 0      0      OK
other tests
X
Correct for more than half the tests

LVL 7
###### Who is Participating?

Senior Java ArchitectCommented:
I think it means "come immediately after a 13 [in the array]." So if there's a 13 in the array, the next element doesn't count. And there's no apparent reason to test for 12 at all.
0

Senior Java ArchitectCommented:
Why are you checking for 12 and 14?
0

Author Commented:
so it does not count and numbers that come immediately after a 13

as per challenge above immediately after numbers to 13 I thought are 14 and 13(I mean before and after numbers of 13 which are immediate ones)
0

Commented:
This is a stupid question in any case. What does "and numbers that come immediately after a 13 also do not count." mean ?? IF the array contains the number 13, and it is not the last member of the array, then does the question mean that only the next array index number should not count, or all the remaining numbers in all the remaining index positions ??

Like so many of these Codingbat questions, this is another mess.
0

Senior Java ArchitectCommented:
"Immediately after" means "the next element in the array," in my eyes.
0

Commented:
No, it doesn't necessarily. That's what's wrong with this nonsense.
0

Author Commented:
Return the sum of the numbers in the array, returning 0 for an empty array. Except the number 13 is very unlucky, so it does not count and numbers that come immediately after a 13 also do not count.

sum13([1, 2, 2, 1]) → 6
sum13([1, 1]) → 2
sum13([1, 2, 2, 1, 13]) → 6

descrition is not clear.
Expected      Run
sum13([1, 2, 2, 1]) → 6      6      OK
sum13([1, 1]) → 2      2      OK
sum13([1, 2, 2, 1, 13]) → 6      6      OK
sum13([1, 2, 13, 2, 1, 13]) → 4      6      X      //not sure how returning 4?
sum13([13, 1, 2, 13, 2, 1, 13]) → 3      6      X      //not sure how returning 3?
sum13([]) → 0      0      OK
sum13([13]) → 0      0      OK
sum13([13, 13]) → 0      0      OK
sum13([13, 0, 13]) → 0      0      OK
sum13([13, 1, 13]) → 0      1      X
sum13([5, 7, 2]) → 14      14      OK
sum13([5, 13, 2]) → 5      7      X
sum13([0]) → 0      0      OK
sum13([13, 0]) → 0      0      OK
other tests
X
0

Senior Java ArchitectCommented:
It means, if you find a 13 in the array, it doesn't count. The number immediately following the 13 also doesn't count.

So for this array, the bold elements count:

[1, 2, 13, 2, 1, 13]

Add them up and you get 4.
0

Author Commented:
The number immediately following the 13 also doesn't count.

what it means by above

[1, 2, 13, 2, 1, 13]
underline numbers come before 13 so they also should be ignore just like 13?
0

Senior Java ArchitectCommented:
The challenge says nothing about numbers that come before 13.
0

Commented:
A corollary to this exercise would be to sum all of the integers in the array, identify any that are 13 and any that follow 13 in the array and subtract them.

nums = [13, 1, 2, 13, 2, 1, 13] ==> sum = 45
identify 13 plus following
[13, 1, 2, 13, 2, 1, 13] ==bold numbers add up to 42
so sum13[13, 1, 2, 13, 2, 1, 13] returns 3 (45 - 42)
0

Commented:
I agree with Krakatoa that the description is not clear and subject to interpretation. But since they give several cases along with the expected result, you have to make a hypothesis about interpreting the wording of the description. After a couple of guesses, you come to the conclusion that the challenge meant to ignore the first number following 13 if there is a number.

In the job, you often get crappy description of what has to be done, and then I have to write up several guesses as to what the customer meant. Then they sometimes realize that what they wrote is crap and they revise it until it becomes clear.
0

Author Commented:
``````public int sum13(int[] nums) {
int sum=0;
int sumTotal=0;
int len=nums.length;
int sumNon13=0;
int sumNextTo13=0;
int sumAll=0;
int sumJunk=0;
int sumActual=0;
int result=0;

for(int i=0;i<len-1;i++){

if(nums[i]==13){
sumTotal=sumTotal+nums[i];
sumNon13=sumNon13+nums[i];
sumNextTo13=sumNextTo13+nums[i+1];
sumJunk=sumNon13+sumNextTo13;
sumActual=sumTotal-sumJunk;
result=sumActual;

} else {

sumAll=sumAll+nums[i];
result=sumAll;
//return sumAll;
}

}

return result;
}

``````

``````Expected	Run
sum13([1, 2, 2, 1]) → 6	5	X
sum13([1, 1]) → 2	1	X
sum13([1, 2, 2, 1, 13]) → 6	6	OK
sum13([1, 2, 13, 2, 1, 13]) → 4	6	X
sum13([13, 1, 2, 13, 2, 1, 13]) → 3	6	X
sum13([]) → 0	0	OK
sum13([13]) → 0	0	OK
sum13([13, 13]) → 0	-13	X
sum13([13, 0, 13]) → 0	0	OK
sum13([13, 1, 13]) → 0	1	X
sum13([5, 7, 2]) → 14	12	X
sum13([5, 13, 2]) → 5	-2	X
sum13([0]) → 0	0	OK
sum13([13, 0]) → 0	0	OK
other tests
X
``````
0

Senior Java ArchitectCommented:
You might be trying to do a little too much at once here, with all the adding and subtracting going on. Instead of running your code through all of the tests, pick one test and get it to pass.

First, since you're failing the tests that don't even have any 13's, you know that logic can't be right. Focus there, first, and get code working that successfully sums up the elements in an array. Then, add some code that skips elements that equal 13. That will let you pass tests where the 13 comes at the end of the array. Finally, add some more code that skips elements if they follow a 13.

When you get to that point, think about how best to skip the next element, when you see a 13. You could have a boolean whose value you change when you see a 13. You could also increment your index by an extra step when you see a 13.

But the key is to break the problem down and focus on one thing at a time.
0

Commented:
I agree with Krakatoa that the description is not clear and subject to interpretation

the confusion is in the fact that for me that the word 'number' is in the plural - numbers. It is mitigated by the word 'immediately' I grant, but numberS plural implies that it could apply to everything after the 13 - ie the rest of the array. Because anything in the very index position after a 13 is a NUMBER, not a NUMBERS.

Meticulousness in describing objectives is not always at its best in Codingbat.
0

Author Commented:
``````public static int sum13(int[] nums) {
int sum = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 13)
sum += nums[i];
else if (nums[i] == 13 && i < nums.length - 1) {
nums[i] = 0;
nums[i + 1] = 0;
}
}
return sum;
}
``````

above passed all tests
0

Senior Java ArchitectCommented:
That does pass all tests, but, in an actual project, other developers you work with might get mad at your code for modifying the array that's passed in.
0

Author Commented:
A corollary to this exercise would be to sum all of the integers in the array, identify any that are 13 and any that follow 13 in the array and subtract them.

nums = [13, 1, 2, 13, 2, 1, 13] ==> sum = 45
identify 13 plus following
[13, 1, 2, 13, 2, 1, 13] ==bold numbers add up to 42
so sum13[13, 1, 2, 13, 2, 1, 13] returns 3 (45 - 42)
i tried above approach passed almost all tests except one
``````public int sum13(int[] nums) {/*
* int sum = 0; for (int i = 0; i < nums.length;
* i++) { if (nums[i] != 13) sum += nums[i]; else if
* (nums[i] == 13 && i < nums.length - 1) { nums[i]
* = 0; nums[i + 1] = 0; } } return sum;
*/
int sum = 0;
int sumBold = 0;
for (int i = 0; i < nums.length; i++) {
/*
* if (i<nums.length){ sum += nums[i]; System.out.println(
* "sum is-->"+sum); } else
*/
sum += nums[i];
//	System.out.println("sum is-->" + sum);
if (nums[i] == 13)
if (i < nums.length-1) {
sumBold += nums[i] + nums[i + 1];
//		System.out.println("sumBold is-->" + sumBold);
} else {
sumBold += nums[i];
}
}
return (sum - sumBold);
}

``````
Expected      Run
sum13([1, 2, 2, 1]) → 6      6      OK
sum13([1, 1]) → 2      2      OK
sum13([1, 2, 2, 1, 13]) → 6      6      OK
sum13([1, 2, 13, 2, 1, 13]) → 4      4      OK
sum13([13, 1, 2, 13, 2, 1, 13]) → 3      3      OK
sum13([]) → 0      0      OK
sum13([13]) → 0      0      OK
sum13([13, 13]) → 0      -13      X
sum13([13, 0, 13]) → 0      0      OK
sum13([13, 1, 13]) → 0      0      OK
sum13([5, 7, 2]) → 14      14      OK
sum13([5, 13, 2]) → 5      5      OK
sum13([0]) → 0      0      OK
sum13([13, 0]) → 0      0      OK
other tests
OK

trying to see why one test alone failed with {13,13}
0

Author Commented:
That does pass all tests, but, in an actual project, other developers you work with might get mad at your code for modifying the array that's passed in.

I got you. How to fix above test. I am debugging in eclipse now to see why sum is coming only 13 instead of 26 and sumBold coming 26 hence difference is -13 instead of 0?
some edge case issue
0

Senior Java ArchitectCommented:
I think since the second 13 is immediately following the first 13, you're adding it to sumBold twice.
0

Author Commented:
let me see
0

Author Commented:
``````public class Sum13 {

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] ar = { 13, 13 };
System.out.println(sum13(ar));

}

// public static int sum13(int[] nums) {}
public static int sum13(
int[] nums) {/*
* int sum = 0; for (int i = 0; i < nums.length;
* i++) { if (nums[i] != 13) sum += nums[i]; else if
* (nums[i] == 13 && i < nums.length - 1) { nums[i]
* = 0; nums[i + 1] = 0; } } return sum;
*/
int sum = 0;
int sumBold = 0;
for (int i = 0; i < nums.length; i++) {
/*
* if (i<nums.length){ sum += nums[i]; System.out.println(
* "sum is-->"+sum); } else
*/
sum += nums[i];
System.out.println("sum is-->" + sum);
if (nums[i] == 13)
if (i < nums.length - 1) {
sumBold += nums[i] + nums[i + 1];
System.out.println("sumBold is-->" + sumBold);
} else {
sumBold += nums[i];
}
}
return (sum - sumBold);
}
}
``````

Above code gives below output
sum is-->13
sumBold is-->26
sum is-->26
-13

Sum is messed up. if i fix others are failing index out of bound
0

Author Commented:
if i comment last esle block getting right output for this tests

``````public class Sum13 {

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] ar = { 13, 13 };
System.out.println(sum13(ar));

}

// public static int sum13(int[] nums) {}
public static int sum13(
int[] nums) {/*
* int sum = 0; for (int i = 0; i < nums.length;
* i++) { if (nums[i] != 13) sum += nums[i]; else if
* (nums[i] == 13 && i < nums.length - 1) { nums[i]
* = 0; nums[i + 1] = 0; } } return sum;
*/
int sum = 0;
int sumBold = 0;
for (int i = 0; i < nums.length; i++) {
/*
* if (i<nums.length){ sum += nums[i]; System.out.println(
* "sum is-->"+sum); } else
*/
sum += nums[i];
System.out.println("sum is-->" + sum);
if (nums[i] == 13)
if (i < nums.length - 1) {
sumBold += nums[i] + nums[i + 1];
System.out.println("sumBold is-->" + sumBold);
} /*else {
sumBold += nums[i];
}*/
}
return (sum - sumBold);
}
}
``````

output is

sum is-->13
sumBold is-->26
sum is-->26
0

but other tests are failing

Expected      Run
sum13([1, 2, 2, 1]) → 6      6      OK
sum13([1, 1]) → 2      2      OK
sum13([1, 2, 2, 1, 13]) → 6      19      X
sum13([1, 2, 13, 2, 1, 13]) → 4      17      X
sum13([13, 1, 2, 13, 2, 1, 13]) → 3      16      X
sum13([]) → 0      0      OK
sum13([13]) → 0      13      X
sum13([13, 13]) → 0      0      OK
sum13([13, 0, 13]) → 0      13      X
sum13([13, 1, 13]) → 0      13      X
sum13([5, 7, 2]) → 14      14      OK
sum13([5, 13, 2]) → 5      5      OK
sum13([0]) → 0      0      OK
sum13([13, 0]) → 0      0      OK
other tests
X
0

Senior Java ArchitectCommented:
That makes me feel like that approach is too finicky to work for all cases. Have you tried the approach I laid out in this comment?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.