Question: 1. Implement logic.py This problem is a gentle introduction to boolean algebra, a branch of algebra in which computations are expressed in terms of True
1. Implement logic.py
This problem is a gentle introduction to boolean algebra, a branch of algebra in which computations are expressed in terms of True and False instead of numbers.
Boolean logic is a key ingredient in all programming languages. Remember that at the most basic level, a computer is a machine that can understand only two states: ON (True) and OFF(False). Therefore, boolean algebra can be used to represent computations.
Similar to addition, subtraction, multiplication and division, boolean algebra has its own set of operations. This problem invites you to explore these operations and implement them. The three most fundamental boolean operations that you will need to use in this assignment are described below:
and: Given two booleans A and B, A and B is True if and only if both A and B are True and False otherwise. The truth table for and is described below for your reference.
| A | B | A and B |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |
or: Given two booleans A and B, A or B is True if either or both of A and B are True and False only when both A and B are False. The truth table for or is described below for your reference.
| A | B | A or B |
|---|---|---|
| True | True | True |
| True | False | True |
| False | True | True |
| False | False | False |
not: Given a boolean A, not A is True if A is False and False if A is True. The truth table for not is described below for your reference.
| A | Not A |
|---|---|
| True | False |
| False | False |
If you revisit Lecture 5, you will notice that Python comes with and, or and not operators. In fact, these are all what you really need for this problem!
There are several other complex boolean operations in boolean algebra. Interestingly, many of those operations can be constructed using and, or and not. In this problem, we provide to you descriptions of certain boolean operations. Your task is to implement the specified boolean operations using any combination of and, or and not, using one or more of these operations.
1a. Implement NAND (1 point)
NAND is an operation that takes as input two booleans A and B and returns False only if both A and B are True. The truth table for NAND is described below for your reference.
| A | B | A nand B |
|---|---|---|
| True | True | False |
| True | False | True |
| False | True | True |
| False | False | True |
Your task is to implement a function with the following signature:
def nand(a:bool, b:bool) -> bool
This function should take as input two booleans a and b and return the value of a nand b.
Example:
nand(True, True) # This should return False nand(False, True) # This should return True
1b. Implement IMPLIES (1 point)
Implication is an operation that takes as input two booleans A and B and returns False only when A is True and B is False and False otherwise. The truth table for IMPLIES is described below for your reference.
| A | B | A implies B |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | True |
| False | False | True |
Your task is to implement a function with the following signature:
def implies(a:bool, b:bool) -> bool
This function should take as input two booleans a and b and return the value of a implies b.
Example:
implies(True, True) # This should return True implies(True, False) # This should return False
1c. Implement IFF (1 point)
If and only if (Iff) is an operation that takes as input two booleans A and B and returns True only when A and B are either both True or both False and False otherwise. The truth table for IFF is described below for your reference.
| A | B | A iff B |
|---|---|---|
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | True |
Your task is to implement a function with the following signature:
def iff(a:bool, b:bool) -> bool
This function should take as input two booleans a and b and return the value of a iff b.
Example:
iff(True, True) # This should return True iff(True, False) # This should return False
1d. Implement XOR (1 point)
Exclusive or (xor) is an operation that takes as input two booleans A and B and returns False when A and B are either both True or both False and True otherwise. The truth table for XOR is described below for your reference.
| A | B | A xor B |
|---|---|---|
| True | True | False |
| True | False | True |
| False | True | True |
| False | False | False |
Your task is to implement a function with the following signature:
def xor(a:bool, b:bool) -> bool
This function should take as input two booleans a and b and return the value of a xor b.
Example:
xor(True, True) # This should return False xor(True, False) # This should return True
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
