Python 操作

Python operations

in this mission you should implement some boolean operations:

  • "conjunction" denoted x ∧ y, satisfies x ∧ y = 1 if x = y = 1 and x ∧ y = 0 otherwise.
  • "disjunction" denoted x ∨ y, satisfies x ∨ y = 0 if x = y = 0 and x ∨ y = 1 otherwise.
  • "implication" (material implication) denoted x→y and can be described as ¬ x ∨ y. If x is true then the value of x → y is taken to be that of y. But if x is false then the value of y can be ignored; however the operation must return some truth value and there are only two choices, so the return value is the one that entails less, namely true.
  • "exclusive" (exclusive or) denoted x ⊕ y and can be described as (x ∨ y)∧ ¬ (x ∧ y). It excludes the possibility of both x and y. Defined in terms of arithmetic it is addition mod 2 where 1 + 1 = 0.
  • "equivalence" denoted x ≡ y and can be described as ¬ (x ⊕ y). It's true just when x and y have the same value.

Here you can see the truth table for these operations:

    x | y | x∧y | x∨y | x→y | x⊕y | x≡y |
    --------------------------------------
    0 | 0 |  0  |  0  |  1  |  0  |  1  |
    1 | 0 |  0  |  1  |  0  |  1  |  0  |
    0 | 1 |  0  |  1  |  1  |  1  |  0  |
    1 | 1 |  1  |  1  |  1  |  0  |  1  |
    --------------------------------------

You are given two boolean values x and y as 1 or 0 and you are given an operation name as described earlier. You should calculate the value and return it as 1 or 0.

到目前为止,这是我的代码:

OPERATION_NAMES = ("conjunction", "disjunction", "implication", "exclusive", "equivalence")

def boolean(x, y, operation):
if (x and y) == 0:
    return 0
elif (x or y) == 1:
    return 1 



if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert boolean(1, 0, u"conjunction") == 0, "and"
    assert boolean(1, 0, u"disjunction") == 1, "or"
    assert boolean(1, 1, u"implication") == 1, "material"
    assert boolean(0, 1, u"exclusive") == 1, "xor"
    assert boolean(0, 1, u"equivalence") == 0, "same?"

第一个 if 正在运行我在完成析取和其他操作时遇到了问题!有人可以帮我吗?

创建一个从名称到操作的 字典 映射。使用 按位 操作,因为您的操作数是整数值 1 和 0:

ops = {
    'conjunction': lambda a, b: a & b,
    'disjunction': lambda a, b: a | b,
    # ... etc.
}
def boolean(a, b, operation):
    return ops[operation](a, b)

operator module 也可以处理合取、析取和排他操作。等价就是相等,所以 operator.eq 可以处理:

import operator

ops = {
    'conjunction': operator.and_,
    'disjunction': operator.or_,
    'exclusive': operator.xor,
    'equivalence': operator.eq,
}

这让您不得不自己实施 implication。然而,文本已经为您提供了一个方便的实施指南:

can be described as ¬ x ∨ y

所以 lambda 将是:

lambda a, b: (1 - a) | b

使用1 - a模拟NOT。

完整的解决方案:

import operator

ops = {
    'conjunction': operator.and_,
    'disjunction': operator.or_,
    'implication': lambda a, b: (1 - a) | b,
    'exclusive': operator.xor,
    'equivalence': operator.eq,
}
def boolean(a, b, operation):
    return ops[operation](a, b)
OPERATION_NAMES = ("conjunction", "disjunction", "implication", "exclusive", "equivalence")
import operator
ops = {
   'conjunction': lambda x, y: x & y,
   'disjunction': lambda x, y: x | y,
   'implication': lambda x, y: (1 - x) | y,
   'exclusive': operator.xor,
   'equivalence': operator.eq,
}
def boolean(x, y, operation):
return ops[operation](x, y)



if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert boolean(1, 0, u"conjunction") == 0, "and"
assert boolean(1, 0, u"disjunction") == 1, "or"
assert boolean(1, 1, u"implication") == 1, "material"
assert boolean(0, 1, u"exclusive") == 1, "xor"
assert boolean(0, 1, u"equivalence") == 0, "same?"

这是解决这个问题的有效方法