unlucky1challenge

Hi,

i am working on below challenge
http://codingbat.com/prob/p197308

public boolean unlucky1(int[] nums) {

  int len=nums.length;
 
 
  if(nums[0]==1&nums[1]==3)
  return true;
 
 
 
 if(len>2){
 
    if(nums[1]==1&nums[2]==3)
  return true;
 
 
    if(nums[len-2]==1&nums[len-1]==3)
  return true;
 
    if(nums[len-3]==1&nums[len-2]==3)
  return true;
 
  }
 
 
  else
  return false;
 
}

i am getting below error

Compile problems:


Error:      public boolean unlucky1(int[] nums) {
                     ^^^^^^^^^^^^^^^^^^^^
This method must return a result of type boolean

Possible problem: the if-statement structure may theoretically
allow a run to reach the end of the method without calling return.
Consider adding a last line in the method return some_value;
so a value is always returned.

see Example Code to help with compile problems

how to improve my code and fix the compilation error. what art alternate ways of doing it.

when there are multiple if else if else etc hw to make sure to keep prper{ } and also corresponding else statements etc. please advise
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gudii9Author Commented:
public boolean unlucky1(int[] nums) {

  int len=nums.length;
  
  
   if(len<2){ 
  if(nums[0]==1&nums[1]==3)
  return true;
  }
  
  else{
  
 if(len>2){ 
 
    if(nums[1]==1&nums[2]==3)
  return true;
  
  
    if(nums[len-2]==1&nums[len-1]==3)
  return true;
  
    if(nums[len-3]==1&nums[len-2]==3)
  return true;
  
  }
  
  
  }
  
}
  
}

Open in new window


i tried as above still same error. please advise on how to balance if with else's and write it symmetrically without missing one or other
0
ozoCommented:
if( nums.length<2 ){ return false; }
  for( int i:new int[]{0,1,nums.length-2} ){
     if( nums[ i ]==1 && nums[i+1]==3 ){ return true; }
  }
  return false;
0
ozoCommented:
Add an unconditional  return <boolean value>;  to the end of your function.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

krakatoaCommented:
return nums.length==0||nums.length==1?false:(nums[0]==1&&nums[1]==3)||(nums[1]==1&&nums[2]==3)||(nums[nums.length-2]==1&&nums[nums.length-1]==3)?true:false;

Open in new window

0
gudii9Author Commented:
Add an unconditional  return <boolean value>;  to the end of your function.

i got this.
0
gudii9Author Commented:
public boolean unlucky1(int[] nums) {

  int len=nums.length;
  
  
   if(len<2){ 
  if(nums[0]==1&nums[1]==3)
  return true;
  }
  
  else{
  
 if(len>2){ 
 
    if(nums[1]==1&nums[2]==3)
  return true;
  
  
    if(nums[len-2]==1&nums[len-1]==3)
  return true;
  
    if(nums[len-3]==1&nums[len-2]==3)
  return true;
  
  }
  
  
  }
  return false;
}

Open in new window


i am failing some

Expected      Run            
unlucky1({1, 3, 4, 5}) → true      false      X         
unlucky1({2, 1, 3, 4, 5}) → true      true      OK         
unlucky1({1, 1, 1}) → false      false      OK         
unlucky1({1, 3, 1}) → true      true      OK         
unlucky1({1, 1, 3}) → true      true      OK         
unlucky1({1, 2, 3}) → false      false      OK         
unlucky1({3, 3, 3}) → false      false      OK         
unlucky1({1, 3}) → true      false      X         
unlucky1({1, 4}) → false      false      OK         
unlucky1({1}) → false      Exception:java.lang.ArrayIndexOutOfBoundsException: 1 (line number:7)      X         
unlucky1({}) → false      Exception:java.lang.ArrayIndexOutOfBoundsException: 0 (line number:7)      X         
unlucky1({1, 1, 1, 3, 1}) → false      true      X         
unlucky1({1, 1, 3, 1, 1}) → true      true      OK         
unlucky1({1, 1, 1, 1, 3}) → true      true      OK         
unlucky1({1, 4, 1, 5}) → false      false      OK         
unlucky1({1, 1, 2, 3}) → false      false      OK         
unlucky1({2, 3, 2, 1}) → false      false      OK         
unlucky1({2, 3, 1, 3}) → true      true      OK         
unlucky1({1, 2, 3, 4, 1, 3}) → true      true      OK         
other tests
X         
Correct for more than half the tests

