Solved

lucky13 challenge

Posted on 2016-08-01
11
120 Views
Last Modified: 2016-08-03
Hi,

I am working on below challenge
http://codingbat.com/prob/p194525
I wrote my code as below
public boolean lucky13(int[] nums) {
  
 
 
  
  boolean result=false;

  int sum=0;
  for(int n:nums){
   if(n!=1&&n!=3)
   result=true;
  
  }
   return result;






}

Open in new window





I am not passing all tests.
Expected      Run            
lucky13([0, 2, 4]) → true      true      OK      
lucky13([1, 2, 3]) → false      true      X      
lucky13([1, 2, 4]) → false      true      X      
lucky13([2, 7, 2, 8]) → true      true      OK      
lucky13([2, 7, 1, 8]) → false      true      X      
lucky13([3, 7, 2, 8]) → false      true      X      
lucky13([2, 7, 2, 1]) → false      true      X      
lucky13([1, 2]) → false      true      X      
lucky13([2, 2]) → true      true      OK      
lucky13([2]) → true      true      OK      
lucky13([3]) → false      false      OK      
lucky13([]) → true      false      X      
other tests
X

How to improve my design, approach, code? please advise
0
Comment
Question by:gudii9
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 250 total points
ID: 41738050
public boolean lucky13(int[] nums) {
  for(int y=0;y<nums.length;y++){if(nums[y]==1||nums[y]==3){return false;}}
  return true;
}

Open in new window

0
 
LVL 14

Expert Comment

by:CPColin
ID: 41738053
Start with one of the tests that you're failing and try to figure out why it's failing.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41738193
ok
0
 
LVL 37

Accepted Solution

by:
zzynx earned 250 total points
ID: 41738515
You did it the wrong way.
As soon as you encounter a 1 or a 3 you can return false. (the rest of the items is not important anymore)
If you end the loop you can return true.

public boolean lucky13(int[] nums) {
     for (int n:nums) {
        if (n==1||n==3)
            return false;
     }
     return true;
}

Open in new window

0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41739091
Once again, you have posted code that suggests you haven't even read the Challenge.
Where is your English description of your approach?
Where is there any indication that you have learned anything about the programming process over the past month and 20+ Challenge Questions?
1
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 16

Expert Comment

by:krakatoa
ID: 41739512
Or, if you want to avoid any loops at all, which might do you well :

public boolean lucky13(int[] nums) {
  return !Arrays.toString(nums).contains("1")&&!Arrays.toString(nums).contains("3");
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41739741
Psedo Code Description:

1. check each digit of array by looping in a for loop
2. if find either 1 o 2 then return false immediately
3. All other cases return true
0
 
LVL 7

Author Comment

by:gudii9
ID: 41739750
public class Lucky13 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//lucky13([1, 2, 3]) → false	true
		System.out.println("value is-->"+lucky13[1, 2, 3] );

	}

	public boolean lucky13(int[] nums) {

		boolean result = false;

		int sum = 0;
		for (int n : nums) {
			if (n != 1 || n != 3)
				return false;

		}
		return true;

	}

}

Open in new window


how to call my lucky13 method from main method. Compiler is complaining at line 7


Multiple markers at this line
      - Syntax error, insert "]" to complete
       Expression
      - Syntax error on token "]", delete this
       token
      - lucky13 cannot be resolved to a
       variable
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41739787
public class Lucky13 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//lucky13([1, 2, 3]) → false	true
		int[] iA = {1,2,3};
		System.out.println("value is-->"+lucky13(iA));

	}

	public static boolean lucky13(int[] nums) {

		boolean result = false;

		int sum = 0;
		for (int n : nums) {
			if (n != 1 || n != 3)
				return false;

		}
		return true;

	}

}

Open in new window

0
 
LVL 27

Expert Comment

by:d-glitch
ID: 41739796
I have posted a serious Request for Attention:

gudii9 has approx 140,000 points in Java topic areas, but over the past month or so he has posted 30+ simple "Challenge" questions which seem to baffle him.  I actually believe he his doing this to aggravate the experts for fun.

This question is perhaps the best example of feigned incompetence.  I can ignore him, but that won't stop him from irritating other people and damaging the community.
1
 
LVL 27

Expert Comment

by:rrz
ID: 41741066
I empathize with all the experts that commented here. I have been frustrated by gudii9 in the past.  There were times that I questioned his sincerity.
Look at  
https://www.experts-exchange.com/questions/28397347/multiple-chain-filter-example-in-java.html#a40010408
I must say that he has improved and has learned over the years. I think he took a lot of my advice. In fact, I was the one who taught him the difference between the words advise and advice.
In his profile, he posted "Working in Java, J2EE development area for many years".
He has been a member of EE for 8 years. He has earned 116,036 points and has a Master certification in Java. He has helped others. Look at
https://www.experts-exchange.com/questions/28089957/how-to-call-a-java-class.html 
Many times I wondered whether he was one person or a group of people?
His unorthodox way of learning is difficult to understand. It is difficult to have patience with his endless follow up questions. His habit of working on a dozen questions at once, is crazy. I do admire him for his energy and for his thrist for knowledge. I wouldn't recommend that he be banished from EE.  He has come a long way. I think he should take a course on logic.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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.
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 …
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.

911 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