验证真值表的最少检查次数

发布于 2024-09-30 12:25:34 字数 208 浏览 4 评论 0原文


我有一个java程序,我想验证3个布尔值中的任何一个是否为假。我想找出可以编写的最小表达式来检查排列。

if(!(needsWork && (needsApproval || isAdmin) ))

我认为这足以确保如果 3 个布尔值中的任何一个为 false,我想停止处理。然而,我偷偷怀疑我错过了一些东西。

I have a java program where I want to validate if any of 3 booleans is false. I want to figure out the smallest expression I can write to check against the permutations.

if(!(needsWork && (needsApproval || isAdmin) ))

I think this is enough to make sure that if any of the 3 booleans is false I want to stop processing. However, I have the sneaking suspicion I am missing something.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷弦 2024-10-07 12:25:35
if(!(needsWork & needsApproval & isAdmin))
if(!(needsWork & needsApproval & isAdmin))
墨小墨 2024-10-07 12:25:34

if (!needsWork || !needsApproval || !isAdmin) 不起作用吗? Java 支持短路评估。

Would if (!needsWork || !needsApproval || !isAdmin) not work? Java supports short-circuit evaluation.

像你 2024-10-07 12:25:34

由于

`any 3 booleans are false` (i.e. `!a || !b || !c`)

`(! (needsWork && (needsApproval || isAdmin))` (i.e. (! (a && ( b || c))`

具有不同的真值表,您确定不同的情况并不重要吗?

a b c   (!a || !b || !c)    (! (a && (b || c)))
T T T          F                    F          
T T F          T                    F
T F T          T                    F
T F F          T                    T
F T T          T                    T
F T F          T                    T
F F T          T                    T
F F F          T                    T

转换

我经常使用布尔表达式来尝试澄清或简化它们,并且我使用这些逻辑转换来帮助我:

// You can push (distribute) `!` into parenthesis if you reverse the `||` or `&&` operator inside:
! (a || b)             <=> (! a && ! b)
! (a || b || c || ...) <=> (! a && ! b && ! c && ...)

! (a && b)             <=> (! a || ! b)
! (a && b && c && ...) <=> (! a || ! b || ! c || ...)

// You can drop parens when the boolean operator outside the parens is the same as inside:
(a || (b || c || ...)) <=> (a || b || c)
(a && (b && c && ...)) <=> (a && b && c)

// You can push (distribute) a boolean op into parenthesis by applying it to each term inside:
(a || (b && c)         <=> ((a || b) && (a || c)
(a || (b && c && ...)  <=> ((a || b) && (a || c) && (a || ...) ...

(a && (b || c)         <=> ((a && b) || (a && c))
(a && (b || c || ...)  <=> ((a && b) || (a && c) || (a || ...) ...

// XOR means the term values have to be different:
(a ^ b)                <=> ((a && !b) || (!a && b))

// XOR means the same as OR unless both terms are true:
(a ^ b)                <=> ((a || b) && ! (a && b))

当然还有很多其他的转换,但这些是我最常使用的。它可能看起来很复杂,但是一旦你开始练习它们就很容易记住它们。

在你的情况下,如果你想看看一些可能的等效语句:

(! (needsWork && (needsApproval || isAdmin) ))

这里有一些转换:

(! (needsWork && (needsApproval || isAdmin) ))   => [push the '!' through the `()`]
(! needsWork || ! (needsApproval || isAdmin) )   => [push the 2nd '!' through the `()`]
(! needsWork || (! needsApproval && ! isAdmin))

但我没有看到你所拥有的任何真正的简化。

当然,如果检查 3 个布尔值中的任何一个为 false 都可以,那么您的选择就很简单

(! needsWork || ! needsApproval || ! isAdmin) => [or pull the `!` outside the `()`]
(! (needsWork  && needsApproval && isAdmin))

Since

`any 3 booleans are false` (i.e. `!a || !b || !c`)

and

`(! (needsWork && (needsApproval || isAdmin))` (i.e. (! (a && ( b || c))`

have different truth tables, are you sure that the cases that are different don't matter?

a b c   (!a || !b || !c)    (! (a && (b || c)))
T T T          F                    F          
T T F          T                    F
T F T          T                    F
T F F          T                    T
F T T          T                    T
F T F          T                    T
F F T          T                    T
F F F          T                    T

Transformations

I will often play around with boolean expressions to try to clarify or simplify them and I use these logic transformations to help me:

// You can push (distribute) `!` into parenthesis if you reverse the `||` or `&&` operator inside:
! (a || b)             <=> (! a && ! b)
! (a || b || c || ...) <=> (! a && ! b && ! c && ...)

! (a && b)             <=> (! a || ! b)
! (a && b && c && ...) <=> (! a || ! b || ! c || ...)

// You can drop parens when the boolean operator outside the parens is the same as inside:
(a || (b || c || ...)) <=> (a || b || c)
(a && (b && c && ...)) <=> (a && b && c)

// You can push (distribute) a boolean op into parenthesis by applying it to each term inside:
(a || (b && c)         <=> ((a || b) && (a || c)
(a || (b && c && ...)  <=> ((a || b) && (a || c) && (a || ...) ...

(a && (b || c)         <=> ((a && b) || (a && c))
(a && (b || c || ...)  <=> ((a && b) || (a && c) || (a || ...) ...

// XOR means the term values have to be different:
(a ^ b)                <=> ((a && !b) || (!a && b))

// XOR means the same as OR unless both terms are true:
(a ^ b)                <=> ((a || b) && ! (a && b))

There are of course many others, but these are ones I use most often. It may look complicated, but they are easy to get to know by heart once you start by practicing them.

In your case, if you wanted to see what some possible equivalent statements for:

(! (needsWork && (needsApproval || isAdmin) ))

here are some transformations:

(! (needsWork && (needsApproval || isAdmin) ))   => [push the '!' through the `()`]
(! needsWork || ! (needsApproval || isAdmin) )   => [push the 2nd '!' through the `()`]
(! needsWork || (! needsApproval && ! isAdmin))

but I don't see any real simplification of what you have.

Of course, if checking that any of 3 booleans are false is fine, then your choices are simple

(! needsWork || ! needsApproval || ! isAdmin) => [or pull the `!` outside the `()`]
(! (needsWork  && needsApproval && isAdmin))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文