Solved

scoresClump  challenge

Posted on 2016-08-26
31
123 Views
Last Modified: 2016-09-01
Hi,

I am going through below challenge.
http://codingbat.com/prob/p194530
Expected      Run            
scoresClump([3, 4, 5]) → true      false      X      
scoresClump([3, 4, 6]) → false      false      OK      
scoresClump([1, 3, 5, 5]) → true      false      X      
scoresClump([2, 4, 5, 6]) → true      false      X      
scoresClump([2, 4, 5, 7]) → false      false      OK      
scoresClump([2, 4, 4, 7]) → true      false      X      
scoresClump([3, 3, 6, 7, 9]) → false      false      OK      
scoresClump([3, 3, 7, 7, 9]) → true      false      X      
scoresClump([4, 5, 8]) → false      false      OK      
other tests
X      
Your progress graph for this problem

i wonder how below is false?
scoresClump([3, 4, 6]) → false
i expected above true as difference is atmost 2?  please advise
0
Comment
Question by:gudii9
  • 11
  • 8
  • 6
  • +4
31 Comments
 
LVL 37

Assisted Solution

by:Gerwin Jansen
Gerwin Jansen earned 125 total points
ID: 41773183
[3, 4, 6] only has 1 set of 3 numbers next to each other, the difference from 3 to 6 is 3 which is higher than the challenge is asking for (2).

The other 2 examples:
[3, 4, 5] - 1 set, difference from 3 to 5 is 2
[1, 3, 5, 5] - 2 sets, 1-3-5 is valid, 3-5-5 is also valid
0
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
ID: 41773236
with computers there is no almost .. it is either true or false.. no maybe true.
1.9999999999 does not equal 2
0
 
LVL 7

Author Comment

by:gudii9
ID: 41773413
public boolean scoresClump(int[] scores) {
  boolean result=false;
for(int i=0;i<scores.length-2;i++){
  if( (scores[i+2]-scores[i])==2||(scores[i+1]-scores[i])==2){
    result=true;
    
  }
  else{
  result=false;
}

}

return result;
}

Open in new window


i am failing one test. please advise on improvements, fix
Expected      Run            
scoresClump([3, 4, 5]) → true      true      OK      
scoresClump([3, 4, 6]) → false      false      OK      
scoresClump([1, 3, 5, 5]) → true      true      OK      
scoresClump([2, 4, 5, 6]) → true      true      OK      
scoresClump([2, 4, 5, 7]) → false      false      OK      
scoresClump([2, 4, 4, 7]) → true      false      X      
scoresClump([3, 3, 6, 7, 9]) → false      false      OK      
scoresClump([3, 3, 7, 7, 9]) → true      true      OK      
scoresClump([4, 5, 8]) → false      false      OK      
other tests
X      
0
 
LVL 35

Accepted Solution

by:
mccarl earned 250 total points
ID: 41773434
Like in the other questions, once you have found a clump, you can return straight away, you don't need to keep looking. That's why that one test case is failing, you are correctly finding the clump and setting result to true, but then in a later iteration you set it back to false. Like my comments in other questions, you can totally get rid of the result variable, and just return inside the if statement.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41773893
public boolean scoresClump(int[] scores) {
for(int i=0;i<scores.length-2;i++){
  if( (scores[i+2]-scores[i])==2||(scores[i+1]-scores[i])==2){
    return true;
  }
}
return false;
}

Open in new window


i think i made changes but failing one other test
xpected      Run            
scoresClump([3, 4, 5]) → true      true      OK      
scoresClump([3, 4, 6]) → false      false      OK      
scoresClump([1, 3, 5, 5]) → true      true      OK      
scoresClump([2, 4, 5, 6]) → true      true      OK      
scoresClump([2, 4, 5, 7]) → false      true      X      
scoresClump([2, 4, 4, 7]) → true      true      OK      
scoresClump([3, 3, 6, 7, 9]) → false      false      OK      
scoresClump([3, 3, 7, 7, 9]) → true      true      OK      
scoresClump([4, 5, 8]) → false      false      OK      
other tests
X      

