The approach of utilizing **dict(zip([keys],[values])) for solving a wide range of tasks in the course of Boolean Algebra for students is known in general . This post is just a demo of flexibility of python programming for a hard to complete similar tasks in the way limited by the framework of exclusively truth tables, even obtained with limited Python programming skills
The logical function F is given by the expression:
A partially filled fragment containing non-repeating rows of the
truth table of function F is given. Determine which column of the
truth table corresponds to each of the variables x, y, z, w
In your answer, write the letters w,x,y,z in the order in which the
columns corresponding to them appear ( first the letter corresponding to the first column; then the letter corresponding to the second column, etc.) . Write the letters in your answer in a row; no separators need to be placed between the letters.
Solution of the problem above
Instead of building truth table for x,y,z,w via nested loop :-
print("x y z w")
for x in range(0,2):
for y in range(0,2):
for z in range(0,2):
for w in range(0,2):
if ((w == y) or (((not x) <= z) and ((not z) <= y))) == 0:
print(x, y, z, w)
for x in range(0,2):
for y in range(0,2):
for z in range(0,2):
for w in range(0,2):
if ((w == y) or (((not x) <= z) and ((not z) <= y))) == 0:
print(x, y, z, w)
and attempt to place x,y,z,w as correct headers of original columns of the table above using Boolean reasoning .
We intend to run python module below
─boris ~/DICTZIP02
╰─ ❯❯ cat djs02-dzen.py
def f(x, y, w, z):
# due to (not(x) -> z) is equivalent (x or z)
# due to (not(z) -> y) is equivalent (z or y)
return (w == y) or ((x or z) and (z or y))
from itertools import *
for a1, a2, a3, a4, a5, a6, a7, a8 in product([0, 1], repeat=8):
t = [(a1,1,1,a2), (a3,a4,1,a5),(1,a6,a7,a8)]
if len(set(t)) == 3:
for p in permutations('xywz'):
if [f(**dict(zip(p, r))) for r in t] == [0, 0, 0]:
print(*p, sep='')
and obtain result in a few minutes
─boris~/DICTZIP02
╰─ ❯❯ python djs02-dzen.py
yxwz
No comments:
Post a Comment