patternpythonMinor
Performing discrete mathematics operations on conjunctive statements
Viewed 0 times
performingoperationsconjunctivestatementsdiscretemathematics
Problem
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
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 o
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 o
Solution
# 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 isn't really helpful in understanding this function. Normally, we'd use create. It might be a good idea to give a longer description as well.
''' 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)))You don't need the parens around this expression
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): ')The command line is where you enter commands, here you are printing to standard output or the terminal
if selection == 'yes':
for table in truth_table:A truth table doesn't contain multiple tables, so this variable shouldn't be called table
print(table)
else: return truth_tableWhy in the world you want to return the truth_table here? A calling function can't really make use of it because it doesn't get returned consistently, and the calling function already has the table anyways
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: '''))For crazy long strings, I recommend putting them global constants so as not the interrupt your flow
#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 expressThis syntax is invalid, I assume it got messed up in the same way your got $ thrown all over it
```
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)
Code Snippets
# 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_tableContext
StackExchange Code Review Q#11783, answer score: 5
Revisions (0)
No revisions yet.