It was asked to me in an Interview and I freaked out and answered something. I don't rem tho what I put in. Just wanted to know how you guys would solve it.
Thanks.
Michael Dunn
Ranch Hand
Joined: Jun 09, 2003
Posts: 4632
posted
0
solve what?
look at this block
Arjun Reddy
Ranch Hand
Joined: Nov 10, 2007
Posts: 622
posted
0
Originally posted by Michael Dunn: solve what?
look at this block
Yes I got confused there too. Guess the answer would be like we can not simplify the method and state the thing you said above as the reason.
i would concur, but then again if this was for an interview i would shy away from the ? : as many folks don't like using this construct.
Harvinder Thakur
Ranch Hand
Joined: Jun 10, 2008
Posts: 231
posted
0
Originally posted by Campbell: return fruminous && !uffish && manxome ? BANDERSNATCH : JUBJUB_BIRD;
I think the solution provided by Ritchie will fail for the case: fruminous = TRUE && uffish = FALSE && manxome = FALSE as it would give JUBJUB_BIRD whereas it should give BANDERSNATCH.
I agree with Rob's solution. That's cool Rob. Please tell me. How did you get the solution? How should we approach such questions? :roll: After looking at your solution it seems to be correct. But really i had no clue when i first looked at the question.
thanks
Harvinder
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2778
2
posted
0
[Steve]: i would concur, but then again if this was for an interview i would shy away from the ? : as many folks don't like using this construct.
Whereas many others people prefer it; that can go either way. For an interview I would offer both solutions, and make clear that I was willing to code in either style if there was a local style guide to follow. Left to my own devices though, I'd definitely favor the ternary operator.
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2778
2
posted
0
[Harvinder]: I agree with Rob's solution. That's cool Rob. Please tell me. How did you get the solution? How should we approach such questions? After looking at your solution it seems to be correct. But really i had no clue when i first looked at the question.
Obviously I'm not Rob, but one way to approach this would be to notice that since there are only three boolean inputs, there are only eight possible combinations of values. Make a table and see what happens for each value combination:
Once you complete this, it may well be possible to see a simple pattern in the results which is easy to describe with a formula like what Rob gave.
Harvinder Thakur
Ranch Hand
Joined: Jun 10, 2008
Posts: 231
posted
0
Thanks Mike for the prompt answer. Earlier i had thought that it was some trick question. But after some time i did end up following the approach suggested by you. I wonder if Rob did it that way or not?
I did but with a little different layout I learned during an electro course in University. It uses some form of matrix, but it's kinda hard to explain without the book that I learned it from.
The only advantage that layout adds is the ease of grouping "cells" together. For instance, if every case where frumious is false yields true, it's very easy to see.
It gets hard at 5 or more booleans though.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32651
4
posted
0
I was just having a quick look at the code; I obviously should have done it formally.
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2778
2
posted
0
Rob: I assume you're thinking of a Karnaugh map (or alternately a Veitch diagram, or KV map). Those were fun. [ December 26, 2008: Message edited by: Mike Simmons ]
Those were the ones yes. Funny how the name gets completely forgotten but the technique stays, even after 10 years.
Jeez, I started university 10 years ago already... I'm getting old...
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2778
2
posted
0
Past your Prime?
ramya narayanan
Ranch Hand
Joined: Oct 06, 2008
Posts: 338
posted
0
Rob need a slight clarification. In this code there is a condition :
annuling it with your suggestion
In this case , taking manxome value as false it should return JUBJUB_BIRD But with your suggestion it will return : BANDERSNATCH Have you noticed it. Regards.
But have you checked the values for frumious and uffish?
At that point you already know that frumious is true (the entire first if statement returns JUBJUB_BIRD). You also know that either uffish is true or manxone is true (second if statement).
Given that manxone is false (otherwise you won't get inside that if statement), uffish must be true. And therefore !frumious || uffish yields true and JUBJUB_BIRD is returned.
ramya narayanan
Ranch Hand
Joined: Oct 06, 2008
Posts: 338
posted
0
Great Thinking Rob & now I've understood. Thanks & keep going My Best Regards!
Harvinder Thakur
Ranch Hand
Joined: Jun 10, 2008
Posts: 231
posted
0
thanks Rob and Mark once again for your valuable inputs