only14 challenge

Hi,

I am working on below challenge

http://codingbat.com/prob/p162010

Psedo code description of approach :
1. loop through array.
2. check if each individual element is either 1 or 4
3. if step2 is yes return true
4. if step2 no return false

I wrote my code as below


public boolean only14(int[] nums) {
  boolean result=false;
  for(int i=0; i<nums.length;i++){
    if(nums[i]==1||nums[i]==4){
      result=true;
      return result;
    }
    else {
     result=false;
      
    }
  }
  return result;
}

Open in new window



I am not passing all tests
Expected      Run            
only14([1, 4, 1, 4]) → true      true      OK      
only14([1, 4, 2, 4]) → false      true      X      
only14([1, 1]) → true      true      OK      
only14([4, 1]) → true      true      OK      
only14([2]) → false      false      OK      
only14([]) → true      false      X      
only14([1, 4, 1, 3]) → false      true      X      
only14([3, 1, 3]) → false      true      X      
only14([1]) → true      true      OK      
only14([4]) → true      true      OK      
only14([3, 4]) → false      true      X      
only14([1, 3, 4]) → false      true      X      
only14([1, 1, 1]) → true      true      OK      
only14([1, 1, 1, 5]) → false      true      X      
only14([4, 1, 4, 1]) → true      true      OK      
other tests
X      

How to improve my design, approach, code? please advise
LVL 7
gudii9Asked:
Who is Participating?
 
Hammadh Abdul RahmanConnect With a Mentor Commented:
If there is no code in the bracket after the if statement, then nothing happens inside the if statement. Same for else statement.

public boolean only14(int[] nums) {
  boolean result=true;
  for(int i=0; i<nums.length;i++){
   // if(nums[i]==1||nums[i]==4){
   if(nums[i]!=1&&nums[i]!=4){
      result=false;
      return result;
    }
    else {
     result=true;
      
    }
  }
  return result;
}

Open in new window


If the number is neither 1 nor 4 we can be sure the statement "contains only 1 and 4" is false. This is why the if condition has to be as in the code above. But otherwise (if number is either 1 or 4), we can't be sure whether the statement "contains only 1 and 4" is true or false without checking the rest of the numbers. And this is why the else statement is unnecessary.
0
 
Hammadh Abdul RahmanCommented:
Psedo code should be like this :
1. loop through array.
2. check if each individual element is neither 1 nor 4
3. if step2 is yes, return false
4. After checking all elements, return true
0
 
gudii9Author Commented:
Given an array of ints, return true if every element is a 1 or a 4.

only14([1, 4, 1, 4]) → true
only14([1, 4, 2, 4]) → false
only14([1, 1]) → tru
'


above is description of the challenge right

Psedo code should be like this :
1. loop through array.
2. check if each individual element is neither 1 nor 4
3. if step2 is yes, return false
4. After checking all elements, return true

why we have to take carollary backward approach rather than direct forward approach( which seems not working strangely)
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
Hammadh Abdul RahmanCommented:
Because the current approach is returning true if even one number is 1 or 4.

So let's try dry running the first Pseudo code with numbers 1 and 2.
It will loop through the numbers first taking 1.
It will check whether 1is either 1 or 4.
Since the above is true it will return true.

So it is returning true without checking number 2. This is why the Pseudo code doesn't work.


Now let's try dry  running the second Pseudo code with the same numbers 1 and 2.

It will loop through the numbers first taking number 1.
It will check whether 1 is neither 1 nor 4.
Since the above is false it will just move onto the next number which is 2.
It will now check whether 2 is neither 1 nor 4.
Since the above is true it will return false. (Since 2 is neither 1 nor 4)
0
 
gudii9Author Commented:
0
 
gudii9Author Commented:
public boolean only14(int[] nums) {
  boolean result=true;
  for(int i=0; i<nums.length;i++){
    if(nums[i]!=1||nums[i]!=4){
      result=false;
      return result;
    }
    else {
     result=true;
      
    }
  }
  return result;
}

Open in new window


i wonder why i am failing still some test cases

Expected      Run            
only14([1, 4, 1, 4]) → true      false      X      
only14([1, 4, 2, 4]) → false      false      OK      
only14([1, 1]) → true      false      X      
only14([4, 1]) → true      false      X      
only14([2]) → false      false      OK      
only14([]) → true      true      OK      
only14([1, 4, 1, 3]) → false      false      OK      
only14([3, 1, 3]) → false      false      OK      
only14([1]) → true      false      X      
only14([4]) → true      false      X      
only14([3, 4]) → false      false      OK      
only14([1, 3, 4]) → false      false      OK      
only14([1, 1, 1]) → true      false      X      
only14([1, 1, 1, 5]) → false      false      OK      
only14([4, 1, 4, 1]) → true      false      X      
other tests
X      
please advise
0
 
Hammadh Abdul RahmanCommented:
Your original code will work if you change the second line as shown below and delete lines 5 and 6.

boolean result=true;

Open in new window


