inOrderEqual challenge

Hi,

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

I tried my code as below

public boolean inOrderEqual(int a, int b, int c, boolean equalOk) {

if((a>b)&&(b>c)){
return true;
}

else if((equalOk==true)){

if((a==b)||(b>c))
return true;

else
return false;

}

else 
return false;
  
}

Open in new window



I am getting below result



Expected      Run            
inOrderEqual(2, 5, 11, false) → true      false      X         
inOrderEqual(5, 7, 6, false) → false      false      OK         
inOrderEqual(5, 5, 7, true) → true      true      OK         
inOrderEqual(5, 5, 7, false) → false      false      OK         
inOrderEqual(2, 5, 4, false) → false      false      OK         
inOrderEqual(3, 4, 3, false) → false      false      OK         
inOrderEqual(3, 4, 4, false) → false      false      OK         
inOrderEqual(3, 4, 3, true) → false      true      X         
inOrderEqual(3, 4, 4, true) → true      false      X         
inOrderEqual(1, 5, 5, true) → true      false      X         
inOrderEqual(5, 5, 5, true) → true      true      OK         
inOrderEqual(2, 2, 1, true) → false      true      X         
inOrderEqual(9, 2, 2, true) → false      false      OK         
inOrderEqual(0, 1, 0, true) → false      true      X         
other tests
X

how to  improve my approach and design of this challenge. How do i make a

graphical venn or some other relevant diagram to design it before writing single

line of code to decide best strategy?
 Please advise
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
public boolean inOrderEqual(int a, int b, int c, boolean equalOk) {
  return equalOk?a<=b && b<=c:a<b && b<c;
}
or
public boolean inOrderEqual(int a, int b, int c, boolean equalOk) {
  if( equalOk ){ b+=1; c+=2; }
  return a<b && b<c;
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alexandre SimõesManager / Technology SpecialistCommented:
I like these things! :)

Here's mine:
public boolean inOrderEqual(int a, int b, int c, boolean equalOk) {
  if(equalOk)
    return (a <= b) && (b <= c);
  else
    return (a < b) && (b < c);
}

Open in new window

Sorry OZO, just realized that this is the same as your first solution :)
gudii9Author Commented:
public boolean inOrderEqual(int a, int b, int c, boolean equalOk) {

if((b>a)&&(c>b)){
return true;
}

else if((equalOk==true)){

if((a==b)||(b==c))
return true;

else
return false;

}

else 
return false;
  
}

Open in new window


i updated my code still failing two how to fix my above code?
Expected      Run            
inOrderEqual(2, 5, 11, false) → true      true      OK         
inOrderEqual(5, 7, 6, false) → false      false      OK         
inOrderEqual(5, 5, 7, true) → true      true      OK         
inOrderEqual(5, 5, 7, false) → false      false      OK         
inOrderEqual(2, 5, 4, false) → false      false      OK         
inOrderEqual(3, 4, 3, false) → false      false      OK         
inOrderEqual(3, 4, 4, false) → false      false      OK         
inOrderEqual(3, 4, 3, true) → false      false      OK         
inOrderEqual(3, 4, 4, true) → true      true      OK         
inOrderEqual(1, 5, 5, true) → true      true      OK         
inOrderEqual(5, 5, 5, true) → true      true      OK         
inOrderEqual(2, 2, 1, true) → false      true      X         
inOrderEqual(9, 2, 2, true) → false      true      X         
inOrderEqual(0, 1, 0, true) → false      false      OK         
other tests
OK         
Determine the Perfect Price for Your IT Services

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

ozoCommented:
inOrderEqual(2, 2, 1, true) → false      true      X        
inOrderEqual(9, 2, 2, true) → false      true      X
Are you asking why Expected is false, or are you asking why Run is true.
gudii9Author Commented:
public boolean inOrderEqual(int a, int b, int c, boolean equalOk) {

if((b>a)&&(c>b)){
return true;
}

else if((equalOk==true)){

if((a==b)||(b==c))
return true;

else
return false;

}

else 
return false;
  
}

Open in new window

i am wondering how to tweak my code to pass remaining two test cases as well? I passed most of them except 2. please advise
ozoCommented:
Do you understand why Run is true when the code in http:#a41363836 is invoked for inOrderEqual(2, 2, 1, true) and inOrderEqual(9, 2, 2, true) ?

