fount 9's in java challenge

Hi,

I am working on below challenge.

I wrote as below
public class Test26 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] nums={1,9,1,9,4};
		System.out.println("return value boolean is--->"+arrayFront9(nums));

	}
	public static boolean arrayFront9(int[] nums) {
		int count=0;
		  for(int num: nums){
		  if(num==9)
			  count++;
		  }
		  
		  if(count>=4)
		  return true;
		
	 /*if(count<4)
		return false;*/
	return false;
	}


}

Open in new window


I am failing in one test case as below


Expected      Run            
arrayFront9({1, 2, 9, 3, 4}) → true      true      OK         
arrayFront9({1, 2, 3, 4, 9}) → false      true      X         
arrayFront9({1, 2, 3, 4, 5}) → false      false      OK         
arrayFront9({9, 2, 3}) → true      true      OK         
arrayFront9({1, 9, 9}) → true      true      OK         
arrayFront9({1, 2, 3}) → false      false      OK         
arrayFront9({1, 9}) → true      true      OK         
arrayFront9({5, 5}) → false      false      OK         
arrayFront9({2}) → false      false      OK         
arrayFront9({9}) → true      true      OK         
arrayFront9({}) → false      false      OK         
arrayFront9({3, 9, 2, 3, 3}) → true      true      OK         
Correct for more than half the tests

How do i fix and improve my program. please advise
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
Your code is counting the number of 9s.

The problem is to see if there is a 9 in the first 4 values.

Can you see how I changed your method and why this works?

	public boolean arrayFront9(int[] nums) {
		int count=0;
		  for(int num: nums){
                    count++ ;
                    
		  if(num==9 && count <= 4)
                     return true ;
		  }
		  
	return false;
	}

Open in new window

gudii9Author Commented:
count++ ;

this counts number of elements in nums array right?

