maxMod5  challenge

gudii9
gudii9 used Ask the Experts™
on
Hi,

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

I tried my code as below
public int maxMod5(int a, int b) {
  if(a>b){
  return a;
  }
  else{
  
 if( (a==b)){
 
 return 0;
 }
 
 else if( (b%5==a%5)){
 
 return 0;
 }
 
 }
 return 1;
  
}

Open in new window


I am getting below result
Expected      Run            
maxMod5(2, 3) → 3      1      X         
maxMod5(6, 2) → 6      6      OK         
maxMod5(3, 2) → 3      3      OK         
maxMod5(8, 12) → 12      1      X         
maxMod5(7, 12) → 7      0      X         
maxMod5(11, 6) → 6      11      X         
maxMod5(2, 7) → 2      0      X         
maxMod5(7, 7) → 0      0      OK         
maxMod5(9, 1) → 9      9      OK         
maxMod5(9, 14) → 9      0      X         
maxMod5(1, 2) → 2      1      X         
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
 return a==b?0:(a%5)==(b%5)?Math.min(a,b):Math.max(a,b);

Open in new window

Author

Commented:
can i write else if without condition similar to else?
Apart from Venn diagrams, I think you'll eventually need to grasp narratively-presented logic as in the present challenge.

"Given two int values, return whichever value is larger. However if the two values have the same remainder when divided by 5, then the return the smaller value. However, in all cases, if the two values are the same, return 0."

So deal with the clear instruction "However, in all cases, if the two values are the same, return 0." first. This gives you 'a==b?0;' Now you have two other conditions to assess. You have to deal with the mod (remainder) case first, because comparing the two ints  just based on their size at this stage, is not good enough, as the two ints *could* be something like 15 and 500, both of which mod by 5, leaving 0 remainder in both cases.  So you go 'a%5 == b%5 ?Math.min(a,b)' and you have the second part of your return statement. That just leaves the clean-up comparison, which is a simple size issue -> return the largest of a and b.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

if(a==b){
  return 0;
  
  }
  
  else if(a%5==b%5){
  return Math.min(a,b);
  }
  
  return Math.max(a,b);

Open in new window

Author

Commented:
if(a==b){
  return 0;
  
  }
  
  else if(a%5==b%5){
  return Math.min(a,b);
  }
  
  return Math.max(a,b);

Open in new window


above worked perfect?

My question is which condition to put in top if block
and which condition to put in middle else if block
and which condition to put in bottom else block.

when i interachage like below if with else if condition it failed one

public int maxMod5(int a, int b) {

 

 if(a%5==b%5){
 
 return Math.min(a,b);
 }
 else  if( (a==b)){
 
 return 0;
 }
 
 
  else {
 
 return Math.max(a,b);
 
}
  
 
 }
 
 
 

Open in new window


please advise
public int maxMod5(int a, int b) {
  
 if(a==b){return 0;}

 if(a%5==b%5){return Math.min(a,b);}

  else {return Math.max(a,b);
 
 }
 
}

Open in new window

Author

Commented:
public int maxMod5(int a, int b) {

 

 if(a%5==b%5){
 
 return Math.min(a,b);
 }
 else  if( (a==b)){
 
 return 0;
 }
 
 
  else {
 
 return Math.max(a,b);
 
}
  
 
 }
 
 
 

Open in new window


why above failed one test as below when i interchange if and else if conditions as above?

Expected      Run            
maxMod5(2, 3) → 3      3      OK         
maxMod5(6, 2) → 6      6      OK         
maxMod5(3, 2) → 3      3      OK         
maxMod5(8, 12) → 12      12      OK         
maxMod5(7, 12) → 7      7      OK         
maxMod5(11, 6) → 6      6      OK         
maxMod5(2, 7) → 2      2      OK         
maxMod5(7, 7) → 0      7      X         
maxMod5(9, 1) → 9      9      OK         
maxMod5(9, 14) → 9      9      OK         
maxMod5(1, 2) → 2      2      OK         
other tests
X         
Correct for more than half the tests
What you need to think about is when the ints are the same and you run the mod test first.

