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

asked on

withoutDoubles challenge

Hi,

I am working on below challenge
http://codingbat.com/prob/p115233

I tried my code as below
public int withoutDoubles(int die1, int die2, boolean noDoubles) {

if(noDoubles==false){
return die1+die2;
}
  
  else if(die1==die2){
  return die1+die2+1;
  
  }
  else
  return 1;
}

Open in new window


I am getting below result
Expected      Run            
withoutDoubles(2, 3, true) → 5      1      X         
withoutDoubles(3, 3, true) → 7      7      OK         
withoutDoubles(3, 3, false) → 6      6      OK         
withoutDoubles(2, 3, false) → 5      5      OK         
withoutDoubles(5, 4, true) → 9      1      X         
withoutDoubles(5, 4, false) → 9      9      OK         
withoutDoubles(5, 5, true) → 11      11      OK         
withoutDoubles(5, 5, false) → 10      10      OK         
withoutDoubles(6, 6, true) → 7      13      X         
withoutDoubles(6, 6, false) → 12      12      OK         
withoutDoubles(1, 6, true) → 7      1      X         
withoutDoubles(6, 1, false) → 7      7      OK         
other tests
OK         

how to  improve my approach, results and design of this challenge. How do i make a graphical venn or flow chart or some other relevant diagram to design it before writing code to decide best strategy?
 Please advise
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

iirc, you already did this challenge quite a while ago.

?
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America 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
Avatar of gudii9

ASKER

public int withoutDoubles(int die1, int die2, boolean noDoubles) {

if(noDoubles && die1==die2){
  return die1+die2%6+1;
}
  
 
  
  
  else
  return die1+die2; 
}

Open in new window



above passed all cases as you mentioned.

My question is can i write as below as well?

public int withoutDoubles(int die1, int die2, boolean noDoubles) {

if(noDoubles && die1==die2){
  return die1%6+die2+1;
}
  
 
  
  
  else
  return die1+die2; 
}

Open in new window



which is best approach ?

die1%6 approach or die1%6 approach?
does it vary results if i change approach to  die1%6
Avatar of gudii9

ASKER

public int withoutDoubles(int die1, int die2, boolean noDoubles) {

if(noDoubles && die1==die2){
  return die1%6+die2+1;
}
  
 
  
  
  else
  return die1+die2; 
}

Open in new window


apparently above also passed all tests??
die1%6 approach or die1%6 approach?
looks the same to me
Avatar of gudii9

ASKER

wrapping around to 1 if its value was 6.

but challenge did not mention die1 or die2? i wonder which one to take ideally?
Avatar of gudii9

ASKER

withoutDoubles(6, 5, true) →

withoutDoubles(6, 5, false) →

withoutDoubles(5, 6, true) →

withoutDoubles(5, 6, false) →

what results we get in above 4 cases ?

please advise
In above 4 cases, the two dice do not show the same value, so just return the sum.
Avatar of gudii9

ASKER

oops i missed the point that both dies needs to be equal to do
die1%6+die2+1
then it really does not matter die1%6 or die2%6.

How to read and comprehend challenge correctly first time itself like you people?
it is some times taking many times of going over to understand the depth and width of the challenge?