let me study above solutions more deeply
0
gudii9Author Commented:
cani put multiple if's inside else as above?
please advise
0
krakatoaCommented:
public boolean unlucky1(int[] nums) {

  if (! (nums.length >= 2) ) {return false;}
if (nums[0] == 1 && nums[1] == 3 || nums[1] == 1 && nums[2] == 3) {return true;}
else if(nums[nums.length-2] ==1 && nums[nums.length-1] ==3) { return true;}
else {return false;}
}

Open in new window

0
ozoCommented:
public boolean unlucky1(int[] nums) {

  int len=nums.length;
 
 
   if(len<2){
  //if(nums[0]==1&nums[1]==3)
  return false; //return true;
  }
 
  else{
 
 if(len>=2){
 
    if( len>2 && nums[1]==1&nums[2]==3)
  return true;
 
 
    if(nums[len-2]==1&nums[len-1]==3)
  return true;
 
    if(nums[0]==1&nums[1]==3)
  return true;
 
  }
 
 
  }
  return false;
}
0
ozoCommented:
I just noticed that although
if( nums.length<2 ){ return false; }
  for( int i:new int[]{0,1,nums.length-2} ){
     if( nums[ i ]==1 && nums[i+1]==3 ){ return true; }
  }
  return false;
passes all tests, it would have failed if any of the tests had included {1,1} or {3,1}
Here is a possible fix:
public boolean unlucky1(int[] nums) {
  if( nums.length<2 ){ return false; }
  for( int i:nums.length>1?new int[]{0,1,nums.length-2}:new int[]{0} ){
    if( nums[ i ]==1 && nums[i+1]==3 ){ return true; }
  }
  return false;
}
0
krakatoaCommented:
it would have failed if

that's why I made the comment about the data not being fit for purpose. As long as the site hides the params for the "other tests", (which cannot be exhaustive within the types' ranges), these tests are meaningless.
0
gudii9Author Commented:
public boolean unlucky1(int[] nums) {

  if (! (nums.length >= 2) ) {return false;}
if (nums[0] == 1 && nums[1] == 3 || nums[1] == 1 && nums[2] == 3) {return true;}
else if(nums[nums.length-2] ==1 && nums[nums.length-1] ==3) { return true;}
else {return false;}
}

Open in new window


how we know below is false


  if (! (nums.length >= 2) ) {return false;}

please advise
0
gudii9Author Commented:
public boolean unlucky1(int[] nums) {

  int len=nums.length;
  
  
   if(len<2){ 
  if(nums[0]==1&nums[1]==3)
  return true;
  }
  
  else{
  
 if(len>2){ 
 
    if(nums[1]==1&nums[2]==3)
  return true;
  
  
    if(nums[len-2]==1&nums[len-1]==3)
  return true;
  
    if(nums[len-3]==1&nums[len-2]==3)
  return true;
  
  }
  
  
  }
  return false;
}

Open in new window


i failed only 2 test cases . Can i modify above code to fix those 2 tests alone? please advise
0
ozoCommented:
public boolean unlucky1(int[] nums) {

  int len=nums.length;
  
  
   if(len<2){ 
  if(false)//if(nums[0]==1&nums[1]==3)
  return true;
  }
  
  else{
  
 if(true){ //if(len>2){ 
 
    if(len>2 && nums[1]==1&nums[2]==3)//if(nums[1]==1&nums[2]==3)
  return true;
  
  
    if(nums[len-2]==1&nums[len-1]==3)
  return true;
  
    if(nums[0]==1&nums[1]==3)//if(nums[len-3]==1&nums[len-2]==3)
  return true;
  
  }
  
  
  }
  return false;
}

Open in new window

0
ozoCommented:
how we know below is false


  if (! (nums.length >= 2) ) {return false;}
