开发者

Code that return True if only one or two of three params are true

I need a code that return T开发者_运维问答rue if only one or two of three params are true

what is the shortest/best way?


I'm addicted to this question!

bool MyFourthAnswer(bool a, bool b, bool c)
{
   return (a != b) || (b != c);
}


Just check whether at least one of the values is set and not all three values are set:

bool result = (a | b | c) & !(a & b & c);


Here's a fancy way:

bool oneOrTwoTrue = a ? (!b || !c) : (b || c);

If the first bool is set, either of the remaining should be unset. Otherwise, either of the of the remaining should be set.

EDIT- In response to comments: in production code, I would probably go with @AS-CII or @Stuart; it communicates the intent of what is being computed most clearly.


This should do it

return !((a & b & c) || (!a & !b & !c))


Another answer... I like this question...

bool MyThirdAnswer(params bool[] list)
{
   return list.Distinct().Count() == 2;
}


LINQ way:

bool[] params = { true, false, true };
int count = params.Count(a => a);
bool result = count == 2 || count == 1;


Final answer from me... honest!

One question that's occurred to me is whether this is really a situation where 3 bools should be used.

Instead of using 3 bools, it might be more appropriate to use a [Flags] enum - and it might make the code faster, more readable and more usable.

The code for this might be:

[flags]
enum Alarm
{
   None = 0x0,
   Kitchen = 0x1,
   Bathroom = 0x2,
   Bedroom = 0x4,
   All = Kitchen | Bathroom | Bedroom,
}

bool MyFifthAnswer(Alarm alarmState)
{
   switch (alarmState)
   {
       case Alarm.None:
       case Alarm.All:
          return false;
       default:
          return true;
   }
}

Out of interest, what are the 3 bools in the original question?


Just for fun, if true = 1 and false = 0:

return (a + b + c) % 3

And another one, assuming false = 0 and true = any strictly positive integer:

return (a*b + b*c + c*a) > (3*a*b*c)

Why stick to a couple comparisons / boolean operations when you could do 6 multiplications AND make it completely obscure? ;-)


bool MyAnswer(params bool[] list)
{
   var countTrue = list.Where(x => x).Count();
   return countTrue == 1 || countTrue == 2;
}

Edit: after badgering by commenters true == x removed... sorry - this was in a "coding standards" document I had to follow once!


This is such a fun question - I had to try it in a Clojure (a language that I am learning)

(defn one-or-two-args-true? [& args]
      (> 3 (count (filter true? args)) 0))


user=> (one-or-two-args-true? false false false)
false
user=> (one-or-two-args-true? false false true)
true
user=> (one-or-two-args-true? false true true)
true
user=> (one-or-two-args-true? true true true)
false


Since my previous answer was too long, I'll try again:

bool MySecondAnswer(params bool[] list)
{
   return list.GroupBy(x => x).Count() == 2;
}


Put the booleans in a list and then filter using linq:

var options = new List<bool>() { true, true, false };
var trueOptions = options.Where( opt => opt };
var count = trueOptions.Count();

return count == 1 || count == 2;


bool result = !(a && b && c) && (a || b || c)


Good question
My Answer:

return (a||b||c) != (a&&b&&c)


69 characters in bash

x (){ test "$(echo $@|sed 's/ /\n/g'|sort|uniq|grep -c .)" == "2"; }

32 characters in python

def x(a,b,c):return a!=b or b!=c
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