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

Python finish the truthtable implementation use extract_variables method to comp

ID: 3743121 • Letter: P

Question

Python finish the truthtable implementation

use extract_variables method to complete

# 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):

Explanation / Answer

Below is the implementation of required funciton

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

----------------------------------------------------------UPDATED-------------------------------------------------------------

Below is the complete code.

added one extra import for product

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

def extract_variables(expression):

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

return sorted_variable_set

def truth_table(expression):

variables = extract_variables(expression)

result = []

inputArray = [(True,False),(True,True),(False,True),(False,False)]

for p in inputArray , 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