Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

FILE PA2.py: import sys def implies(p, q): Return the result of the logical operation p -> q. return (not p) or q def npIMPnq(p, q):

image text in transcribedimage text in transcribed

FILE PA2.py:

import sys

def implies(p, q): """Return the result of the logical operation p -> q.""" return (not p) or q

def npIMPnq(p, q): """Return the result of the logical operation ~p -> ~q.""" return (not p) or (not q)

def nqIMPnp(p, q): """Return the result of the logical operation ~q -> ~p.""" return (not q) or (not p)

def iff(p, q): """Return the result of the logical operation p q.""" return p == q

def nand(p, q): """Return the result of the logical operation p NAND q.""" return not (p and q)

def npANDnq(p, q): """Return the result of the logical operation ~p AND ~q.""" return (not p) and (not q)

def nor(p, q): """Return the result of the logical operation p NOR q.""" return not (p or q)

def make_tt_ins(n): """Return a list of all possible inputs for a truth table with n variables.""" if n == 0: return [[]] else: result = [] for t in make_tt_ins(n-1): result.append(t + [True]) result.append(t + [False]) return result

def run(f): """Print the truth table for a given Boolean function f.""" inputs = make_tt_ins(f.__code__.co_argcount) print("{:^{}} | {:^{}}".format("Input", len(inputs[0]), "Output", 4)) print("-" * (len(inputs[0]) + 3) + "|" + "-" * 5) for i in inputs: output = f(*i) print("{:^{}} | {:^{}}".format(str(i), len(inputs[0]), str(output), 4))

def main(): print("Implication") run(implies) print(" Not p IMPLIES not q") run(npIMPnq) print(" Not q IMPLIES not p") run(nqIMPnp) print(" If and only if") run(iff) print(" NAND") run(nand) print(" Not p AND not q") run(npANDnq) print(" NOR") run(nor)

if __name__ == '__main__': main()

''' PA1: Boolean functions and truth-table inputs -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

For PA1 you will implememnt a number of Boolean functions, such as implies, nand, etc.

a) Boolean functions You are provided with a set of undefined functions.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 a truth table for all combinations of n Boolean variables. A truth table is an arrayList of 2**n arrayLists of n Booleans. For example make_tt_ins(2) returns [[False, False], [False, True], [True, False], [True, True]]

Notice the recursive nature of make_tt_ins(n): It consists of a truth-table(n-1), each row prefixed with False followed by the same truth-table(n-1), each row prefixed with True, with a base case for n==1: [[False], [True]]

Two functions are provided: run(f) and main, so that you can test your code.

python3 PA1.py tests your Boolean function python3 PA1.p=y tt tests your function make_tt_ins() '''<><>

import sys

# p implies q def implies(p, q): return False

# not p implies not q def npIMPnq(p,q): return False

# not q implies not p def nqIMPnp(p,q): return False

# p if and only if q: (p implies q) and (q implies p) def iff(p, q): return False

# not ( p and q ) def nand(p, q): return False

# not p and not q def npANDnq(p,q): return False

# not ( p or q) def nor(p, q): return False

# not p or not q def npORnq(p,q): return False

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

FILE PA3.py:

''' PA3: Truth tables and equivalence -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

For PA3 you will be provided with a function callf2(f,p,q) which will cal f(p,q), and with a main to test your code. You will implement two functions: eval_tt_f2(f) and equivalent(tt1,tt2).

The function eval_tt_f2(f) will, given a two argument Boolean function f ( one of the functions you created in PA2.py), return the truth table for f. Use your make_tt_ins(n) function from PA2.py to create an arrayList with n inputs, then use callf2 to append the truth value for f to each row of that arrayList. For example, eval_tt_f2(iff) will return:

[[False, False, True], [False, True, False], [True, False, False], [True, True, True]]

The function equivalent(tt1,tt2) will return True if tt1 and tt2 are equivalent and False otherwise. For example, equivalent(eval_tt_f2(PA2.implies), eval_tt_f2(PA2.nqIMPnp)) will return True.

You will need to add your code from PA2 by selecting PA2.py as the current file above.

'''

import sys import PA2

# provided def callf2(f, p, q): return f(p,q)

# implement this def eval_tt_f2(f): return []

# implement this def equivalent(tt1,tt2): return False

