import itertools
def f(x,y,z,w):
return ((x and y) or (y and z)) == ((not(x) or w) and (not(w) or z))
for p in itertools.permutations("xyzw"):
ans = []
t = [(0,1,1,1), (0,1,0,0), (0,1,0,1)]
for r in t:
#ans.append(f(*r))
#ans.append(f(**dict(zip(p,r))))
if ans == [1,1,1]:
print(*p)
break
If I uncomment ans.append(f(*r)) it outputs nothing, but if I uncomment ans.append(f(**dict(zip(p,r)))) it outputs my answer. Why does that happen?
I'm expecting the same output, but there are different arrays after dict(zip) values.
The reason you're getting different outputs is because the two function calls are not passing the same values to the arguments of
f():f(*r)passes positional arguments from the array of tuplest, with the first value being assigned tox, the second toy, the third toz, and the fourth tow. So no matter whatpis, you're gettingans = [f(0,1,1,1), f(0,1,0,0), f(0,1,0,1)].f(**dict(zip(p,r)))passes keyword arguments with the expected keysx,y,z,w, but the order in which the values from the array of tuplestare assigned tox,y,z,wis determined by the current permutationp. For example, on the first iteration of the for loop, withp = ('x', 'y', 'z', 'w'), you'll getans = [f(x=0,y=1,z=1,=w=1), ...]equivalent to[f(0,1,1,1), ...]as in the positional argument version above, but on a later iteration, withp = ('y', 'x', 'z', 'w')you'll getans = [f(y=0,x=1,z=1,w=1), ...](note the inverted assignments of values to x and y) which is equivalent toans = [f(1,0,1,1), ...].