Solved

fount 9's in java challenge

Posted on 2014-11-05
21
241 Views
Last Modified: 2014-11-11
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
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
  • 9
  • 7
  • 5
21 Comments
 
LVL 27

Expert Comment

by:dpearson
ID: 40425004
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

0
 
LVL 7

Author Comment

by:gudii9
ID: 40425023
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?
0
 
LVL 27

Expert Comment

by:dpearson
ID: 40425040
Yep that's all correct.
0
Independent Software Vendors: 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 84

Expert Comment

by:ozo
ID: 40425570
Can you post a link to this challenge?
0
 
LVL 27

Expert Comment

by:dpearson
ID: 40425574
It's this one:
http://codingbat.com/prob/p186031

Doug
0
 
LVL 84

Expert Comment

by:ozo
ID: 40425652
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.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40426193
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
0
 
LVL 27

Accepted Solution

by:
dpearson earned 250 total points
ID: 40426848
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
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 250 total points
ID: 40426867
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;
0
 
LVL 7

Author Comment

by:gudii9
ID: 40426916
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
0
 
LVL 7

Author Comment

by:gudii9
ID: 40426921
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
0
 
LVL 27

Expert Comment

by:dpearson
ID: 40426955
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
0
 
LVL 7

Author Comment

by:gudii9
ID: 40427058
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;
      }
0
 
LVL 27

Expert Comment

by:dpearson
ID: 40427328
Yes that's what ozo is suggesting.

Doug
0
 
LVL 7

Author Comment

by:gudii9
ID: 40428379
what would be the difference. To  me both approaches looks same.please advise.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40428429
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.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40428618
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

0
 
LVL 84

Expert Comment

by:ozo
ID: 40428669
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;
0
 
LVL 27

Expert Comment

by:dpearson
ID: 40428945
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
0
 
LVL 7

Author Comment

by:gudii9
ID: 40432804
	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;
0
 
LVL 7

Author Comment

by:gudii9
ID: 40432846
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
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

738 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