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

asked on

sum67 challenge

Hi,

I am working on below challenge

http://codingbat.com/prob/p111327
I wrote my code as below

public int sum67(int[] nums) {
  
  

  int sum=0;
  for(int n:nums){
   if(n!=6&&n!=12&&n!=7)
   sum=sum+n;
  
  }
   return sum;


}

Open in new window




I am not passing all tests

xpected      Run            
sum67([1, 2, 2]) → 5      5      OK      
sum67([1, 2, 2, 6, 99, 99, 7]) → 5      203      X      
sum67([1, 1, 6, 7, 2]) → 4      4      OK      
sum67([1, 6, 2, 2, 7, 1, 6, 99, 99, 7]) → 2      204      X      
sum67([1, 6, 2, 6, 2, 7, 1, 6, 99, 99, 7]) → 2      204      X      
sum67([2, 7, 6, 2, 6, 7, 2, 7]) → 18      6      X      
sum67([2, 7, 6, 2, 6, 2, 7]) → 9      6      X      
sum67([1, 6, 7, 7]) → 8      1      X      
sum67([6, 7, 1, 6, 7, 7]) → 8      1      X      
sum67([6, 8, 1, 6, 7]) → 0      9      X      
sum67([]) → 0      0      OK      
sum67([6, 7, 11]) → 11      11      OK      
sum67([11, 6, 7, 11]) → 22      22      OK      
sum67([2, 2, 6, 7, 7]) → 11      4      X      
other tests
X      


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 n != 12?
Avatar of gudii9

ASKER

I have solidified my knowledge on String related concept( which I think to lot extent but there is always room to learn and grow and practice though). As next progression moving to array challenges now to solidify knowledge on arrays as well then recursion maps as per coding bat order.
Avatar of gudii9

ASKER


Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to the next 7 (every 6 will be followed by at least one 7). Return 0 for no numbers.

sum67([1, 2, 2]) → 5
sum67([1, 2, 2, 6, 99, 99, 7]) → 5
sum67([1, 1, 6, 7, 2]) → 4

i was not very clear on this challenge description.

Are they saying every 6 must immediately followed by one 7 (we have to return such 6 count??)
They're saying every six is eventually followed by a seven, not immediately.
Avatar of gudii9

ASKER

sum67([1, 2, 2, 6, 99, 99, 7]) → 5

how above is 5?
i see they ignored 6 and 7 but why they ignored both 99?
It wants you to ignore everything between the 6 and the 7, too.
Avatar of gudii9

ASKER

oh
Avatar of gudii9

ASKER

public int sum67(int[] nums) {
  
  

  int sum=0;
  int len=nums.length;
  for(int i=0;i<len;i++){
   if(nums[i]!=6&&nums[i]!=7){
   sum=sum+nums[i];
   }
   else if(nums[i]==6&&nums[i+1]==7){
   sum=sum+nums[i];
   }
  
  }
   return sum;


}

Open in new window


i got challenge. i improved success rate still need make it more granular code to pass all
Expected      Run            
sum67([1, 2, 2]) → 5      5      OK      
sum67([1, 2, 2, 6, 99, 99, 7]) → 5      203      X      
sum67([1, 1, 6, 7, 2]) → 4      10      X      
sum67([1, 6, 2, 2, 7, 1, 6, 99, 99, 7]) → 2      204      X      
sum67([1, 6, 2, 6, 2, 7, 1, 6, 99, 99, 7]) → 2      204      X      
sum67([2, 7, 6, 2, 6, 7, 2, 7]) → 18      12      X      
sum67([2, 7, 6, 2, 6, 2, 7]) → 9      6      X      
sum67([1, 6, 7, 7]) → 8      7      X      
sum67([6, 7, 1, 6, 7, 7]) → 8      13      X      
sum67([6, 8, 1, 6, 7]) → 0      15      X      
sum67([]) → 0      0      OK      
sum67([6, 7, 11]) → 11      17      X      
sum67([11, 6, 7, 11]) → 22      28      X      
sum67([2, 2, 6, 7, 7]) → 11      10      X      
other tests
X      
Avatar of phoffric
phoffric

What debugger are you using to step through your code?
If you are not using a debugger, then you must learn how to use one ASAP.
Avatar of gudii9

