Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Provided with a function callf 2 ( f , p , q ) whose input is a function and its two arguments, and returns the

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 and main (both of which should not be changed), I need 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 in PA2.py), return the truth table for f.
Use the function make_tt_ins(n) function 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 equivalent(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_tautology(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.
My code is incorrect and I need help correcting it as debugging has been rough:
PA3.py
import sys
import PA2
# provided
def callf2(f, p, q):
return f(p,q)
def eval_tt_f2(f):
tt_ins = PA2.make_tt_ins(2)
tt =[]
for i in tt_ins:
row = i +[f(*i)]
tt.append(row)
return tt
# implement this
def equivalent(tt1,tt2):
min_length = min(len(tt1), len(tt2))
for i in range(min_length):
if tt1[i][-1]!= tt2[i][-1]:
return False
return True
# implement this
def is_tautology(tt):
for i in tt:
if not i[-1]:
return False
return True
# 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()
PA2.py
import sys
# p implies q
def implies(p, q):
return not p or q
# not p implies not q
def npIMPnq(p,q):
return p or not(q)
# not q implies not p
def nqIMPnp(p,q):
return q or not(p)
# p if and only if q
# equivalent to: (p implies q) and (q implies p)
def iff(p, q):
return (not(p) or q) and (not(q) or p)
# not ( p and q )
def nand(p, q):
return not(p and q)
# not p and not q
def npANDnq(p,q):
return not(p) and not(q)
# not ( p or q)
def nor(p, q):
return not(p) and not(q)
# not p or not q
def npORnq(p,q):
return not(p) or not(q)
def make_tt_ins(n):
if n ==0:
return [[]]
else:
result =[]
for t in make_tt_ins(n -1):
result.append(t +[True])
result.append(t +[False])
return result
#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])))
Testing eval_tt_f2 with implies
Your output: [[True, True, True]]
Testing eval_tt_f2 with nand
Your output: [[True, True, False]]
So looks like I'm missing one list item and getting if True/False wrong

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 Systems For Advanced Applications 18th International Conference Dasfaa 2013 Wuhan China April 22 25 2013 Proceedings Part 2 Lncs 7826

Authors: Weiyi Meng ,Ling Feng ,Stephane Bressan ,Werner Winiwarter ,Wei Song

2013th Edition

3642374492, 978-3642374494

More Books

Students also viewed these Databases questions