Problem:
Given a number n, return true if n is in the range 1..10, inclusive. Unless "outsideMode" is true, in which case return true if the number is less or equal to 1, or greater or equal to 10.
in1To10(5, false) → true
in1To10(11, false) → false
in1To10(11, true) → true
Solution:
public boolean in1To10(int n, boolean outsideMode) { if (!outsideMode){ if (n >= 1 && n <= 10) { return true; } else { return false; } } else { if (n <= 1 || n >= 10) { return true; } else { return false; } } }
public boolean in1To10(int n, boolean outsideMode) {
ReplyDeleteif(!outsideMode) return n >= 1 && n <= 10 ? true : false;
return n <= 1 || n >= 10 ? true : false;
}
Even less confusing and simpler is:
ReplyDeletepublic boolean in1To10(int n, boolean outsideMode) {
if(outsideMode) return n <= 1 || n >= 10;
return n >= 1 && n <= 10;
}
public boolean in1To10(int n, boolean outsideMode) {
ReplyDeleteif(outsideMode) return n <= 1 || n >= 10;
return n >= 1 && n <= 10;
}
public boolean in1To10(int n, boolean outsideMode) {
ReplyDeletereturn ((!outsideMode&&(n>=1&&n<=10))||(outsideMode&&(n<=1||n>=10)));
}
public boolean in1To10(int n, boolean outsideMode) {
ReplyDeletereturn outsideMode ? n <= 1 || n >= 10 : n >= 1 && n <= 10;
}
public boolean in1To10(int n, boolean outsideMode) {
ReplyDeleteif (!outsideMode)
return n >= 1 && n <= 10;
else
return n <= 1 || n >= 10;
}
//IMDAD MOMIN :-
ReplyDeletereturn (outsideMode? (n>=10 || n<=1) : (n>=1 && n<=10));
public boolean in1To10(int n, boolean outsideMode) {
ReplyDeleteif(outsideMode==true && (n<=1 || n>=10))
return true;
else if(!(outsideMode) && (n>=1 && n<=10))
return true;
else return false;
}
public boolean in1To10(int n, boolean isOutsideMode) {
ReplyDeletereturn (n == 1) != (n == 10) || isOutsideMode != (1 <= n && n <= 10);
}
I don't understand why mine doesn't work:
ReplyDeletepublic boolean in1To10(int n, boolean outsideMode) {
if ((n >= 1) && (n<=10) && (outsideMode == false)){
return true;
}
else if ((n <= 1) || (n >= 10) && (outsideMode == true)) {
return true;
}
return false;
}
if(!outsideMode) return(n >= 1 && n <= 10);
ReplyDeleteelse return(n <= 1 || n >= 10);
public boolean in1To10(int n, boolean outsideMode) {
ReplyDeleteif(!outsideMode) {
if(n>=1 && n<=10) {
return true;
}
}else if(outsideMode){
if(n<=1 || n>=10) {
return true;
}
}
return false;
}
public boolean in1To10(int n, boolean outsideMode) {
ReplyDeletereturn (n>0 && n<=10) && outsideMode==false? true : outsideMode==true && (n<=1 || n>=10) ? true : false;
}