2015-01-03 41 views
-5

在這個任務中,你應該實施一些布爾操作:Python的操作

  • 「結合」 表示爲x∧Y,滿足x∧Y = 1,如果X = Y = 1和x∧Y = 0,否則。
  • 如果x = y = 0且x∨y = 1,則表示爲x∨y的「分離」,否則滿足x∨y = 0。
  • 「蘊涵」(材料蘊含)表示爲x→y,可以描述爲x∨y。如果x爲真,那麼x→y的值就是y的值。但是如果x是錯誤的,那麼y的值可以被忽略;然而操作必須返回一些真值,並且只有兩個選擇,所以返回值是需要較少的值,即真實值。
  • 「exclusive」(exclusive或)表示爲x⊕y,可描述爲(x∨y)∧¬(x∧y)。它排除了x和y的可能性。根據算術定義,它是加法mod 2,其中1 + 1 = 0.
  • 「等價」表示爲x≡y並且可以描述爲¬(x⊕y)。只有當x和y具有相同的值時纔是真的。

在這裏你可以看到真值表這些操作:

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 | 
    -------------------------------------- 

現在給你兩個布爾值x和y爲1或0,如前面所述,你將得到一個操作名稱。您應該計算的價值和回報作爲1或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?" 

第一,如果是工作我有一個問題,完成脫節和其他操作!請some1幫助我嗎?

+1

任何比* 「有問題」 *更具體的工作解決方案? – jonrsharpe

+0

是的我不知道我應該怎麼做其餘的操作 –

+0

*「我不知道怎麼做」*不是[主題問題](http://stackoverflow.com/help/on-話題)。你的嘗試在哪裏,究竟是什麼錯誤? – jonrsharpe

回答

3

創建一個字典從名稱映射到操作。使用按位操作作爲操作數是整數值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自己。然而,文本已經爲您提供了一個方便的實施指南:

可謂¬X∨Ÿ

所以拉姆達是:

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

使用1 - a到無法模擬。

完整的解決方案:

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) 
+0

那麼你將如何做獨家和暗示? –

+0

@Martjin Pieters爲什麼析取說無效的語法? –

+0

@ferarimaliferari:因爲逗號缺失,現在添加。 –

0
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?" 

這是該