Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python Truth tables Finish the 4 boldened method Test code is provided after the

ID: 3743428 • Letter: P

Question

Python Truth tables Finish the 4 boldened method

Test code is provided after the 4 methods

If your implementation is correct, then you will see the following output:

Truth table for the expression: a and b [[True, True, True], [True, False, False], [False, True, False], [False, False, False]]

Number of satisfying values in the expression ‘a and b’ is: 1

The expression ‘a and b’ is NOT a tautology!

Truth table for expression 1: a or b [[True, True, True], [True, False, True], [False, True, True], [False, False, False]]

Truth table for expression 2: x or y [[True, True, True], [True, False, True], [False, True, True], [False, False, False]]

The expressions ‘not a or b’ and ‘a |implies| b’ are equivalent!

# NOTE:
# You must use small single letters for your variable names, for eg. a, b, c
# You may use parenthesis to group your expressions such as 'a and (b or c)'

# Implement the following four functions:
# truth_table, count_satisfying, is_tautology and are_equivalent

# Submission:
# Submit this file using the checkin system on the course web page.

######## Do not modify the following block of code ########
# ********************** BEGIN *******************************

from functools import partial
import re


class Infix(object):
def __init__(self, func):
self.func = func
def __or__(self, other):
return self.func(other)
def __ror__(self, other):
return Infix(partial(self.func, other))
def __call__(self, v1, v2):
return self.func(v1, v2)

@Infix
def implies(p, q) :
return not p or q

@Infix
def iff(p, q) :
return (p |implies| q) and (q |implies| p)


# You must use this function to extract variables
# This function takes an expression as input and returns a sorted list of variables
# Do NOT modify this function
def extract_variables(expression):
sorted_variable_set = sorted(set(re.findall(r'[a-z]', expression)))
return sorted_variable_set

# *********************** END ***************************


############## IMPLEMENT THE FOLLOWING FUNCTIONS ##############
############## Do not modify function definitions ##############


# This function calculates a truth table for a given expression
# input: expression
# output: truth table as a list of lists
# You must use extract_variables function to generate the list of variables from expression
# return a list of lists for this function
def truth_table(expression):
variables = extract_variables(expression)
result = []

pass
  

# count the number of satisfying values
# input: expression
# output: number of satisfying values in the expression
def count_satisfying(expression):
pass

# if the expression is a tautology return True,
# False otherwise
# input: expression
# output: bool
def is_tautology(expression):
pass

# if expr1 is equivalent to expr2 return True,
# False otherwise
# input: expression 1 and expression 2
# output: bool
def are_equivalent(expr1, expr2):
pass

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Tester Code

# NOTE:
# This is a Sample Test file.
# This file is only for your reference.
# Do not submit this file.

import PA1

# This is the function we will use to check the format of your truth table
# Do not modify this function.
def check_format(table, num_vars):

# check whether the table is a list
assert type(table) is list, "table is not a list: %r" % table

# check whether every row in the table is a list
for row in table:
assert type(row) is list, "table is not a list of lists: %r" % table

# check if the table covers all possible combinations
if len(table) < (2**num_vars):
print('Truth table %r does not cover all combinations of variables. There should be %r combinations.' % (table, (2**num_vars)))
return False

# check whether table has extra rows
if len(table) > (2**num_vars):
print('Truth table %r has extra rows. There should be %r combinations.' % (table, (2**num_vars)))
return False

for row in table:
# check if the table has missing columns
if len(row) < (num_vars+1):
print('One or more columns in the table are absent, total number of columns should be: %r' % len(row))
return False
# check if the table has extra columns
elif len(row) > (num_vars+1):
print('Truth table has extra columns, total number of columns should be: %r' % len(row))
return False

return True

# sample test cases for each function
# You should use different expressions to thoroughly test your output
def main():
# sample test for truth_table function
expression = 'a and b'
tt = PA1.truth_table(expression)
variables = PA1.extract_variables(expression)