In line 9, what does (a==b)||(b==c) mean?
What does "equality is allowed' mean as a 'However' modifier of ''strict increasing order?
What does (a<=b)&&(b<=c) mean?
gudii9Author Commented:
if((a==b)||(b==c))
return true;

i am blindly checking only equal to as above without having knowledge to see if increasing or not?
Not sure how i tell that to program to care about increasing order as well?
gudii9Author Commented:
exception that if "equalOk" is true, equality is allowed, such as 5 5 7 or 5 5 5.

equality allowed if  "equalOk" is true but still need to worry about increasing order or not is not clear to me?


inOrderEqual(2, 2, 1, true) → false      true      X         
inOrderEqual(9, 2, 2, true) → false      true

run is true even though decreasing order as we  have "equalOk" is true. which i understand now. But how to tweak my code to ignore decreasing order case if "equalOk" is true. ??
gudii9Author Commented:
inOrderEqual(2, 2, 1, true) → false      true      X         
inOrderEqual(9, 2, 2, true) → false      true      X

actually i did not understand above two RUN. please advise
ozoCommented:
(a==b)||(b==c)

inOrderEqual(2, 2, 1, true)
What is (2==2)||(2==1) ?

inOrderEqual(9, 2, 2, true)
What is (9==2)||(2==2)?
gudii9Author Commented:
inOrderEqual(2, 2, 1, true)
What is (2==2)||(2==1) ?===>true as true||false is true

inOrderEqual(9, 2, 2, true)
What is (9==2)||(2==2)?===>true as false||true is true
ozoCommented:
So now you understand why Run is true.
Do you understand why Expected is false?
gudii9Author Commented:
Do you understand why Expected is false?
i know why expected is false.

i want to know how to fix my code approach as below

public boolean inOrderEqual(int a, int b, int c, boolean equalOk) {

if((b>a)&&(c>b)){
return true;
}

else if((equalOk==true)){

if((a==b)||(b==c))
return true;

else
return false;

}

else 
return false;
  
}

Open in new window


not sure above approach is fixable or not?
ozoCommented:
Do you understand the difference between
(a==b)||(b==c)
and
(a<=b)&&(b<=c)
gudii9Author Commented:
yes,
(a==b)||(b==c) we are checking equality with shortcut OR
(a<=b)&&(b<=c) we are checking leass than equal conditions with short circuit AND
gudii9Author Commented:
public boolean inOrderEqual(int a, int b, int c, boolean equalOk) {

if(equalOk)
    return (a <= b) && (b <= c);
  else
    return (a < b) && (b < c);
  
}

Open in new window


above worked well.

my challenge is which one to within if condition which one not to take and order of elimination wider elimination to lower elimination etc.

please advise
gudii9Author Commented:
public boolean inOrderEqual(int a, int b, int c, boolean equalOk) {
  if(equalOk)
    return (a <= b) && (b <= c);
  else
    return (a < b) && (b < c);
}

Open in new window


how we took equalOk in if loop?
please advise
gudii9Author Commented:

Given three ints, a b c, return true if they are in strict increasing order, such as 2 5 11, or 5 6 7, but not 6 5 7 or 5 5 7. However, with the exception that if "equalOk" is true, equality is allowed, such as 5 5 7 or 5 5 5.

inOrderEqual(2, 5, 11, false) → true
inOrderEqual(5, 7, 6, false) → false
inOrderEqual(5, 5, 7, true) → true

i guess i di dnot understand the challenge and how to go about making diagram and then code with right logic?


public boolean inOrderEqual(int a, int b, int c, boolean equalOk) {
  if(equalOk)
    return (a <= b) && (b <= c);
  else
    return (a < b) && (b < c);
}

Open in new window


how we started with   if(equalOk)
we are saying if equalOk boolean is true then less than and also equal is fine
where as if equalOk false then mpre than also equal is  fine all other not fine result false??
gudii9Author Commented:
if given challenge has boolean parameter then take that boolean parameter like equalOk in if loop to make life simple?
ozoCommented:
Which of
    inOrderEqual(2, 5, 11, false) → true
    inOrderEqual(5, 7, 6, false) → false
    inOrderEqual(5, 5, 7, true) → true
did you not understand, and how would you have interpreted
    Given three ints, a b c, return true if they are in strict increasing order, such as 2 5 11, or 5 6 7, but not 6 5 7 or 5 5 7. However, with the exception that if "equalOk" is true, equality is allowed, such as 5 5 7 or 5 5 5.