# implement this def is_tautology(tt): return False # use program input section to input choice # ex. entering 'iff' will run code after the label # one arg # and entering 'iff implies' will run code after the labe # one arg and # two args if __name__ == "__main__": print("program", sys.argv[0]) args = input().split() argc = len(args) f1 = args[0] print(f1) tt1 = [] # one arg if(f1 == "implies"): tt1 = eval_tt_f2(PA2.implies) if(f1 == "iff"): tt1 = eval_tt_f2(PA2.iff) if(f1 == "npIMPnq"): tt1 = eval_tt_f2(PA2.npIMPnq) if(f1 == "nqIMPnp"): tt1 = eval_tt_f2(PA2.nqIMPnp) if(f1 == "nand"): tt1 = eval_tt_f2(PA2.nand) if(f1 == "nor"): tt1 = eval_tt_f2(PA2.nor) if(f1 == "npANDnq"): tt1 = eval_tt_f2(PA2.npANDnq) if(f1 == "npORnq"): tt1 = eval_tt_f2(PA2.npORnq) print(tt1)

# two args if(argc>1): f2 = args[1] print(f2) tt2 = [] if(f2 == "implies"): tt2 = eval_tt_f2(PA2.implies) if(f2 == "iff"): tt2 = eval_tt_f2(PA2.iff) if(f2 == "npIMPnq"): tt2 = eval_tt_f2(PA2.npIMPnq) if(f2 == "nqIMPnp"): tt2 = eval_tt_f2(PA2.nqIMPnp) if(f2 == "nand"): tt2 = eval_tt_f2(PA2.nand) if(f2 == "nor"): tt2 = eval_tt_f2(PA2.nor) if(f2 == "npANDnq"): tt2 = eval_tt_f2(PA2.npANDnq) if(f2 == "npORnq"): tt2 = eval_tt_f2(PA2.npORnq) print(tt2) if equivalent(tt1,tt2): print("equivalent!") else: print("NOT equivalent!") print()

IMPORTANT: You will need to use the drop down above the code, below, to select "PA2.py" and paste your PA2 code in that file in order for your PA3 code to work. For this assignment you will be provided with a function callf2 f,p,q) whose input is a function and its two arguments, and returns the value of the function applied to the arguments. You are also given a "main" to test your code. Your task is to implement three functions: eval_tt_f2( f) and equivalent(tt1,tt2) and is_tautology (tt). The function eval_tt_f2 (f) will, given a two argument Boolean function f as input (one of the functions you created in PA2.py), return the truth table for f. Use the function make_tt_ins ( n ) function you implemented in PA2.py to create a list with the inputs for the truth table, and then use callf2 to append the truth value for f to each row. For example, evalttf2(iff) should return: [[False, False, True], [False, True, False], [True, False, False], [True, True, True]] The function equiva lent (tt1,tt2) should return True if tt1 and tt2 are logically equivalent and False otherwise. For example, equivalent(eval_tt_f2(PA2.implies), eval_tt_f2(PA2.nqIMPnp)) should return True. The function is_tauto logy (tt) should return True if the tt parameter has a True value as the output for all possible tt inputs, otherwise it should return false. \begin{tabular}{l|lr} LAB & 5.9.1: Populating Truth Tables with Boolean Functions & 0/100 \end{tabular} Current file: PA3.py PA3: Truth tables and equivalence For PA3 you will be provided with a function callf2(f,p,q) which will cal f(p,q), and with a main to test your code. 7 You will implement two functions: eval_tt_f2(f) and equivalent (tt1,tt2). The function eval_tt_f2(f) will, given a two argument Boolean function f ( one of the functions you created in PA2.py), return the truth table for f. Use your make_tt_ins( n ) function from 13 PA2.py to create an arraylist with n inputs, then use callf2 14 to append the truth value for f to each row of that arrayList. 15 For example, eval_tt_f2(iff) will return: Run your program as often as you'd like, before submitting for grading. Below, type any needed input values in the first box, then click Run program and observe the program's output in the second box. Enter program input (optional) Input(fromabove)Output(shownbelow) Proaram output displaved here Coding trail of your work History of your effort will appear here once you begin working on this zyLab

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 24th International Conference Dexa 2013 Prague Czech Republic August 2013 Proceedings Part 2 Lncs 8056

Authors: Hendrik Decker ,Lenka Lhotska ,Sebastian Link ,Josef Basl ,A Min Tjoa

2013th Edition

3642401724, 978-3642401725

More Books

Students also viewed these Databases questions

Question

i)0,8n0n [,n=00

Answered: 1 week ago