Check if at least two out of three booleans are true

后端 未结 30 1607
自闭症患者
自闭症患者 2020-11-28 16:47

An interviewer recently asked me this question: given three boolean variables, a, b, and c, return true if at least two out of the three are true.

My solution follow

相关标签:
30条回答
  • 2020-11-28 17:33

    In Clojure:

    (defn at-least [n & bools]
      (>= (count (filter true? bools)) n)
    

    Usage:

    (at-least 2 true false true)
    
    0 讨论(0)
  • 2020-11-28 17:33

    In C:

    return !!a + !!b + !!c >= 2;
    
    0 讨论(0)
  • 2020-11-28 17:35

    Readability should be the goal. Someone who reads the code must understand your intent immediately. So here is my solution.

    int howManyBooleansAreTrue =
          (a ? 1 : 0)
        + (b ? 1 : 0)
        + (c ? 1 : 0);
    
    return howManyBooleansAreTrue >= 2;
    
    0 讨论(0)
  • 2020-11-28 17:41

    Here's a test-driven, general approach. Not as "efficient" as most of the solutions so far offered, but clear, tested, working, and generalized.

    public class CountBooleansTest extends TestCase {
        public void testThreeFalse() throws Exception {
            assertFalse(atLeastTwoOutOfThree(false, false, false));
        }
    
        public void testThreeTrue() throws Exception {
            assertTrue(atLeastTwoOutOfThree(true, true, true));
        }
    
        public void testOnes() throws Exception {
            assertFalse(atLeastTwoOutOfThree(true, false, false));
            assertFalse(atLeastTwoOutOfThree(false, true, false));
            assertFalse(atLeastTwoOutOfThree(false, false, true));
        }
    
        public void testTwos() throws Exception {
            assertTrue(atLeastTwoOutOfThree(false, true, true));
            assertTrue(atLeastTwoOutOfThree(true, false, true));
            assertTrue(atLeastTwoOutOfThree(true, true, false));
        }
    
        private static boolean atLeastTwoOutOfThree(boolean b, boolean c, boolean d) {
            return countBooleans(b, c, d) >= 2;
        }
    
        private static int countBooleans(boolean... bs) {
            int count = 0;
            for (boolean b : bs)
                if (b)
                    count++;
            return count;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 17:44

    This kind of questions can be solved with a Karnaugh Map:

          | C | !C
    ------|---|----
     A  B | 1 | 1 
     A !B | 1 | 0
    !A !B | 0 | 0
    !A  B | 1 | 0
    

    from which you infer that you need a group for first row and two groups for first column, obtaining the optimal solution of polygenelubricants:

    (C && (A || B)) || (A && B)  <---- first row
           ^
           |
       first column without third case
    
    0 讨论(0)
  • 2020-11-28 17:44

    Sum it up. It's called boolean algebra for a reason:

      0 x 0 = 0
      1 x 0 = 0
      1 x 1 = 1
    
      0 + 0 = 0
      1 + 0 = 1
      1 + 1 = 0 (+ carry)
    

    If you look at the truth tables there, you can see that multiplication is boolean and, and simply addition is xor.

    To answer your question:

    return (a + b + c) >= 2
    
    0 讨论(0)
提交回复
热议问题