nums.length=1 or nums.length=0 would have no room to contain both a 1 and a 3
0
ozoCommented:
public boolean unlucky1(int[] nums) {

  int len=nums.length;
  
  
   if(len>=2){ //if(len<2){ 
  if(nums[0]==1&nums[1]==3)
  return true;
  }
  
 // else{
  
 if(len>2){ 
 
    if(nums[1]==1&nums[2]==3)
  return true;
  
  
    if(nums[len-2]==1&nums[len-1]==3)
  return true;
  
    //if(nums[len-3]==1&nums[len-2]==3)
  //return true;
  
  }
  
  
//  }
  return false;
}

Open in new window

0
gudii9Author Commented:
public boolean unlucky1(int[] nums) {

  int len=nums.length;
  
  
   if(len>=2){ //if(len<2){ 
  if(nums[0]==1&nums[1]==3)
  return true;
  }
  
 // else{
  
 if(len>2){ 
 
    if(nums[1]==1&nums[2]==3)
  return true;
  
  
    if(nums[len-2]==1&nums[len-1]==3)
  return true;
  
    //if(nums[len-3]==1&nums[len-2]==3)
  //return true;
  
  }
  
  
//  }
  return false;
}

Open in new window


above is classic example of differences between if ..else if...else(my original code) and then if..if..if approaches (above code here in this post )right?
please advise
0
gudii9Author Commented:
public boolean unlucky1(int[] nums) {

  int len=nums.length;
  
  
   if(len>=2){ //if(len<2){ 
  if(nums[0]==1&nums[1]==3)
  return true;
  }
  
 // else{
  
 if(len>2){ 
 
    if(nums[1]==1&nums[2]==3)
  return true;
  
  
    if(nums[len-2]==1&nums[len-1]==3)
  return true;
  
    //if(nums[len-3]==1&nums[len-2]==3)
  //return true;
  
  }
  
  
//  }
  return false;
}

Open in new window


i did not understand the code flow.

  if(len>=2){ //if(len<2){ /for len>=2 we are seeing if 1 is there at 0 position followed by 3 at 1 position right
  if(nums[0]==1&nums[1]==3)
  return true;
  }


 // else{
 
 if(len>2){ //are we not repeating above step here as we covered len>=2??
   if(nums[1]==1&nums[2]==3)//we are checking 1 followed by 3 in postion 1 right?
  return true;
 
 
    if(nums[len-2]==1&nums[len-1]==3)//we are checking 1 followed by 3 in last but one right?
  return true;
 
    //if(nums[len-3]==1&nums[len-2]==3)// i thought we should checking 1 followed by 3 in last but two also right?
  //return true;
 
  }
 
 
//  }
 please advise
0
ozoCommented:
//are we not repeating above step here as we covered len>=2??
above step did not cover nums[1]==1&nums[2]==3

// i thought we should checking 1 followed by 3 in last but two also right?
"unlucky 1 in the first 2 or last 2 positions in the array."
last 2 positions are len-2 and len-1
1 in nums[len-3] is not in either of the last 2 positions
0
gudii9Author Commented:
 
   if(len>=2){ //if(len<2){
  if(nums[0]==1&nums[1]==3)
  return true;
  }
 
 // else{
 
 if(len>2){
 
    if(nums[1]==1&nums[2]==3)
  return true;

above covered first two positions which is clear.

  if(nums[len-2]==1&nums[len-1]==3)
  return true;

Open in new window

above seem to covered 1 in last but one position right.

Are not we supposed to check last postion as well for 1. (does followed means after or before...i am thinking followed means either after or before..) please advise
0
gudii9Author Commented:
unlucky1({1, 2, 3, 4, 3, 1}) → true      true      OK

i mean above shuld give true right?
0
ozoCommented:
unlucky1({1, 1, 1, 3, 1}) → false
the last 1 is not immediately followed by a 3
last is not followed by anything
0
gudii9Author Commented:
unlucky1({1, 1, 1, 3, 1}) → false

the last 1 is not immediately followed by a 3
last is not followed by anything

so no matter what the dictionary definition of 'immediately followed' for codingbat site if they say 'immediately followed' means i have to think as 'after' right(not 'before')??
0
ozoCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.