Your second code will work if you change line 4 as shown below and delete line 9.

    if(nums[i]!=1&&nums[i]!=4){

Open in new window

0
 
gudii9Author Commented:
public boolean only14(int[] nums) {
  boolean result=true;
  for(int i=0; i<nums.length;i++){
   // if(nums[i]!=1||nums[i]!=4){
      if(nums[i]!=1&&nums[i]!=4){
      result=false;
      return result;
    }
    else {
     //result=true;
      
    }
  }
  return result;
}

Open in new window


as above second approach passed all tests
if every element is a 1 or a 4.

as underline above challenge says OR right and then why when i use || it won't work but surprisingly works with &&.

Any tips on when to use || and when to use && (as here it seems paradox to me to use short cut AND when challenge gave OR
0
 
Hammadh Abdul RahmanCommented:
if(nums[i]!=1||nums[i]!=4){

Open in new window


above condition will return true for any given number.

#      nums[i]!=1      nums[i]!=4      nums[i]!=1||nums[i]!=4
1      F                        T                        T
2      T                        T                        T
3      T                        T                        T
4      T                        F                        T
5      T                        T                        T

The two codes below are equivalent. If the first code below seems paradoxical you may use the second code which uses "||".

if(nums[i]!=1&&nums[i]!=4){

Open in new window

if(!(nums[i]==1||nums[i]==4)){

Open in new window

0
 
gudii9Author Commented:
if(nums[i]!=1&&nums[i]!=4){


 

if(!(nums[i]==1||nums[i]==4)){

Open in new window

to bring ! outside of() you ave rto change || to && inside ()?
Bit strange to me . Any good books, links, resources to understand these kind of logic parts more deeper?
may be some venn diagram?
0
 
Hammadh Abdul RahmanCommented:
0
 
awking00Commented:
gudii9, not sure what happened but codingbat link is showing me countEvens challenge and not only14 challenge?
0
 
gudii9Author Commented:
public boolean only14(int[] nums) {
  boolean result=true;
  for(int i=0; i<nums.length;i++){
    if(nums[i]==1||nums[i]==4){
      //result=true;
     // return result;
    }
    else {
     result=false;
      
    }
  }
  return result;
}

Open in new window



first approach also worked as you mentioned. what is meaning of if loop with empty implementation in it?
i see both line commented then magically all test cases are passing? please advise
 boolean algebra link is good. i am reading that now
0
 
gudii9Author Commented:
public boolean only14(int[] nums) {
  boolean result=true;
  for(int i=0; i<nums.length;i++){
   // if(nums[i]!=1||nums[i]!=4){
      if(nums[i]!=1&&nums[i]!=4){
      result=false;
      return result;
    }
    else {
     //result=true;
      
    }
  }
  return result;
}

Open in new window


what is meaning of empty else implementation as below in case of second approach?

    else {
     //result=true;
     
    }

what happens inside else as no code is there in that?? please advise
0
 
gudii9Author Commented:
public boolean only14(int[] nums) {
  boolean result=true;
  for(int i=0; i<nums.length;i++){
   // if(nums[i]==1||nums[i]==4){
   if(nums[i]!=1&&nums[i]!=4){
      result=false;
      return result;
    }
    else {
     result=true;
      
    }
  }
  return result;
}

Open in new window


i was more clear on above which passes all tests.

so inside if it is always better to use condition which you know 100%(as above latest && approach in if condition) is true or false rather than putting some condition which is kind of shaky(like my original approach??)?
0
 
gudii9Author Commented:
If the number is neither 1 nor 4 we can be sure the statement "contains only 1 and 4" is false.

when you say statement you mena if statement or else statement or the array statement like {1,4,2,4}

please advise
0
 
gudii9Author Commented:
If the number is neither 1 nor 4 we can be sure the statement "contains only 1 and 4" is false. This is why the if condition has to be as in the code above. But otherwise (if number is either 1 or 4), we can't be sure whether the statement "contains only 1 and 4" is true or false without checking the rest of the numbers. And this is why the else statement is unnecessary.

are we not supposed to check all numbers to say statement "contains only 1 and 4" is false.??

you mean checking one number is suffice If the number is neither 1 nor 4 .

I am having some confusion still here?
may be some diagram will help me?
0
 
gudii9Author Commented:
I think I got it
 for(int i=0; i<nums.length;i++){
 // if(nums[i]==1||nums[i]==4){
 if(nums[i]!=1&&nums[i]!=4){


 lets say given array is {3,2,7,1}
 so index 0 is 3 which is neither 1 nor 4 hence we can safely say array has only 1 or 4 as elements is false

 where as if I write as below


 for(int i=0; i<nums.length;i++){
 if(nums[i]==1||nums[i]==4){

 given array is {1,3,2,4}

 index 0 is element 1 so if(nums[i]==1||nums[i]==4)===>true but we cannot stop here and say array has only 1 or 4 as elements as we are forced to check other elements as we well like 3, 2, 4 etc.


 any kind of pictures or flow diagrams etc we can draw to understand these coding challenges easily.

Open in new window

0
 
Hammadh Abdul RahmanCommented:
I think a truth table is the best tool with this kind of problems.

It is a diagram in rows and columns showing how the truth or falsity of a proposition varies with that of its components.

x      condition
1      true/false/Can't Determine
2      true/false/Can't Determine
...      true/false/Can't Determine

Draw a truth table like the one above, showing what you can determine given that you know the current number is x and don't know the rest of the numbers. Then you can use the truth table to help you write the IF condition. Your IF statement should trigger for the x values that have either "true" or "false" in the second column of the table.

You could also write stuff like x<10 in the x column, and write what you can determine if x is less than 10.
0
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.

All Courses

From novice to tech pro — start learning today.