Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Hi,

I am working on below challenge

http://codingbat.com/prob/p127384

I wrote my code as below

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

How to improve my design, approach, code? please advise

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 testsExpected 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

How to improve my design, approach, code? please advise

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)

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

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

So for this array, the bold elements count:

[

Add them up and you get 4.

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

what it means by above

[1,

underline numbers come before 13 so they also should be ignore just like 13?

nums = [13, 1, 2, 13, 2, 1, 13] ==> sum = 45

identify 13 plus following

[

so sum13[13, 1, 2, 13, 2, 1, 13] returns 3 (45 - 42)

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.

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

i passed few other tests but failing below. please advise

```
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
```

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.

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.

```
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

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.i tried above approach passed almost all tests except one

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)

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

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

```
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

```
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

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.

All Courses

From novice to tech pro — start learning today.