ASKER

I am using eclipse built in debugger
SOLUTION
Avatar of awking00
awking00
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
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

Do you know the answer to this case?
1 1 6 2 3 4 5 8 9 7 1
1+1+7+1=10
Nope
Avatar of gudii9

ASKER

oops
1+1+1=3 as i have to exclude all in between 6 and 7 including them as well
Avatar of gudii9

ASKER

public class Sum67 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] ar = { 1, 2, 2, 6, 99, 99, 7, 3, 9 };
		System.out.println("value is" + sum67(ar));

	}

	public static int sum67(int[] nums) {

		int sum = 0;
		int len = nums.length;
		int sixPos=0;
		int sevenPos=0;
		int sum1=0;
		int sum2=0;
	
		for (int i = 0; i < len; i++) {
			if (nums[i] == 6){
				 sixPos=i;
			}
			
			if (nums[i] ==7){
				 sevenPos=i;
			}		
		}
		for (int i = 0; i < sixPos; i++) {
			sum1=sum1+nums[i];
		}
		for (int i = sevenPos; i < len; i++) {
			sum2=sum2+nums[i];
		}
		return sum1+sum2;

}

}

Open in new window

something like above?
Avatar of gudii9

ASKER

i got correct output as
value is24

while debugging on eclipse
Avatar of gudii9

ASKER

public int sum67(int[] nums) {

		int sum = 0;
		int len = nums.length;
		int sixPos=0;
		int sevenPos=0;
		int sum1=0;
		int sum2=0;
	
		for (int i = 0; i < len; i++) {
			if (nums[i] == 6){
				 sixPos=i;
			}
			
			if (nums[i] ==7){
				 sevenPos=i;
			}		
		}
		for (int i = 0; i < sixPos; i++) {
			sum1=sum1+nums[i];
		}
		for (int i = sevenPos; i < len; i++) {
			sum2=sum2+nums[i];
		}
		return sum1+sum2;

}

Open in new window

failing few tests
Expected	Run		
sum67([1, 2, 2]) → 5	5	OK	
sum67([1, 2, 2, 6, 99, 99, 7]) → 5	12	X	
sum67([1, 1, 6, 7, 2]) → 4	11	X	
sum67([1, 6, 2, 2, 7, 1, 6, 99, 99, 7]) → 2	26	X	
sum67([1, 6, 2, 6, 2, 7, 1, 6, 99, 99, 7]) → 2	32	X	
sum67([2, 7, 6, 2, 6, 7, 2, 7]) → 18	24	X	
sum67([2, 7, 6, 2, 6, 2, 7]) → 9	24	X	
sum67([1, 6, 7, 7]) → 8	8	OK	
sum67([6, 7, 1, 6, 7, 7]) → 8	21	X	
sum67([6, 8, 1, 6, 7]) → 0	22	X	
sum67([]) → 0	0	OK	
sum67([6, 7, 11]) → 11	18	X	
sum67([11, 6, 7, 11]) → 22	29	X	
sum67([2, 2, 6, 7, 7]) → 11	11	OK	
other tests
X	

Open in new window


i have to remind about psedo code description

1. Loop through each element of given array
2. Find index position of six
3. find index position of seven.
4. find sum1 which is sum of all integers till six index position
5. find sum2 which is sum of all intergers starting from seven index till end
6. return sum of both sextions sum1 plus sum2
ASKER CERTIFIED 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 class Sum67 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//int[] ar = { 1, 2, 2, 6, 99, 99, 7, 3, 9 };
		int[] ar = {1, 1, 6, 2, 3, 4, 5, 8, 9, 7, 1};//sixPos 2 sevenPos 9
		System.out.println("value is" + sum67(ar));

	}

	public static int sum67(int[] nums) {

		int sum = 0;
		int len = nums.length;
		int sixPos=0;
		int sevenPos=0;
		int sum1=0;
		int sum2=0;
	
		for (int i = 0; i < len; i++) {
			if (nums[i] == 6){
				 sixPos=i;
				 System.out.println("sixPos is-->"+sixPos);
			}
			
			if (nums[i] ==7){
				 sevenPos=i;
				 System.out.println("sevenPos is-->"+sevenPos);
			}		
		}
		for (int i = 0; i < sixPos; i++) {
			sum1=sum1+nums[i];
		}
		for (int i = sevenPos+1; i < len; i++) {
			sum2=sum2+nums[i];
		}
		System.out.println("sum1 is-->"+sum1);
		System.out.println("sum2 is-->"+sum2);
		return sum1+sum2;

}

}

