Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

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

Open in new window

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

How to improve my design, approach, code? please advise
Avatar of CPColin
CPColin
Flag of United States of America image

Why are you checking for 12 and 14?
Avatar of gudii9

ASKER

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)
ASKER CERTIFIED SOLUTION
Avatar of CPColin
CPColin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of krakatoa
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.
"Immediately after" means "the next element in the array," in my eyes.
No, it doesn't necessarily. That's what's wrong with this nonsense.
Avatar of gudii9

ASKER

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      
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.
Avatar of gudii9

ASKER

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?
The challenge says nothing about numbers that come before 13.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

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

Open in new window


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	

Open in new window

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.
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.
Avatar of gudii9

ASKER

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

Open in new window


above passed all tests
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.
Avatar of gudii9

ASKER

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

Open in new window

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}
Avatar of gudii9

ASKER

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
I think since the second 13 is immediately following the first 13, you're adding it to sumBold twice.
Avatar of gudii9

ASKER

let me see
Avatar of gudii9

ASKER

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

Open in new window



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
Avatar of gudii9

ASKER

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

Open in new window


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