Solved

scores100 challenge

Posted on 2016-08-26
3
83 Views
Last Modified: 2016-08-28
Hi,

I am working on below challenge

http://codingbat.com/prob/p179487

Pseudo Code:
1. loop through given array
2. find elemt a i position and i+1 position.
3. check their equality.
4. if equal return true
5. if not equal reurn false

I wrote my code as below
public boolean scores100(int[] scores) {
  
  boolean result=true;
  int len=scores.length;
for(int i=0;i<len-1;i++){
  if(scores[i]!=100&&scores[i+1]!=100){
   // i=i+1;
    result=false;
    
    return result;
  }
}
return result;
  

}

Open in new window



Expected      Run            
scores100([1, 100, 100]) → true      true      OK      
scores100([1, 100, 99, 100]) → false      true      X      
scores100([100, 1, 100, 100]) → true      true      OK      
scores100([100, 1, 100, 1]) → false      true      X      
scores100([1, 2, 3, 4, 5]) → false      false      OK      
scores100([1, 2, 100, 4, 5]) → false      false      OK      
other tests
OK      

I am not passing all tests

How to improve my psedo code, approach, code? please advise
0
Comment
Question by:gudii9
3 Comments
 
LVL 37

Accepted Solution

by:
Gerwin Jansen earned 250 total points
ID: 41773188
Your pseudo code looks OK to me, your code doesn't match. Assume that there are no 2 scores of 100 next to each other, set your flag to true once you've found one.
0
 
LVL 7

Author Comment

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

}
//Your pseudo code looks OK to me, your code doesn't match. Assume that there 
//are no 2 scores of 100 next to each other, set your flag to true once you've found one.

Open in new window


above passes all tests.
any improvements?
0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 250 total points
ID: 41773433
Your logic is good, the only thing that I would do differently, is that I think it reads even more simple if you remove the local variables, ie...
public boolean scores100(int[] scores) {
    for (int i = 0;i < scores.length - 1; i++) {
        if (scores[i] == 100 && scores[i+1] == 100) {
            return true;
        }
    }
    return false;
}

Open in new window

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The viewer will learn how to implement Singleton Design Pattern in Java.
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 …

758 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

21 Experts available now in Live!

Get 1:1 Help Now