Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

love6 challenge java

Hi,

I am working one below challenge

http://codingbat.com/prob/p137742

public boolean love6(int a, int b) {
 
  if((a==6||b==6)|(a+b==6)|(a-b==6))
  {
  return true;
  }
  else{
  return false;
  }
}
i am failing one test case

Expected      Run            
love6(6, 4) → true      true      OK         
love6(4, 5) → false      false      OK         
love6(1, 5) → true      true      OK         
love6(1, 6) → true      true      OK         
love6(1, 8) → false      false      OK         
love6(1, 7) → true      false      X         
love6(7, 5) → false      false      OK         
love6(8, 2) → true      true      OK         
love6(6, 6) → true      true      OK         
love6(-6, 2) → false      false      OK         
love6(-4, -10) → true      true      OK         
love6(-7, 1) → false      false      OK         
love6(7, -1) → true      true      OK         
love6(-6, 12) → true      true      OK         
love6(-2, -4) → false      false      OK         
love6(7, 1) → true      true      OK         
love6(0, 9) → false      false      OK         
love6(8, 3) → false      false      OK         
love6(3, 3) → true      true      OK         
love6(3, 4) → false      false      OK         
other tests
OK         

how do i design this problem with flow chart etc and how to improve my design and implementation. please advise
ASKER CERTIFIED SOLUTION
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or mebbe this :

return (Math.abs(Math.max(a,b)-Math.min(a,b))==6)||(a+b==6)?true:a+b-a==6||a+b-b==6?true:false;

Open in new window

Avatar of gudii9

ASKER

ASCII flowchart

i like this. Where i can find more information on it. I never used it. Please advise
Honestly, I made it up. Apparently it wasn't orginal though because there is a free tool to do something similar at asciiflow.com.
Avatar of gudii9

ASKER

The question gave you a clue about using Math.abs(..)

i overlooked that point

Secondly, instead of using bitwise-OR you need to use ||

why is it preferred due to performance (where if it is false (or true) then does not bother to check next one whether true or false since result is anyway false?
Yes its cleaner and quicker to use logical OR.
Avatar of gudii9

ASKER

(Math.abs(Math.max(a,b)-Math.min(a,b))==6)||(a+b==6)?true:a+b-a==6||a+b-b==6?true:false;

above is not clear to me. please advise
Avatar of gudii9

ASKER

(Math.max(a,b)-Math.min(a,b))==6)-->checking max and min value?

||(a+b==6) then or condition(ie not bit or??)

?true:a+b-a==6||a+b-b==6?true:false

too confusing to me
this is easier . . .

return (Math.abs(a-b)==6)||(a+b==6)?true:a==6||b==6?true:false;

Open in new window

return a==6 || b==6 || a+b==6 || Math.abs(a-b)==6;
return a==6 || b==6 || a+b==6 || Math.abs(a-b)==6; 

Open in new window

I think you'll find i've already posted that ;)
i've already posted that ;)
Then why is anyone still posting anything else?
Not sure ;)
I posted because the OP said he didnt follow my code.
Then why is anyone still posting anything else?

I might be wrong, but I think CEHJ was talking to *you* . ;)
:) A classic guddi9 puzzler
Yep - always bound to become more complicated than the original question. ;)

return a+b==6||a-b==6||b-a==6||a+b-a==6||a+b-b==6;

Open in new window

Avatar of gudii9

ASKER

is it necessary to understand all solutions given by everyone or take the one which i understand easily?
Please advise
Avatar of gudii9

ASKER

what is limit of ternary inside other ternary inside other ternary...i can go till any number?
Avatar of gudii9

ASKER

I am some how not good fan of ternaries not sure why
Avatar of gudii9

ASKER

my mind wont go further down if it has more than 2, 3 ternaries not sure how you guys picture, imagine and code multiple ternaries scenarios
Avatar of gudii9

ASKER

return a+b==6||a-b==6||b-a==6||a+b-a==6||a+b-b==6;

in above line what is meaning of last part as below?

a+b-a==6||a+b-b==6(a+b-a means b right and same way a+b-b results in a right???not sure what we are conveying to compiler or to jvm whichever

other first part is clear which is a+b==6||a-b==6||b-a==6
is it necessary to understand all solutions given by everyone or take the one which i understand easily?
Please advise

Eventually, yes; but at the outset . . . like perhaps now . . . it's best to understand one at a time, the if statement structure first - which all the guys here and in your others questions are masterful at, and then, after that, the ternary ops.
OK so my last post was severely overcomplicated and you ought to ignore it really for now, and look at what gurps, ozo and CEHJ have recommended, as these are all succinct and therefore powerful as well as easy to understand answers.
my mind wont go further down if it has more than 2, 3 ternaries not sure how you guys picture, imagine and code multiple ternaries scenarios

I certainly don't ;) That's just spaghetti
I class mine as tagliatelle. ;)
I would aim for lasagne at the very worst
Ahhh . . . so you are then nesting quite deeply. ;)