Question: ''' PA2: Boolean functions and truth-table inputs -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- For PA2 you will implememnt a number of Boolean functions, such as implies, nand, etc. a) Boolean

''' PA2: Boolean functions and truth-table inputs -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
For PA2 you will implememnt a number of Boolean functions, such as implies, nand, etc.
a) Boolean functions You are provided with a set of Boolean functions to implement. You will create the bodies for these 2 parameter functions, such that they return the appropriate Boolean value (True or False). The formal parameters are always p and q (in that order)
Notice the difference between npIMPnq and nqIMPnp: In npIMPnq you return not p implies not q (not first param implies not second param), for example npIMPnq(True,False) returns True.
In nqIMPnp you return not q implies not p (not second param implies not first param), for example nqIMPnp(True,False) returns False.
b) Truth-table inputs The function make_tt_ins(n) will create all combinations of n Boolean variables. The output is in the form of a list, each element of which is a list with n Boolean values. For example, make_tt_ins(2) should return [[False, False], [False, True], [True, False], [True, True]]
Two functions are provided: run(f) and main, so that you can test your code.
python3 PA2.py
import sys
# p implies q def implies(p, q): return
# not p implies not q def npIMPnq(p,q): return
# not q implies not p def nqIMPnp(p,q): return
# p if and only if q # equivalent to: (p implies q) and (q implies p) def iff(p, q): return
# not ( p and q ) def nand(p, q): return
# not p and not q def npANDnq(p,q): return
# not ( p or q) def nor(p, q): return
# not p or not q def npORnq(p,q): return
def make_tt_ins(n): return [[]]
#provided def run(f): print(" True,True : ", f(True,True)) print(" True,False : ", f(True,False)) print(" False,True : ", f(False,True)) print(" False,False: ", f(False,False)) print() #provided if __name__ == "__main__": print("program", sys.argv[0]) f1 = sys.argv[1] print(f1) if(f1 == "implies"): run(implies) if(f1 == "iff"): run(iff) if(f1 == "npIMPnq"): run(npIMPnq) if(f1 == "nqIMPnp"): run(nqIMPnp) if(f1 == "nand"): run(nand) if(f1 == "nor"): run(nor) if(f1 == "npANDnq"): run(npANDnq) if(f1 == "npORnq"): run(npORnq) if(f1 == "tt"): print(make_tt_ins(int(sys.argv[2])))
For this assignment you will implement a number of Boolean functions, such as implies, nand, etc. In this assignment you will implement several Boolean functions that will later enable you to compute their truth tables. In addition, you will implement a function that creates all combinations of Boolean values for n variables. a) Boolean functions You are provided with a set of Boolean functions to implement. You will create the bodies for these 2 parameter functions, such that they return the appropriate Boolean value (True or False). The formal parameters are always p and q (in that order). The code provides the specification of the Boolean functions that need to be implemented. b) Truth-table inputs The function make_tt_ins (n ) will create all combination of Boolean values for n variables, which is the input that needs to be provided for a truth table with n variables. For example make_tt_ins (2) needs to return [[False, False], [False, True], [True, False], [True, True]] Note that we do not care about the order, so [[True, True], [False, False], [False, True], [True, False]] is also valid output. Since the input for a truth table has 2n entries, the output for this function is a list with 2n elements, each of which is a list of n Boolean values. We suggest to solve this problem recursively: use the list generated by makettins( n1) and update it accordingly by appending True/False to each row. For the base case ( n==1) you should return [ [False] , [True] ] Two functions are provided: run (f) and main, so that you can test your code. python 3 PA2.py tests your Boolean function python 3 PA2.py tt n tests your function make_tt_ins (n) 4.6.1: Boolean functions and truth-table inputs 0/100
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