in such a case?
gudii9Author Commented:
public boolean inOrderEqual(int a, int b, int c, boolean equalOk) {

if(equalOk){
return ((b>=a)&&(c>=b));
}

else if(equalOk){
return ((b==a)||(c==b)||(c==a));
  
}
return false;
}

Open in new window


i improved my code still failing one. please advise

Expected      Run            
inOrderEqual(2, 5, 11, false) → true      false      X         
inOrderEqual(5, 7, 6, false) → false      false      OK         
inOrderEqual(5, 5, 7, true) → true      true      OK         
inOrderEqual(5, 5, 7, false) → false      false      OK         
inOrderEqual(2, 5, 4, false) → false      false      OK         
inOrderEqual(3, 4, 3, false) → false      false      OK         
inOrderEqual(3, 4, 4, false) → false      false      OK         
inOrderEqual(3, 4, 3, true) → false      false      OK         
inOrderEqual(3, 4, 4, true) → true      true      OK         
inOrderEqual(1, 5, 5, true) → true      true      OK         
inOrderEqual(5, 5, 5, true) → true      true      OK         
inOrderEqual(2, 2, 1, true) → false      false      OK         
inOrderEqual(9, 2, 2, true) → false      false      OK         
inOrderEqual(0, 1, 0, true) → false      false      OK         
other tests
X         

not sure why i am getting false instead of run as it is strick increasing order from a to b to c and equalOk also is false(hence no exception)?

attached screenshot helped in this
oderEqual.png
ozoCommented:
It looks like you always return false when equalOk is false
Is that what you intended to do?
gudii9Author Commented:
public boolean inOrderEqual(int a, int b, int c, boolean equalOk) {

if(equalOk){

     if((b==a)||(c==b)||(c==a)||a<=b||b<=c||c<=a){
     
     return true;
     }
     else{
     return false;
     }
}
else{

       if((b>a)&&(c>b)&&(c>a)){
     
     return true;
     }
     else{
     return false;
     }




}     
     




}

Open in new window


i changed approach as above still failing few

Expected      Run            
inOrderEqual(2, 5, 11, false) → true      true      OK         
inOrderEqual(5, 7, 6, false) → false      false      OK         
inOrderEqual(5, 5, 7, true) → true      true      OK         
inOrderEqual(5, 5, 7, false) → false      false      OK         
inOrderEqual(2, 5, 4, false) → false      false      OK         
inOrderEqual(3, 4, 3, false) → false      false      OK         
inOrderEqual(3, 4, 4, false) → false      false      OK         
inOrderEqual(3, 4, 3, true) → false      true      X         
inOrderEqual(3, 4, 4, true) → true      true      OK         
inOrderEqual(1, 5, 5, true) → true      true      OK         
inOrderEqual(5, 5, 5, true) → true      true      OK         
inOrderEqual(2, 2, 1, true) → false      true      X         
inOrderEqual(9, 2, 2, true) → false      true      X         
inOrderEqual(0, 1, 0, true) → false      true      X         
other tests
OK      

please advise
gudii9Author Commented:
new approach diagtram
oderEqual2.png
ozoCommented:
How are you interpreting what equalOk means?

What do you think "equality is allowed" means in distinguishing increasing from strictly increasing?
gudii9Author Commented:
means in distinguishing increasing from strictly increasing?
not 100% sure.

Strictly increasing means 1 < 8< 9 like that
increasing without strictly could be 1<1<3 like that where allows equality??

if you seee my both flow chart approaches which one best
which preferred to come at top number or equalityOk?
ozoCommented:
How would you test for "increasing without strictly"?
gudii9Author Commented:
In line 9, what does (a==b)||(b==c) mean?
What does "equality is allowed' mean as a 'However' modifier of ''strict increasing order?
What does (a<=b)&&(b<=c) mean?

How would you test for "increasing without strictly"?

i see what you meant.


 (a<=b)&&(b<=c)

above checks  "increasing without strictly"
 (a<b)&&(b<c)

