Solved

sum67 challenge

Posted on 2016-08-01
35
88 Views
Last Modified: 2016-08-03
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
0
Comment
Question by:gudii9
  • 19
  • 7
  • 6
  • +1
35 Comments
 
LVL 14

Expert Comment

by:CPColin
Comment Utility
Why are you checking for n != 12?
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility

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??)
0
 
LVL 14

Expert Comment

by:CPColin
Comment Utility
They're saying every six is eventually followed by a seven, not immediately.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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?
0
 
LVL 14

Expert Comment

by:CPColin
Comment Utility
It wants you to ignore everything between the 6 and the 7, too.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
oh
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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      
0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
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.
1
 
LVL 7

Author Comment

by:gudii9
Comment Utility
I am using eclipse built in debugger
0
 
LVL 31

Assisted Solution

by:awking00
awking00 earned 125 total points
Comment Utility
>> i improved success rate >>
In programming there are really only two success rates 0 and 100. I feel you need to improve more on your ability to think logically than on the various classes and methods with their syntax that Java can provide to generate solutions. In this exercise, there are basically two things to look for - any section of numbers (which I interpret to be a series of numbers) and the sum of all numbers except the ones in those "sections". To determine if a "section" exists, you might start by looking for the first index of 6 then from there finding the first index of 7, eliminating those two numbers and everything in between from the sum. Since there can be more than one such "section" you might have to repeat the search a number of times (now starting from the last found index of 7).
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 125 total points
Comment Utility
You showed the question along with some sample cases and their answers. Yet you wrote a program without even knowing why their answers are correct. Why in the world would you do that? If you do not know what the program is supposed to do and yet you write a program, then of course you get the wrong answer.
Do you know the answer to this case?
1 1 6 2 3 4 5 8 9 7 1

If so, starting from left to right, by hand, look at one digit at a time, and write down the current sum. While doing that, step through the program one digit at a time. The sumby hand and in your debugger should match as you look at each digit. If it doesn't match, then spend a good deal amount of time thinking about what you did by hand and then keep modifying your code until you get it right. If you still have problems, then post several attempts at getting the solution and tell us on which digit did the program fail to match the sum when you do it by hand.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
Do you know the answer to this case?
1 1 6 2 3 4 5 8 9 7 1
1+1+7+1=10
0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
Nope
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
oops
1+1+1=3 as i have to exclude all in between 6 and 7 including them as well
0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
yes
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 7

Author Comment

by:gudii9
Comment Utility
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?
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
i got correct output as
value is24

while debugging on eclipse
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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
0
 
LVL 14

Accepted Solution

by:
CPColin earned 250 total points
Comment Utility
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:

  1. If the current element is a 6, set the boolean to true.
  2. If the boolean is false, add the current element to the sum.
  3. If the current element is a 7, set the boolean to false.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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

0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
@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.
1
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
@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.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
But in a free online U.C.S.D. course

i wonder where i can register and learn, practice them? please advise
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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
0
 
LVL 14

Expert Comment

by:CPColin
Comment Utility
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.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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
0
 
LVL 14

Expert Comment

by:CPColin
Comment Utility
Double-check the places where you're updating your boolean.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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

0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
above failing few
0
 
LVL 14

Expert Comment

by:CPColin
Comment Utility
Are you sure?
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mapAB Challlenge 35 84
Unable to start eclipse ? 17 82
unix in java example 9 37
micro services spring boot application error 3 23
This is an explanation of a simple data model to help parse a JSON feed
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now