Answered step by step
Verified Expert Solution
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 callffpq 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: evalttff and equivalenttttt and istautologytt
The function evalttff will, given a two argument Boolean function f as input one of the functions in PApy return the truth table for f
Use the function makettinsn function implemented in PApy to create a list with the inputs for the truth table, and then use callf to append the truth value for f to each row. For example, evalttfiff should return:
False False, TrueFalse True, FalseTrue False, FalseTrue True, True
The function equivalenttttt should return True if tt and tt are logically equivalent and False otherwise.
For example, equivalentevalttfPAimplies evalttfPAnqIMPnp
should return True.
The function istautologytt 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:
PApy
import sys
import PA
# provided
def callff p q:
return fpq
def evalttff:
ttins PAmakettins
tt
for i in ttins:
row i fi
ttappendrow
return tt
# implement this
def equivalenttttt:
minlength minlentt lentt
for i in rangeminlength:
if tti tti:
return False
return True
# implement this
def istautologytt:
for i in tt:
if not i:
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 namemain:
printprogram sysargv
args inputsplit
argc lenargs
f args
printf
tt
# one arg
iff "implies": tt evalttfPAimplies
iff "iff": tt evalttfPAiff
iff "npIMPnq": tt evalttfPAnpIMPnq
iff "nqIMPnp": tt evalttfPAnqIMPnp
iff "nand": tt evalttfPAnand
iff "nor": tt evalttfPAnor
iff "npANDnq": tt evalttfPAnpANDnq
iff "npORnq": tt evalttfPAnpORnq
printtt
# two args
ifargc:
f args
printf
tt
iff "implies": tt evalttfPAimplies
iff "iff": tt evalttfPAiff
iff "npIMPnq": tt evalttfPAnpIMPnq
iff "nqIMPnp": tt evalttfPAnqIMPnp
iff "nand": tt evalttfPAnand
iff "nor": tt evalttfPAnor
iff "npANDnq": tt evalttfPAnpANDnq
iff "npORnq": tt evalttfPAnpORnq
printtt
if equivalenttttt:
printequivalent
else:
printNOT equivalent!"
print
PApy
import sys
# p implies q
def impliesp q:
return not p or q
# not p implies not q
def npIMPnqpq:
return p or notq
# not q implies not p
def nqIMPnppq:
return q or notp
# p if and only if q
# equivalent to: p implies q and q implies p
def iffp q:
return notp or q and notq or p
# not p and q
def nandp q:
return notp and q
# not p and not q
def npANDnqpq:
return notp and notq
# not p or q
def norp q:
return notp and notq
# not p or not q
def npORnqpq:
return notp or notq
def makettinsn:
if n :
return
else:
result
for t in makettinsn :
result.appendt True
result.appendt False
return result
#provided
def runf:
print True,True : fTrueTrue
print True,False : fTrueFalse
print False,True : fFalseTrue
print False,False: fFalseFalse
print
#provided
if namemain:
printprogram sysargv
f sysargv
printf
iff "implies":
runimplies
iff "iff":
runiff
iff "npIMPnq":
runnpIMPnq
iff "nqIMPnp":
runnqIMPnp
iff "nand":
runnand
iff "nor":
runnor
iff "npANDnq":
runnpANDnq
iff "npORnq":
runnpORnq
ifftt:
printmakettinsintsysargv
Testing evalttf with implies
Your output: True True, True
Testing evalttf with nand
Your output: True True, False
So looks like Im missing one list item and getting if TrueFalse wrong
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started