please advise
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41774014
boolean scoresClump(int[] scores)
 scores[i]+2   int i = 0;   return true;   while(i<){    if(<=){}    scores[i+2]
  }}   i++; int i = 0;return false;scores.length-2{public 

Open in new window


That's the code - but in entirely the wrong order.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 41774083
In the original problem it states, "...differ by at most 2". Another way to read this is if the difference is less than or equal to 2. So an array such as this... [1,2,2] has a difference of only 1 but it should be counted as a clump, ie. you would return true for that case.

Also, the problem says that the numbers are already sorted in increasing order, so if you think carefully about that you will realise that you only need to check the first and the last value of a group of 3. Because they are in order, the middle number of the group of 3, will ALWAYS have a difference that is equal or less than the difference between the first and last.

Translate the above 2 points into code and you should get your solution.
0
 
LVL 32

Expert Comment

by:awking00
ID: 41774720
Since you are looking at groups of 3, think about starting at the second element of the array and ending at the next to last element, comparing the difference of the elements before and after.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41775282
Could you explain your comment  for my benefit, as I don't get it.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41775322
you will realise that you only need to check the first and the last value of a group of 3.
check what difference to be atmost 2 or 3 or 4?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41775332
scoresClump([2, 4, 5, 7]) → false      true      X



As we are checking set of 3 values each time for next iteration do i need to do i=i+3??
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41775409
You check the difference for being more than 2 because that IS WHAT THE CHALLENGE ASKS FOR
0
 
LVL 35

Expert Comment

by:mccarl
ID: 41775460
As we are checking set of 3 values each time for next iteration do i need to do i=i+3??

Open in new window

No, you are looking at groups of 3 but you don't know where they start and end. So for the example that you copied above, you have to examine both the 2,4,5 group and the 4,5,7 group. So no, the loop that you had originally, with the i++ is correct.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 41775466
The only change required is to the if statement on line 3. Remembering the two points I made above, ie, you have to look at only the first and last numbers in the group of 3 (not the middle) and that you need to check for a difference of less than or equal to 2.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 7

Author Comment

by:gudii9
ID: 41775479
yes i think i got what you are saying
public boolean scoresClump(int[] scores) {

		for (int i = 0; i < scores.length - 2; i = i + 1) {
			if ((scores[i + 2] - scores[i]) <= 2) {
			//	if ((scores[i + 2] - scores[i+1]) <= 2) {
				//	System.out.println("i is is--->" + i);
					return true;
			//	}

			}

		}
		return false;

	}

Open in new window


above passed all tests
package com.solution;

public class ScoresCrump {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] ar = { 2, 4, 5, 7 };
		System.out.println("value is===>" + scoresClump(ar));
	}

	public static boolean scoresClump(int[] scores) {

		for (int i = 0; i < scores.length - 2; i = i + 1) {
			if ((scores[i + 2] - scores[i]) == 2) {
				//if ((scores[i + 2] - scores[i]) == 2) {
					System.out.println("i is is--->" + i);
					return true;
				//}

			}

		}
		return false;

	}

}

Open in new window


value is===>false



any improvements/modifications/alternate approaches to my code?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41775493
i wonder why my java program not printing below line?

System.out.println("i is is--->" + i);
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41775502
i wonder why my java program not printing below line?

System.out.println("i is is--->" + i);

If you are talking about Codingbat - you cant print to the console from there.
0
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 125 total points
ID: 41775505
any improvements/modifications/alternate approaches to my code?

You could make it a bit shorter . . .

public boolean scoresClump(int[] scores) {
  int i = 0;
  while(i<scores.length-2){
    if(scores[i+2]<=scores[i]+2){return true;}
    i++;
  }
  return false;
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41775518
sure. any specific preference of while than 'for' for this. I think both does same thing right?

i wonder why my java program not printing below line?
System.out.println("i is is--->" + i);

If you are talking about Codingbat - you cant print to the console from there.

i am not able to print in eclipse only not coding bat?
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41775539
No idea about Eclipse - I don't use it.

As to while versus for - think about it and tell us what you conclude for this case.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 41775543
@krakatoa,

I'm not sure how that is shorter, you've broken the for loop out into a while loop, adding extra lines and then just removed some whitespace!?

@gudii9,

While vs For would be a matter of preference, but in this case I would prefer the for loop because that is exactly what it is made to do. The while loop is more general and can do more things, but in this case you want to loop a fixed number of times, and that is what the for loop is for.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41775671
sure.

i wonder why my java program not printing below line? System.out.println("i is is--->" + i);

i figured out reason as it never goes to if loop for below example so never prints that
package com.solution;

public class ScoresCrump {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] ar = { 2, 4, 5, 7 };
		System.out.println("value is===>" + scoresClump(ar));
	}

	public static boolean scoresClump(int[] scores) {

		for (int i = 0; i < scores.length - 2; i = i + 1) {
			if ((scores[i + 2] - scores[i]) == 2) {
				//if ((scores[i + 2] - scores[i]) == 2) {
					System.out.println("i is is--->" + i);
					return true;
				//}

			}

		}
		return false;

	}

}

Open in new window

0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41775941
@mccarl

This comment's code https://www.experts-exchange.com/questions/28966010/scoresClump-challenge.html#a41775479

unnecessarily tests 3 elements. My code doesn't. Unless I'm mistaken, that seems shorter to me, and if it isn't, it's better.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 41776152
There are lines commented out in that code, so if those were removed it would essentially be the same.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41776160
My point is his original meta was to test 3 values which is unnecessary. Whether the lines are commented out or not doesn't change the Asker's degree of confusion with the nature of the challenge and on top of that it's not good practice to leave scraps of code and unhelpful formatting lying about all over the place. This is one of the Asker's weaknesses and so my comment about the code being shorter stands. Remember, this isn't about you as a programmer- it's about him!
0
 
LVL 84

Expert Comment

by:ozo
ID: 41779298
public boolean scoresClump(int[] scores) {
  for( int i=scores.length; --i>1; ){
    if( scores[ i ]-scores[i-2]<=2 ){ return true; }
  }
  return false;
}
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41779801
Off topic - what telling someone they should use code tags?? Are you SERIOUS??
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41779918
So seriously ozo - were you too lazy to put your code into code tags?
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41779993
sure. any specific preference of while than 'for' for this. I think both does same thing right?

Do you think the people who invented Java would waste their time - and everyone else's - writing routines that did the same thing as routines they already had written??

You must have asked 100s of questions about for and while loops but you still haven't taken the comments and answers on board.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

910 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

16 Experts available now in Live!

Get 1:1 Help Now