# check format of the truth table
if check_format(tt, len(variables)):
print(' Truth table for the expression: ' + expression)
print(tt)

# sample test for count_satisfying function
count = PA1.count_satisfying(expression)
# check format of return value
assert type(count) is int, "count_satisfying: return value is not an int" % count
print(' Number of satisfying values in the expression '' + expression + '' is: ' + str(count))

# sample test for is_tautology function
is_a_tautology = PA1.is_tautology(expression)
# check format of return value
assert type(is_a_tautology) is bool, "is_tautology: return value is not a bool" % is_a_tautology
if is_a_tautology:
print(' The expression '' + expression + '' is a tautology!')
else:
print(' The expression '' + expression + '' is NOT a tautology!')

# sample test for are_equivalent function
expr1 = 'not a or b'
expr2 = 'a |implies| b'

tt1 = PA1.truth_table(expr1)
variables1 = PA1.extract_variables(expr1)

if check_format(tt1, len(variables1)):
print(' Truth table for expression 1: ' + expr1)
print(tt1)

tt2 = PA1.truth_table(expr2)
variables2 = PA1.extract_variables(expr2)

if check_format(tt2, len(variables2)):
print(' Truth table for expression 2: ' + expr2)
print(tt2)

are_equivalent_expressions = PA1.are_equivalent(expr1, expr2)
assert type(are_equivalent_expressions) is bool, "are_equivalent: return value is not a bool" % are_equivalent_expressions
if are_equivalent_expressions:
print(' The expressions '' + expr1 + '' & '' + expr2 + '' are equivalent!')
else:
print(' The expressions '' + expr1 + '' & '' + expr2 + '' are NOT equivalent!')

if __name__ == '__main__':
main()

Explanation / Answer

Below is your code

from itertools import product

from functools import partial

import re

class Infix(object):

def __init__(self, func):

self.func = func

def __or__(self, other):

return self.func(other)

def __ror__(self, other):

return Infix(partial(self.func, other))

def __call__(self, v1, v2):

return self.func(v1, v2)

@Infix

def implies(p, q) :

return not p or q

@Infix

def iff(p, q) :

return (p |implies| q) and (q |implies| p)

# You must use this function to extract variables

# This function takes an expression as input and returns a sorted list of variables

# Do NOT modify this function

def extract_variables(expression):

sorted_variable_set = sorted(set(re.findall(r'[a-z]', expression)))

return sorted_variable_set

# *********************** END ***************************



############## IMPLEMENT THE FOLLOWING FUNCTIONS ##############

############## Do not modify function definitions ##############

# This function calculates a truth table for a given expression

# input: expression

# output: truth table as a list of lists

# You must use extract_variables function to generate the list of variables from expression

# return a list of lists for this function

def truth_table(expression):

variables = extract_variables(expression)

result = []

for p in product((True, False), repeat=len(variables)):

for i in range(len(variables)):

exec(str(variables[i])+'='+str(p[i]))

r = list(p)

r.append(str(eval(expression)))

result.append(r)

return result

pass

# count the number of satisfying values

# input: expression

# output: number of satisfying values in the expression

def count_satisfying(expression):

result = truth_table(expression)

count = 0

for i in range(len(result)):

if result[i][len(result[i])-1]==True:

count+=1

return count

pass

# if the expression is a tautology return True,

# False otherwise

# input: expression

# output: bool

def is_tautology(expression):

if count_satisfying==len(truth_table(expression)):

return True

return False

pass

# if expr1 is equivalent to expr2 return True,

# False otherwise

# input: expression 1 and expression 2

# output: bool

def are_equivalent(expr1, expr2):

flag = True;

r1= truth_table(expr1)

r2= truth_table(expr2)

for i in range(len(r1)):

if r1[i][len(r1[i])-1]!=r2[i][len(r2[i])-1]:

flag=False

return flag

pass