Solved

only14 challenge

Posted on 2016-08-03
19
63 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Expert Comment

by:Hammadh Abdul Rahman
ID: 41743252
0
 
LVL 31

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

706 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

20 Experts available now in Live!

Get 1:1 Help Now