PYTHON
b) Truth-table inputs The function makettins(n) will
create a truth table for all combinations of n Boolean variables. A
truth table is an arrayList of 2**n arrayLists of n Booleans. For
example makettins(2) returns
[[False, False], [False, True], [True, False], [True, True]]
Notice the recursive nature of makettins(n): It consists of a truth-table(n-1), each row prefixed with False followed by the same truth-table(n-1), each row prefixed with True, with a base case for n==1: [[False], [True]]
Two functions are provided: run(f) and main, so that you can test your code.
This can be iterative or recursive.
Find the method to print the truth table below:
1. def print_truth_table(length): 2. no_of_rows = length ** 2 - 1 3. widest_number = len(str(bin(no_of_rows)[2:])) 4. for i in range(no_of_rows): 5. the_binary_number = bin(i)[2:].zfill(widest_number) 6. string = (str(the_binary_number)).replace("0", " False").replace("1", " True") 7. print(list(string.split(" ")[1:]))
Line no 1 is the method definition.
In line 2 we get the number of rows to print.
Line no 3 is to get the width to make proper binary orientation.
Line no 4 to 7 prints the truth table in the form of True and False.
Thanks!
Get Answers For Free
Most questions answered within 1 hours.