?
Solved

scoresIncreasing challenge

Posted on 2016-08-26
10
Medium Priority
?
75 Views
Last Modified: 2016-08-29
Hi,

I am working on below challege.
http://codingbat.com/prob/p146974
my psedo code :
1. loop thorugh given array.
2. compare first array element with second and alsos third with second to make sure later is bigger than earlier one.
3. if yes return true.
4. if no return false

public boolean scoresIncreasing(int[] scores) {
  boolean result=false;
  int len=scores.length;
for(int i=0;i<len;i++){
  if(scores[i+1]>scores[i]&&scores[i+2]>scores[i+1]){
    result=true;
    
    return result;
  }
}
return result;
  
}

Open in new window


Expected      Run            
scoresIncreasing([1, 3, 4]) → true      true      OK      
scoresIncreasing([1, 3, 2]) → false      Exception:java.lang.ArrayIndexOutOfBoundsException: 3 (line number:5)      X      
scoresIncreasing([1, 1, 4]) → true      Exception:java.lang.ArrayIndexOutOfBoundsException: 3 (line number:5)      X      
scoresIncreasing([1, 1, 2, 4, 4, 7]) → true      true      OK      
scoresIncreasing([1, 1, 2, 4, 3, 7]) → false      true      X      
scoresIncreasing([-5, 4, 11]) → true      true      OK      
Your progress graph for this problem

i am failing few tests. How to fix and improve my code. please advise
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 7

Author Comment

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

Open in new window


above passed all tests. Any improvements/refinement/refactoring my code?
0
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 1000 total points
ID: 41772972
Your if statement is too complex, you only need one comparison.
1
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 1000 total points
ID: 41773012
Without requiring a loop -
int[] sorted = scores.clone();
Arrays.sort(sorted);
return Arrays.equals(scores, sorted);
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:gudii9
ID: 41773372
public boolean scoresIncreasing(int[] scores) {
  boolean result=true;
  int len=scores.length;
for(int i=0;i<len-1;i++){
  if(scores[i+1]<scores[i]){
    result=false;
    
    return result;
  }
}
return result;
  
}

Open in new window


passing all tests
0
 
LVL 7

Author Comment

by:gudii9
ID: 41773386
public boolean scoresIncreasing(int[] scores) {
int[] sorted = scores.clone();
Arrays.sort(sorted);
return Arrays.equals(scores, sorted);
  
}

Open in new window


i like this solution too
0
 
LVL 7

Author Comment

by:gudii9
ID: 41773387
i hope no other changes?
0
 
LVL 36

Expert Comment

by:mccarl
ID: 41773435
No change to your logic, but I think the local variables actually make it harder to understand, compare your solution to this... same logic but just reads differently...
public boolean scoresIncreasing(int[] scores) {
    for (int i = 0; i < scores.length - 1; i++) {
        if (scores[i+1] < scores[i]) {
            return false;
        }
    }
    return true;
}

Open in new window

0
 
LVL 7

Author Comment

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

Open in new window


above is much better
0
 
LVL 36

Expert Comment

by:mccarl
ID: 41774098
A few tips on tidying up code like that...

- Look for variables that are only used once. Now, if the variable name doesn't add any extra information, you might consider removing it like we did above. For example, scores.length is pretty obvious in what it means, and "len" doesn't add anything. This isn't always the case, so don't just blindly remove ALL of them, but just think it through.

- Secondly, look for cases where you are setting a variable to just immediately return it, ie. the "result" variable in the if statement above. And then once you tidy that up, apply the first tip above and you can get rid of it completely.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41775325
sure
0

Featured Post

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Make the most of your online learning experience.
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

718 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