Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

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
Avatar of dpearson
dpearson

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

Avatar of gudii9

ASKER

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?
Yep that's all correct.
Can you post a link to this challenge?
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.
Avatar of gudii9

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

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
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
Avatar of gudii9

ASKER

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;
      }
Yes that's what ozo is suggesting.

Doug
Avatar of gudii9

ASKER

what would be the difference. To  me both approaches looks same.please advise.
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.
Avatar of gudii9

ASKER

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

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;
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
Avatar of gudii9

ASKER

	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;
Avatar of gudii9

ASKER

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