Check if at least two out of three booleans are true

后端 未结 30 1858
自闭症患者
自闭症患者 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条回答
  •  Happy的楠姐
    2020-11-28 17:33

    In Clojure:

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

    Usage:

    (at-least 2 true false true)
    

提交回复
热议问题