Solved

only14 challenge

Posted on 2016-08-03
19
70 Views
Last Modified: 2016-08-06
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
0
Comment
Question by:gudii9
  • 11
  • 7
19 Comments
 
LVL 4

Expert Comment

by:Hammadh Abdul Rahman
ID: 41741699
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
 
LVL 7

Author Comment

by:gudii9
ID: 41741703
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
 
LVL 4

Expert Comment

by:Hammadh Abdul Rahman
ID: 41741708
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
 
LVL 7

Author Comment

by:gudii9
ID: 41741709
0
 
LVL 7

Author Comment

by:gudii9
ID: 41741713
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
 
LVL 4

Expert Comment

by:Hammadh Abdul Rahman
ID: 41741717
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
 
LVL 7

Author Comment

by:gudii9
ID: 41742499
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
 
LVL 4

Expert Comment

by:Hammadh Abdul Rahman
ID: 41743133
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
 
LVL 7

Author Comment

by:gudii9
ID: 41743213
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
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 4

Expert Comment

by:Hammadh Abdul Rahman
ID: 41743252
0
 
LVL 32

Expert Comment

by:awking00
ID: 41744773
gudii9, not sure what happened but codingbat link is showing me countEvens challenge and not only14 challenge?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41745096
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
 
LVL 7

Author Comment

by:gudii9
ID: 41745101
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
 
LVL 7

Author Comment

by:gudii9
ID: 41745104
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
 
LVL 4

Accepted Solution

by:
Hammadh Abdul Rahman earned 500 total points
ID: 41745138
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
 
LVL 7

Author Comment

by:gudii9
ID: 41745635
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
 
LVL 7

Author Comment

by:gudii9
ID: 41745641
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
 
LVL 7

Author Comment

by:gudii9
ID: 41745644
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
 
LVL 4

Expert Comment

by:Hammadh Abdul Rahman
ID: 41745739
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

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

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 77
splitOdd10 challenge 5 81
Groovy problem when using SOAPUI : DispatchException occurred 7 30
MySQL  on Tomcat 8 30
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.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

863 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

23 Experts available now in Live!

Get 1:1 Help Now