Question: Python Truth Tables 4 Functions need to be completed The methods that need to be completed is bold PA1.py is to be worked on Tester.py

Python Truth Tables 4 Functions need to be completed

The methods that need to be completed is bold

PA1.py is to be worked on Tester.py is the code to test with

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

######## 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'\b[a-z]\b', 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): 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

----------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------

Test 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()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!