Solved

has22 challenge

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

I am working on below challenge

http://codingbat.com/prob/p121853
I wrote my code as below
public boolean has22(int[] nums) {
 
  
  boolean result=false;

  int sum=0;
  for(int n:nums){
   if(n==2)
   result=true;
  
  }
   return result;




}

Open in new window






I am not passing all tests

Expected      Run            
has22([1, 2, 2]) → true      true      OK      
has22([1, 2, 1, 2]) → false      true      X      
has22([2, 1, 2]) → false      true      X      
has22([2, 2, 1, 2]) → true      true      OK      
has22([1, 3, 2]) → false      true      X      
has22([1, 3, 2, 2]) → true      true      OK      
has22([2, 3, 2, 2]) → true      true      OK      
has22([4, 2, 4, 2, 2, 5]) → true      true      OK      
has22([1, 2]) → false      true      X      
has22([2, 2]) → true      true      OK      
has22([2]) → false      true      X      
has22([]) → false      false      OK      
has22([3, 3, 2, 2]) → true      true      OK      
has22([5, 2, 5, 2]) → false      true      X      
other tests
X      


How to improve my design, approach, code? 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
  • 4
  • 4
  • 3
11 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 250 total points
ID: 41738045
You're supposed to return true only when there's more than one 2 in a row. Your code is returning true when there's a 2 anywhere.
0
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 250 total points
ID: 41738077
public boolean has22(int[] nums) {
  
  for(int y=0;y<nums.length-1;y++){xxxxxxxxxxx){return true;}}
  return false;
}

Open in new window


... what do you insert instead of the xs??
0
 
LVL 7

Author Comment

by:gudii9
ID: 41738189
let me check
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 16

Expert Comment

by:krakatoa
ID: 41738266
Is this snippet relevant or not in this question:

if(nums[y]==2&&nums[y+1]==2)

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41738385
public boolean has22(int[] nums) {
 
  
  boolean result=false;

  int sum=0;
  int len=nums.length;
  for(int i=0;i<len-1;i++){
   if(nums[i]==2&&nums[i+1]==2)
   result=true;
  
  }
   return result;




}

Open in new window


above passed all tests? any improvements, suggestions, comments?
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41738425
You could return true as soon as you find two 2's next to each other and save a little processing time. (If you have an array that's a thousand elements long and the first two elements are 2's, you don't need to check the rest of the array, since you already know you'll be returning true.)
0
 
LVL 7

Author Comment

by:gudii9
ID: 41738441
You could return true as soon as you find two 2's next to each other and save a little processing time.

how to do this?
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41738446
return true;
0
 
LVL 7

Author Comment

by:gudii9
ID: 41738454
public boolean has22(int[] nums) {
 
  
 // boolean result=false;

  int sum=0;
  int len=nums.length;
  for(int i=0;i<len-1;i++){
   if(nums[i]==2&&nums[i+1]==2)
  return true;
  
  }
  // return result;
return false;



}

Open in new window


i see . this is very powerful to use return here.
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41738455
Yep! You can't always break out of a loop early or return from a loop early, but it can be nice, when you can.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41738788
i see . this is very powerful to use return here.

I ALREADY posted code with this return clause in it !

Did you even *look* at that ???

You ARE a troll, are you not ????
1

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Suggested Courses

617 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