if(num==9 && count <= 4
above statement it will make sure it will check till 4th position only not beyond that and also value is 9 right?
dpearsonCommented:
Yep that's all correct.
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

ozoCommented:
Can you post a link to this challenge?
dpearsonCommented:
It's this one:
http://codingbat.com/prob/p186031

Doug
ozoCommented:
The method of http:#a40425004 does work for that, but it may be worth noting that if you have a very long array, it could do a lot more work than it needs to before returning false.

I also notice that the code in the original question fails in more than just the one case.
gudii9Author Commented:
The method of http:#a40425004 does work for that, but it may be worth noting that if you have a very long array, it could do a lot more work than it needs to before returning false.

Is there is a way to improve it? please advise
The method of http:#a40425004 does work for that, but it may be worth noting that if you have a very long array, it could do a lot more work than it needs to before returning false.

correct it fails for all arrays having 9's having bigger count than  4
dpearsonCommented:
Is there is a way to improve it? please advise

The version above loops through all members of the array.

But since we only have to check the first 4 items we could do this instead:

	public boolean arrayFront9(int[] nums) {
                 for (int count = 0 ; count < nums.length && count < 4 ; count++) {
                    int num = nums[count];
		    if(num==9)
                       return true ;
		  }
		  
	         return false;
	}

Open in new window


This is an example of switching between the two different types of loops (for int i = 0 ; i ... ) and (for num : nums), so we can check the count of items so far.

Can you see why it makes sense here to use the older style (for int i = 0 ; i < ... ; i++) style of loop here?

Doug

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
Is there is a way to improve it?
You might change
              if(num==9 && count <= 4)
                     return true ;
to
              if(num==9 )
                     return true ;
                  if( count==4 )
                     return false;
gudii9Author Commented:
count < nums.length && count < 4

I see 2 separate conditions clubbed together with &&.

How many number of conditions we can club together?

I was thinking we should say instead as below
count < nums.length && count < =4
Please advise
gudii9Author Commented:
You might change
              if(num==9 && count <= 4)
                     return true ;
to
              if(num==9 )
                     return true ;
                  if( count==4 )
                     return false;

Open in new window


we are still checking all the elements in for each loop and do above step right. How it is going to be different. please advise
dpearsonCommented:
count < nums.length && count < =4

If you start the loop with "count = 0" you want "count < 4" to do 4 loops.
If you start the loop with "count = 1" you want "count <= 4" to do 4 loops.

We usually loop with counts that start from 0 in the software business, because arrays start from index 0, so you can just do:
int num = nums[count] ;
if the count starts with 0.

You might change
              if(num==9 && count <= 4)
                     return true ;
to
              if(num==9 )
                     return true ;
                  if( count==4 )
                     return false;

Yes that's another way to do it.

Doug
gudii9Author Commented:
You might change
              if(num==9 && count <= 4)
                     return true ;
to
              if(num==9 )
                     return true ;
                  if( count==4 )
                     return false;

Open in new window

do you mean it should be like below(within foreach loop?  Please advise


public boolean arrayFront9(int[] nums) {
            int count=0;
              for(int num: nums){
                    count++ ;
                   
       if(num==9 )
                     return true ;
                  if( count==4 )
                     return false;
              }
             
      return false;
      }
dpearsonCommented:
Yes that's what ozo is suggesting.

Doug
gudii9Author Commented:
what would be the difference. To  me both approaches looks same.please advise.
ozoCommented:
The difference between http:#a40426848 and http:#a40427058 would be mainly which style of for loop you prefer.
In this case, since you do need the count index, and don't always scan all elements of the array, I might be more inclined to use http:#a40426848, but other preferences can be equally valid.
gudii9Author Commented:
In this case, since you do need the count index, and don't always scan all elements of the array
which case you are referring http:#a40426848  or http:#a40427058


in http:#a40427058 scanning of all elements happening in the below lines right?

for(int num: nums){
                    count++ ;
as given in the complete code below

public boolean arrayFront9(int[] nums) {
            int count=0;
              for(int num: nums){
                    count++ ;
                    
       if(num==9 )
                     return true ;
                  if( count==4 )
                     return false;
              }
              
      return false;
      }

Open in new window

ozoCommented:
The case I am referring to is the problem presented by http://codingbat.com/prob/p186031

In  http:#a40427058, scanning of all elements is limited by
                  if( count==4 )
                     return false;
dpearsonCommented:
what would be the difference. To  me both approaches looks same.please advise.

They're both logically equivalent since they both solve the problem.

So in that case you generally want the simpler solution, which is usually the one using less lines of code (although not always).  So in this situation I'd prefer:

	public boolean arrayFront9(int[] nums) {
                 for (int count = 0 ; count < nums.length && count < 4 ; count++) {
                    int num = nums[count];
		    if(num==9)
                       return true ;
		  }
		  
	         return false;
	}

Open in new window


It also reads a little easier - because this line:

                 for (int count = 0 ; count < nums.length && count < 4 ; count++) {

explicitly indicates that we're looping through at most 4 elements of the array, which is otherwise something you only discover later while reading the method (in the alternative solutions, this test for count == 4 comes later).

But at this point it's mostly about a sense of whichever you find clearer.  There are no hard and fast rules and really to be a good developer the key is seeing that there are different ways to solve any given problem - so you can weigh the pros and cons of each option.

Doug
gudii9Author Commented:
	public boolean arrayFront9(int[] nums) {
                 for (int count = 0 ; count < nums.length && count < 4 ; count++) {
                    int num = nums[count];
		    if(num==9)
                       return true ;
		  }
		  
	         return false;
	}

Open in new window


I too feel above code is more readable and simple.Rather than checking inside for loop by putting separate if condition to see if count==4 as below

 if(num==9 )
                     return true ;
                  if( count==4 )
                     return false;
gudii9Author Commented:
Can you see why it makes sense here to use the older style (for int i = 0 ; i < ... ; i++) style of loop here?

i finally got using old for loop rather than new for each loop is advisable in this case to avoid scanning elements beyond count 4 right. please advise
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.