gudii9
asked on
scoresIncreasing challenge
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
i am failing few tests. How to fix and improve my code. please advise
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;
}
Expected Run
scoresIncreasing([1, 3, 4]) → true true OK
scoresIncreasing([1, 3, 2]) → false Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 3 (line number:5) X
scoresIncreasing([1, 1, 4]) → true Exception:java.lang.ArrayIndexOutOfB oundsExcep tion: 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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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;
}
passing all tests
ASKER
public boolean scoresIncreasing(int[] scores) {
int[] sorted = scores.clone();
Arrays.sort(sorted);
return Arrays.equals(scores, sorted);
}
i like this solution too
ASKER
i hope no other changes?
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;
}
ASKER
public boolean scoresIncreasing(int[] scores) {
for (int i = 0; i < scores.length - 1; i++) {
if (scores[i+1] < scores[i]) {
return false;
}
}
return true;
}
above is much better
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.
- 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.
ASKER
sure
ASKER
Open in new window
above passed all tests. Any improvements/refinement/re