mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Create commutative.py
This commit is contained in:
parent
f11c6bec0c
commit
44cc62bad4
49
boolean_algebra/commutative.py
Normal file
49
boolean_algebra/commutative.py
Normal file
@ -0,0 +1,49 @@
|
||||
def commutative_law_and(a, b):
|
||||
"""
|
||||
Implement the commutative law for AND: A AND B = B AND A.
|
||||
|
||||
Parameters:
|
||||
a (bool): The first boolean value.
|
||||
b (bool): The second boolean value.
|
||||
|
||||
Returns:
|
||||
bool: Result of A AND B.
|
||||
|
||||
>>> commutative_law_and(True, False)
|
||||
False
|
||||
>>> commutative_law_and(False, True)
|
||||
False
|
||||
>>> commutative_law_and(True, True)
|
||||
True
|
||||
>>> commutative_law_and(False, False)
|
||||
False
|
||||
"""
|
||||
return a and b
|
||||
|
||||
def commutative_law_or(a, b):
|
||||
"""
|
||||
Implement the commutative law for OR: A OR B = B OR A.
|
||||
|
||||
Parameters:
|
||||
a (bool): The first boolean value.
|
||||
b (bool): The second boolean value.
|
||||
|
||||
Returns:
|
||||
bool: Result of A OR B.
|
||||
|
||||
>>> commutative_law_or(True, False)
|
||||
True
|
||||
>>> commutative_law_or(False, True)
|
||||
True
|
||||
>>> commutative_law_or(True, True)
|
||||
True
|
||||
>>> commutative_law_or(False, False)
|
||||
False
|
||||
"""
|
||||
return a or b
|
||||
|
||||
# Implement other laws similarly
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
doctest.testmod()
|
Loading…
x
Reference in New Issue
Block a user