Making truthtables in python
boolean, python, python-3.x, truthtable
Solution
For B, you want:
def truthtable(f, values=None):
if values is None:
prints(variables(f) + ["formula"])
values = []
# print values
if len(values) == len(variables(f)):
prints(values + [f(*values)])
else:
for b in [True, False]:
truthtable(f, values + [b])
How this meets your spec:
The function should have a second parameter `values` with a default value of `[]`, which will be the list of values the function builds up and eventually passes to `f`; - not quite, "mutable default parameter" is a bad move in Python, but I have `values` and make it an empty list on the first call to `truthtable`
If the list `values` is empty, the function should print a row containing all the variable names (one column header per variable); - done at the same time as initialising `value`
If the list `values` is the same length as the list of variables of `f`, the function should print a row of values containing all the values in `values`, as well as the result of applying `f` to that list of values (use the `*`-operator to apply `f` to the list of arguments); - the second `if` block
If the list `values` is shorter than the list of variables of `f`, the function should make recursive calls to `truthtable()`, with approprate changes to the arguments of `truthtable()`. - the `for` loop at the end.
For more explanation on the last part; you need to build up combinations of `True` and `False` to pass as arguments to `f`, so you recursively call (i.e. call a function from within itself) `truthtable` first with `True`, then with `False`, each time adding to the list until you have the right number of arguments. Uncomment `print values` to watch this happen in the interpreter.
Problem
I'm having some trouble doing some homework related to making truthtables in Python. I've tried going to Office Hours, but they don't know anything so I gotta ask you guys. Here's the question: -- In this problem, you will implement functions for printing truth tables for formulas with variables. You may use the following helper function, which prints a tab-delimited list of values. ``` def prints(values): print("\t".join([str(value) for value in values])) ``` The above function can be used as follows ``` prints([True, False, True]) True False True ``` You may also use the following helper function, which returns a list of the argument names of a function: ``` def variables(f): return list(f.__code__.co_varnames) ``` The above function can be used as follows: ``` def h(x,y,z): return (y or x) and (not(z) <= x) variables(h) ['x', 'y', 'z'] ``` A: Implement a function truthtableXY(f) that takes as its input a single function f (i.e., a Python function corresponding to a formula such as those you defined in Problem #2 above). You may assume f takes two boolean arguments x and y. The function should print a truth table for f. ``` def f(x,y): return x and y truthtableXY(f) y x formula True True True True False False False True False False False False ``` B: Implement a recursive function truthtable(f) that takes as its first argument a single function f (i.e., a Python function corresponding to a formula). The function f may take any non-zero quantity of arguments. The function should print a truth table for f. ``` def h(x,y,z): return (y or x) and (not(z) <= x) truthtable(h) x y z formula True True True False True True False False True False True False True False False False False True True True False True False False False False True False False False False False ``` Your truthtable() function should employ the recursive backtracking approach, and can be organized as follows: - The function should have a second parameter values with a default value of [], which will be the list of values the function builds up and eventually passes to f; - If the list values is empty, the function should print a row containing all the variable names (one column header per variable); - If the list values is the same length as the list of variables of f, the function should print a row of values containing all the values in values, as well as the result of applying f to that list of values (use the *-operator to apply f to the list of arguments); - If the list values is shorter than the list of variables of f, the function should make recursive calls to truthtable(), with approprate changes to the arguments of truthtable(). C: Implement a function rows(f) that takes as its first argument a single function f (i.e., a Python function corresponding to a formula). The function should return the number of rows in the truth table for f. ``` def h(x,y,z): return (y or x) and (not(z) <= x) rows(h) 8 ``` -- I managed to do A, and got this answer: ``` def truthtableXY(f): prints(['y', 'x', 'formula']) for x in [True, False]: for y in [True, False]: prints([x, y, f(x,y)]) ``` which works. But I simply cannot work out how to do the others. Anyone out there who knows/ can work out the answer? Here's the original website with the homework btw: http://cs-people.bu.edu/lapets/131/m.php?#2.4 (question 3) Thanks in advance guys! :)