above checks  "increasing with strictly"
(a==b)&&(b==c)
above checks  "equality"
gudii9Author Commented:
public boolean inOrderEqual(int a, int b, int c, boolean equalOk) {

if(equalOk &&(a<=b||b<=c||c<=a)){

    // if((b==a)||(c==b)||(c==a)||a<=b||b<=c||c<=a){
     
     return true;
     }
     
     else if(!equalOk &&(a<b & b<c & a<c)){

    // if((b==a)||(c==b)||(c==a)||a<=b||b<=c||c<=a){
     
     return false;
     }
     
     
     
return false;



}    
     




i followed above approach still failing 5

Expected      Run            
inOrderEqual(2, 5, 11, false) → true      false      X         
inOrderEqual(5, 7, 6, false) → false      false      OK         
inOrderEqual(5, 5, 7, true) → true      true      OK         
inOrderEqual(5, 5, 7, false) → false      false      OK         
inOrderEqual(2, 5, 4, false) → false      false      OK         
inOrderEqual(3, 4, 3, false) → false      false      OK         
inOrderEqual(3, 4, 4, false) → false      false      OK         
inOrderEqual(3, 4, 3, true) → false      true      X         
inOrderEqual(3, 4, 4, true) → true      true      OK         
inOrderEqual(1, 5, 5, true) → true      true      OK         
inOrderEqual(5, 5, 5, true) → true      true      OK         
inOrderEqual(2, 2, 1, true) → false      true      X         
inOrderEqual(9, 2, 2, true) → false      true      X         
inOrderEqual(0, 1, 0, true) → false      true      X         
gudii9Author Commented:
public boolean inOrderEqual(int a, int b, int c, boolean equalOk) {

boolean x1=(a<=b);
boolean x2=(b<=c);
boolean x3=(a<=c);

if(equalOk){

    // if((b==a)||(c==b)||(c==a)||a<=b||b<=c||c<=a){
     
     return (a<=b||b<=c||c<=a);
     }
     
   //  else if(!equalOk &&((a<b) && (b<c) && (a<c) )){

    // if((b==a)||(c==b)||(c==a)||a<=b||b<=c||c<=a){
     
    // return false;
    // }
   else if(!equalOk){

    // if((b==a)||(c==b)||(c==a)||a<=b||b<=c||c<=a){
     
     return (a==b||b==c||c==a);
     }
     
     
     
return false;



}     
     

Open in new window



above also failing some test cases
gudii9Author Commented:
Expected      Run            
inOrderEqual(2, 5, 11, false) → true      false      X         
inOrderEqual(5, 7, 6, false) → false      false      OK         
inOrderEqual(5, 5, 7, true) → true      true      OK         
inOrderEqual(5, 5, 7, false) → false      true      X         
inOrderEqual(2, 5, 4, false) → false      false      OK         
inOrderEqual(3, 4, 3, false) → false      true      X         
inOrderEqual(3, 4, 4, false) → false      true      X         
inOrderEqual(3, 4, 3, true) → false      true      X         
inOrderEqual(3, 4, 4, true) → true      true      OK         
inOrderEqual(1, 5, 5, true) → true      true      OK         
inOrderEqual(5, 5, 5, true) → true      true      OK         
inOrderEqual(2, 2, 1, true) → false      true      X         
inOrderEqual(9, 2, 2, true) → false      true      X         
inOrderEqual(0, 1, 0, true) → false      true      X         
other tests
X         

not sure where i am struck and how to resolve
gudii9Author Commented:
return equalOk?a<=b && b<=c:a<b && b<c;


above is clear now after trying all those combinations.


public boolean inOrderEqual(int a, int b, int c, boolean equalOk) {
  if( equalOk ){ b+=1; c+=2; }
  return a<b && b<c;
}

i was not clear on above still. why we are adding 1 to b and 2 to c? then checking only 'strictly increasing' not 'loosely increasing' for sake of better work i used loosely for 'not strictly'
gudii9Author Commented:
public boolean inOrderEqual(int a, int b, int c, boolean equalOk) {
if(equalOk){
return (a<=b && b<=c && a<=c );
}
else
return (a<b && b<c && a<c );
}     
     

Open in new window


above also passed all tests?
so a<=c is redundant since we are checking a<=b and b<=a??
gudii9Author Commented:
i modified my daigram as attached--oderEqual3.png .
How to know whatr to put at top level of flow chart as i posted in previous posts

oderEqual1.png or
oderEqual2.png or
oderEqual3.png or
oderEqual3.png
gudii9Author Commented:
please advise
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.