Open in new window


i corrected one error

got more accurate output

sixPos is-->2
sevenPos is-->9
sum1 is-->2
sum2 is-->1
value is3

xpected      Run            
sum67([1, 2, 2]) → 5      4      X      
sum67([1, 2, 2, 6, 99, 99, 7]) → 5      5      OK      
sum67([1, 1, 6, 7, 2]) → 4      4      OK      
sum67([1, 6, 2, 2, 7, 1, 6, 99, 99, 7]) → 2      19      X      
sum67([1, 6, 2, 6, 2, 7, 1, 6, 99, 99, 7]) → 2      25      X      
sum67([2, 7, 6, 2, 6, 7, 2, 7]) → 18      17      X      
sum67([2, 7, 6, 2, 6, 2, 7]) → 9      17      X      
sum67([1, 6, 7, 7]) → 8      1      X      
sum67([6, 7, 1, 6, 7, 7]) → 8      14      X      
sum67([6, 8, 1, 6, 7]) → 0      15      X      
sum67([]) → 0      0      OK      
sum67([6, 7, 11]) → 11      11      OK      
sum67([11, 6, 7, 11]) → 22      22      OK      
sum67([2, 2, 6, 7, 7]) → 11      4      X      
other tests
X      

You can probably get away with a single pass over the array if you have a boolean that indicates whether you're between a 6 and a 7. The pseudocode of the loop could look like this:

If the current element is a 6, set the boolean to true.
If the boolean is false, add the current element to the sum.
If the current element is a 7, set the boolean to false.
let me check on above
Avatar of gudii9

ASKER

I passed few other tests
public class Sum67 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// int[] ar = { 1, 2, 2, 6, 99, 99, 7, 3, 9 };
		// int[] ar = {1, 1, 6, 2, 3, 4, 5, 8, 9, 7, 1};//sixPos 2 sevenPos 9
		int[] ar = { 1, 2, 2 };// sixPos 0 sevenPos 0
		System.out.println("value is" + sum67(ar));

	}

	public static int sum67(int[] nums) {

		int sum = 0;
		int len = nums.length;
		int sixPos = 0;
		int sevenPos = 0;
		int sum1 = 0;
		int sum2 = 0;
		int sumExclude = 0;

		for (int i = 0; i < len; i++) {
			if (nums[i] == 6) {
				sixPos = i;
				System.out.println("sixPos is-->" + sixPos);
			}

			if (nums[i] == 7) {
				sevenPos = i;
				System.out.println("sevenPos is-->" + sevenPos);
			}
		}
		for (int i = 0; i < sixPos; i++) {
			sum1 = sum1 + nums[i];
		}

		for (int i = sevenPos + 1; i < len; i++) {
			sum2 = sum2 + nums[i];
		}
		System.out.println("sum1 is-->" + sum1);
		System.out.println("sum2 is-->" + sum2);
		if (sixPos == 0 && sevenPos == 0) {
			for (int i = 0; i < len; i++) {
				sumExclude = sumExclude + nums[i];
			}
			return sumExclude;
		} else if (sixPos > 0 && sevenPos > 0) {
			return sum1 + sum2;
		} else {
			return 0;
		}
	}

}

Open in new window

@gudii9,
It seems like you are making some progress. But, did you do the debugger step-by-step approach with the manual method of your looking at one card at a time, and when the sum differs, then you try to figure out what your program is doing differently than what you are doing by hand.

Given the difficulties you are having with some of these challenges, may I suggest that you first put a question strictly in the algorithms zone and post your pseudo-code (language independent) and explain where it goes wrong when you compare with your manual approach.

