The program is supposed perform discrete mathematics operations on a number of conjunctive statements (see lines 34 - 50). Currently I just have it so they print out the tables in 0's and 1's, but I'll be adding a compare functionality shortly.
Even though I'm sure this work leaves much to be desired I'm currently worried that about the repetition on lines 78 - 79 as the only unique part is the expression itself while the if else
part isn't or never will be unique.
What can I do to condense?
# Module is created to allow the user to easily select their expression,$
# prints the output and test it to be confident it works.$
import itertools, sys$
$
def setup_table(variables=2):$
''' setup_table prints out all possibilities of a truth_table based on-$
the amount of variables passed to it'''$
return (list(itertools.product(range(2), repeat = variables)))$
$
def printer(truth_table):$
'''printer simply outputs the truth_table in a easier to read way'''$
selection = input('print to the command line? (yes/no): ')$
if selection == 'yes':-$
for table in truth_table:$
print(table)$
else: return truth_table$
--------$
def expression_menu():$
''' expression menu offers the user a selection of expressions to evulate'''$
expression = int(input('''$
choose your expression(s)$
A single expression will print out the truth table$
selecting two expressions will print both and compare:$
$
**************************************************$
Whew look at that list! do you really want to type all that in?$
Type EVALALL in order to compare them all$
$
1. ((p and q) or (p or q)) and not(r or not q)$
2. (p or r) or (q and s)$
3. (p or r) and ( q or (p and s))$
4. not (p and q)$
5. not (p or q)$
6. p -> q$
7. (not p) -> (not q)$
8. p or ( q and r)-$
9. (not p) and (not q)$
10. (not p) or (not q)$
11. (not p) or q$
12. p and (not q)$
13. (p or q) and (p or r)$
----$
BETA (type in your own!)$
----$
**************************************************$
expression: '''))$
#TODO: this is not a good way to do this.., watch your inputs!.$
if expression == 1: variables = 3$
elif expression == 2: variables = 4$
elif expression == 3: variables = 4$
elif expression == 4:$
elif express$
print '''Type in your own is still under contruction'''$
sys.exit()$
--------$
$
return truth(expression,variables)$
$
def truth(expression, variables):$
''' truth evulates the expression selected in expression_menu using$
the table built in setup_table and the boolean logic contained below'''$
truth_table = []$
$
----$
for line in setup_table(variables):$
# all this mess is to get them so their the same type! TODO: type conv$
str_line = ''$
for number in line:$
str_num = str(number)$
str_line += str_num + ' '$
# add your expressions here!$
# TODO: there has to be a way to streamline these expressions, if,else$
if expression == 1:$
if (((line[0] and line[1]) or (line[0] or line[1])) and not (line[2] or not line[1]))>
str_line += '1'$
else:$
str_line += '0'$
$
truth_table.append(str_line)$
elif expression == 2:$
if (line[0] or line[2]) or ( line[1] and line[3]):$
str_line += '1'$
else:$
str_line += '0'$
$
truth_table.append(str_line)$
elif expression == 3:$
if (line[0] or line[2]) and (line[1] or (line[0] and line[3])):$
str_line += '1'$
else:$
str_line += '0'$
elif expressoin == 4:$
$
truth_table.append(str_line)$
# un comment printer to have print to comman line.$
#printer(truth_table)-$
return truth_table$
$
def selection():$
selection = raw_input('choose another expression? (yes/no)')$
if selection == 'yes':$
return expression_menu()$
else: sys.exit()$
---$
if __name__ == '__main__':$
expression_menu()$