Solved

fount 9's in java challenge

Posted on 2014-11-05
21
231 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
  • 9
  • 7
  • 5
21 Comments
 
LVL 26

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 26

Expert Comment

by:dpearson
ID: 40425040
Yep that's all correct.
0
 
LVL 84

Expert Comment

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

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 26

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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 26

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 26

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 26

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
strCopies  challenge 17 73
Java and GPO 11 46
Checkbox and ListView in Android Layout 4 37
Not needed 13 53
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

707 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

13 Experts available now in Live!

Get 1:1 Help Now