gudii9
asked on
answerCell java challenge
Hi,
I am trying below challenge
I tried as below
I am getting below result
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
I am trying below challenge
http://codingbat.com/prob/p110973
I tried as below
public boolean answerCell(boolean isMorning, boolean isMom, boolean isAsleep) {
if(isAsleep==true){
return false;
}
else if(isMorning==false){
return true;
}
return false;
}
I am getting below result
Expected Run
answerCell(false, false, false) → true true OK
answerCell(false, false, true) → false false OK
answerCell(true, false, false) → false false OK
answerCell(true, true, false) → true false X
answerCell(false, true, false) → true true OK
answerCell(true, true, true) → false false OK
other tests
OK
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Similar to krakatoa, my one line solution is
return isAsleep ? false : isMorning ? isMom : true;
Similar to krakatoaBut sleeker. ;)
On the Venn - to anyone who wants to comment - there are 8 possible "sections" to resolve. Now I haven't given it a massive amount of thought, but I'm wondering to what extent a Venn can really help in designing an algorithm or method logic . . . except perhaps for graphically confirming that all cases are covered. But it doesn't look as if it makes a problem easier to resolve.
btw another one : return !isAsleep&&!isMorning||isM
isMorning ? isMom : true
reduces to
!isMorning || isMom
and
isAsleep ? false : !isMorning || isMom
reduces to
!isAsleep && (!isMorning || isMom)
also,
!isMorning?true:false
reduces to
!isMorning
and
(isMorning&&isMom?true:!is Morning)
reduces to
(isMorning&&isMom||!isMorn ing)
which reduces to
(!isMorningis||isMom)
reduces to
!isMorning || isMom
and
isAsleep ? false : !isMorning || isMom
reduces to
!isAsleep && (!isMorning || isMom)
also,
!isMorning?true:false
reduces to
!isMorning
and
(isMorning&&isMom?true:!is
reduces to
(isMorning&&isMom||!isMorn
which reduces to
(!isMorningis||isMom)
In general,
(boolean expression)?true:false
is the same as just
(boolean expression)
!isAsleep&&!isMorning||isM om&&!isAsl eep?true:f alse;
is the same as
!isAsleep&&!isMorning||isM om&&!isAsl eep
and
(!isAsleep&&!isMorning)||( isMom&&!is Asleep);
distributes to
!isAsleep&&(!isMorning||is Mom);
(boolean expression)?true:false
is the same as just
(boolean expression)
!isAsleep&&!isMorning||isM
is the same as
!isAsleep&&!isMorning||isM
and
(!isAsleep&&!isMorning)||(
distributes to
!isAsleep&&(!isMorning||is
ASKER
In general,
(boolean expression)?true:false
is the same as just
(boolean expression)
if this is case
then why people tend to write long code like below
public bolean methodA(){
if(condition){
return true;
}
else(condition(){
return false;
}
}
instead wrte as belwo
public bolean methodA(){
return (boolean expression)
}
or as even more short cut to (boolean expression)?true:false
As I've mentioned before, a good way to move toward robust method formulation is to use clearly visible signposts for your logic - in other words to write in such terms as :
if (a==1){ return true;}
else {return false;}
then everyone, including you, can see immediately what the intention is.
But once you have your logic right, you can sometimes take shorter routes, so in the above you could write :
return a==1;
The Ternary operator is just an inline 'if' statement really, which, like 'if', can also be nested.
if (a==1){ return true;}
else {return false;}
then everyone, including you, can see immediately what the intention is.
But once you have your logic right, you can sometimes take shorter routes, so in the above you could write :
return a==1;
The Ternary operator is just an inline 'if' statement really, which, like 'if', can also be nested.
I don't know.
The unnecessary clutter seems to make it harder to see what is important.
But in other circumstances, if the statements inside the conditions have side effects,
then it possible that the long code could add clarity.
If there is a possibility that the code could have to be modified to require such statements with side effects,
then it may be useful for the code structure to reflect that, making it easier to imagine the possible variations.
If the code had originally evolved from other code in which it would have made sense to have a lot of extra statements, then the extraneous structure could be just a historical remnant.
In some cases it may be informative to leave remnants of history, or not worth cleaning it up.
But if the history is not important, it could be a distraction to the reader who may be left wondering what other things you might have been thinking of and what you thought might be useful to do with the the extra structure.
The unnecessary clutter seems to make it harder to see what is important.
But in other circumstances, if the statements inside the conditions have side effects,
then it possible that the long code could add clarity.
If there is a possibility that the code could have to be modified to require such statements with side effects,
then it may be useful for the code structure to reflect that, making it easier to imagine the possible variations.
If the code had originally evolved from other code in which it would have made sense to have a lot of extra statements, then the extraneous structure could be just a historical remnant.
In some cases it may be informative to leave remnants of history, or not worth cleaning it up.
But if the history is not important, it could be a distraction to the reader who may be left wondering what other things you might have been thinking of and what you thought might be useful to do with the the extra structure.
public boolean answerCell(boolean isMorning, boolean isMom, boolean isAsleep) {
int morn,mum,sleeping,sum;
morn = isMorning?-1:1;
mum = isMom?2:0;
sleeping = isAsleep?-3:0;
sum = morn+mum+sleeping;
return sum>=1;
}
:)
ASKER
public boolean answerCell(boolean isMorning, boolean isMom, boolean isAsleep) {
if(isAsleep){
return false;
}
else if(!isMorning){
return true;
}
else{
if(isMom){
return true;
}
else
return false;
}
}
i wrote as above and passed all tests
ASKER
this kind of flow charts helping me to understand better at this stage than venn or tru false table..not sure if there are any limitations of flow charts?
MomCall.png
MomCall.png
ASKER
return isAsleep?false:isMorning&&isMom?true:!isMorning?true:false;
return isAsleep ? false : isMorning ? isMom : true;
return !isAsleep&&(isMom||!isMorning);
not able to understand clearly above 3 lines?please advise
how do i start constructing above one line solutions beginning my flow chart posted in last post?
ASKER
return isAsleep ? false : isMorning ? isMom : true;
\if Asleet it true then return false which is fine
if Asleep false then check morning true or false
morning true check mom true or false
if mom true return true which is all fine
what happened to mom fase morning false cases? we do not need to write them or is it by default understood by jvm something??
return isAsleep ? false : isMorning ? isMom : true;
// more verbosely:
return
( isAsleep )?(
false
):(
( isMorning )?(
isMom
):(
true
)
)
;
// in other words:
if( isAsleep ){
return false;
}else{
if( isMorning ){
return isMom;
}else{
return true;
}
}
// or more succinctly:
return !isAsleep&&(!isMorning||isMom);
ASKER
return isAsleep ? false : isMorning ? isMom : true;
how to break above to below?
if( isAsleep ){
return false;
}else{
if( isMorning ){
return isMom;
}else{
return true;
}
}
is there is rule or thumb rule for this?
It follows from the definition of the ?: operator
- and an intention style that always uses braces after an if() and cuddles elses at the same indentation level.
- and an intention style that always uses braces after an if() and cuddles elses at the same indentation level.
ASKER
It follows from the definition of the ?: operator
- and an intention style that always uses braces after an if() and cuddles elses at the same indentation level.
i got it
return isAsleep ? false : isMorning ? isMom : true;
how to break above to below?
if( isAsleep ){
return false;
}else{
//return true//or can result boolean still having one other if else
if(){
}
else{
}//can be one other if else??
}
}
how many levels down we can go like this???
We've been over this "depth" and "nesting" question many times, and it's starting to look like we are repeating ourselves.
You can nest if and else and else if as many times as the Java language will allow. Jim cakalic and mccarl already said that your brain would give up before you reached that level of nesting -so why are you worried about it? If you had a problem of that level of if-else ness, you would be implementing it the wrong way by then.
You can nest if and else and else if as many times as the Java language will allow. Jim cakalic and mccarl already said that your brain would give up before you reached that level of nesting -so why are you worried about it? If you had a problem of that level of if-else ness, you would be implementing it the wrong way by then.
ASKER
Not ? But : means we almost reached end of nesting right
You don't reach the end until you have as many : as ?
ASKER
You don't reach the end until you have as many : as ?
i was not aware of this fact.
Does that mean you thought you could have ? without :
ASKER
i thought ? could have more than one : which is wrong.
where i can read all about ternary operator at great great detail.
where i can read all about ternary operator at great great detail.