Once you get the design right (i.e., the pseudo-code), then it will be easier to code up the challenge.
@awking00,
>> In programming there are really only two success rates 0 and 100.
Heh, you are certainly right for this type of challenge.
But in a free online U.C.S.D. course, you get three chances to fail when you submit your program:
1) incorrect answers
2) program exceeds the time-limit
3) program takes up too much memory
- It's a pretty tough checker with lots of edge corner-cases.

Come to think of it, if the author is getting less errors and figuring out on his own how to correct them, then the challenges are proving very valuable. On the other hand, if the author just shows his progress and gets tips from the experts, then he may understand, but may not know. Knowing is a requirement for most software development positions.
Avatar of gudii9

ASKER

But in a free online U.C.S.D. course

i wonder where i can register and learn, practice them? please advise
Avatar of gudii9

ASKER

i passed most of the tests with one section of 6 xxxxx7 failing in two sections of 6 xxxxx7

like

1 7 5 6 xxxxx7 3 9 6 xxxxx7 4 1//in this case my logic adding all non bold digits which is wrong
Yeah, the strategy you're using will only work when there's a single 6 and 7 in the input. Have you tried the suggestion I made in this comment? It should work on multiple ranges as well as on single ranges.
Avatar of gudii9

ASKER

public class Sum67 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// int[] ar = { 1, 2, 2, 6, 99, 99, 7, 3, 9 };
		// int[] ar = {1, 1, 6, 2, 3, 4, 5, 8, 9, 7, 1};//sixPos 2 sevenPos 9
		int[] ar = { 1, 2, 2 };// sixPos 0 sevenPos 0
		System.out.println("value is" + sum67(ar));

	}

	public static int sum67(int[] nums) {
		/*
		 * If the current element is a 6, set the boolean to true.
		If the boolean is false, add the current element to the sum.
		If the current element is a 7, set the boolean to false.*/
		int sum=0;
		boolean ind=false;
		for(int i=0;i<nums.length;i++){
			if(nums[i]==6){
				ind=true;
			}
			if(ind==false){
				sum+=nums[i];
			}
			if(nums[i]==7){
				ind=true;
			}
			
			
		}
		return sum;
	}

}

Open in new window


Expected      Run            
sum67([1, 2, 2]) → 5      5      OK      
sum67([1, 2, 2, 6, 99, 99, 7]) → 5      5      OK      
sum67([1, 1, 6, 7, 2]) → 4      2      X      
sum67([1, 6, 2, 2, 7, 1, 6, 99, 99, 7]) → 2      1      X      
sum67([1, 6, 2, 6, 2, 7, 1, 6, 99, 99, 7]) → 2      1      X      
sum67([2, 7, 6, 2, 6, 7, 2, 7]) → 18      9      X      
sum67([2, 7, 6, 2, 6, 2, 7]) → 9      9      OK      
sum67([1, 6, 7, 7]) → 8      1      X      
sum67([6, 7, 1, 6, 7, 7]) → 8      0      X      
sum67([6, 8, 1, 6, 7]) → 0      0      OK      
sum67([]) → 0      0      OK      
sum67([6, 7, 11]) → 11      0      X      
sum67([11, 6, 7, 11]) → 22      11      X      
sum67([2, 2, 6, 7, 7]) → 11      4      X      
other tests
X      

you mean like above. I am passing few more but failing few. please advise
Double-check the places where you're updating your boolean.
Avatar of gudii9

ASKER

If the current element is a 6, set the boolean to true.
            If the boolean is false, add the current element to the sum.
            If the current element is a 7, set the boolean to false

public int sum67(int[] nums) {int sum=0;
		boolean ind=false;
		for(int i=0;i<nums.length;i++){
			if(nums[i]==6){
				ind=true;
			}
			if(ind==false){
				sum+=nums[i];
			}
			if(nums[i]==7){
				ind=false;
			}
			
			
		}
		return sum;
  
} 

Open in new window

Avatar of gudii9

ASKER

above failing few
Are you sure?
Avatar of gudii9

ASKER

public int sum67(int[] nums) {int sum=0;
		boolean ind=false;
		for(int i=0;i<nums.length;i++){
			if(nums[i]==6){
				ind=true;
			}
			if(ind==false){
				sum+=nums[i];
			}
			if(nums[i]==7&&ind==true){
				ind=false;
			}
			
			
		}
		return sum;
  
} 

Open in new window


above pass all