# [ACCEPTED]-Code that return True if only one or two of three params are true-programming-languages

Score: 23

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

0

Score: 14

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

``````bool result = (a | b | c) & !(a & b & c);
``````
Score: 10

Here's a fancy way:

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

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

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

Score: 5

This should do it

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

0

Score: 5

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

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

0

Score: 4

LINQ way:

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

0

Score: 4

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

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

The code for this 2 might be:

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

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

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

Score: 3

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

``````return (a + b + c) % 3
``````

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

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

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

Score: 2
``````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 2 - this was in a "coding standards" document 1 I had to follow once!

Score: 2

This is such a fun question - I had to try 1 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
``````
Score: 1

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

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

Put the booleans in a list and then filter 1 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;
``````
Score: 0
``````bool result = !(a && b && c) && (a || b || c)
``````

0

Score: 0

Good question
``````return (a||b||c) != (a&&b&&c)