Author

Commented:
What you need to think about is when the ints are the same and you run the mod test first.

Open in new window


but we are doing reverse right as below

public int maxMod5(int a, int b) {
  
 if(a==b){return 0;}

 if(a%5==b%5){return Math.min(a,b);}

  else {return Math.max(a,b);
 
 }
 
}

Open in new window


as above we are instead doing as below

What you need to think about is when the ints are the same and you run the mod test second or next or later??

please advise.
Ask yourself what happens when a==10 and say b==20.

Author

Commented:
Ask yourself what happens when a==10 and say b==20.

mods same as % gives 0 and 0 which are equal

first if skipped goes to second one i.e else if
as below
if(a==b){
  return 0;
  
  }
  
  else if(a%5==b%5){
  return Math.min(a,b);
  }

Open in new window

Author

Commented:
However, in all cases,
in the challenge i have to read above more clearly.

If it says however in all cases means it comes at top if loop right? (not in else if /else)

Author

Commented:
public int maxMod5(int a, int b) {

 

 if(a%5==b%5){
 
 return Math.min(a,b);
 }
 else  if( (a==b)){
 
 return 0;
 }
 
 
  else {
 
 return Math.max(a,b);
 
}
 
 
 }
 
 
 
 
if code as above i see why below is failing

maxMod5(7, 7) → 0      7      X
as the % is 2 and other % also 2 so goes to first above if loop prints min of 7, 7 which is 7.

But challenge says there is a deviation by saying 'However in all cases'
which means this one is like overriding all other instructions and comes as high priority number one thing(not two, three) to take care.

please advise my understanding is correct?
Yes, understanding the wording is important.

But it looks like you can see the reason now.

Author

Commented:
yes kind of 90%
So you must then know the 10% that you still don't know . . . so what is that last 10% ?

Author

Commented:
understanding the wording is important.

which comes first which comes second which comes third and last and also i am sometimes confusing between || and && which makes up rest of 10% i feel
"Given two int values, return whichever value is larger. However if the two values have the same remainder when divided by 5, then the [sic] return the smaller value. However, in all cases, if the two values are the same, return 0. "

Is the Question.

Here is another way of writing that :

Given two int values, return whichever value is larger, BUT if the two values have the same remainder when divided by 5, then return the smaller value, EXCEPT in all cases where the two values are the same, in which case return 0.

So you need to look at the particular cases first, (edges) and then after that you can deal with the remaining cases, which cover everything else - and "everything else" in this question means : "the larger of the two ints". So cases that should return "0" are the most particular, so deal with them first, then the mod cases, and then, as I said above.

Author

Commented:
However means BUT and EXCEPT
identifying the edges or edge case important.

So cases that should return "0" are the most particular, so deal with them first

how do i conclude some sentence(out of given 3 sentences) is most particular than other?

Author

Commented:
i am getting after trying 5-10 times that some sentence is most particular? But not first time or just before start attacking the challenge?
how do i conclude some sentence(out o . . . . .

"
When it rains, I don't go out at all, HOWEVER I do still go to the end of the street if it's a Wednesday, HOWEVER, if it's after 3 o'clock, then I just go into the garden.
"

Author

Commented:
HOWEVER, if it's after 3 o'clock

above is most particular...no matter rain or which day of week you go out i.e to garden.

Is my understanding is correct?
I thought you might list the tests, in the order they need to be made, to decide what action to take.
Is my understanding is correct?

Yes.


-------------------------

I'm not saying that was the best or even a very good example, but you should look at each "clause" in a problem, and see how it impacts the other clauses or terms in the statement, and build the logic based on that. As you touched on in earlier questions, perhaps sometimes a Venn diagram could help, but that would be your personal preference to use or not. I would personally prefer a flow chart approach, but then again, I don't know what the production experts here would say to that these days.

In short, just think things through, several times over, and usually deal with special cases  before anything else.

Author

Commented:
I too like flow chart than anything else

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial