fork download
  1. from itertools import permutations, product
  2.  
  3. def implication(a, b):
  4. return not a or b
  5.  
  6. def f1(x, y, z, w):
  7. return int(implication(w, y) == implication(z, x))
  8.  
  9. def f2(x, y, z, w):
  10. return int(implication(w, y) and (not x) == z)
  11.  
  12. # строки фрагмента: 4 столбца, F1, F2 ('?' - пусто)
  13. rows = [
  14. (0, '?', 0, 0, 0, 1),
  15. (0, 0, 0, '?', 0, '?'),
  16. (0, 1, 1, '?', '?', 0),
  17. ]
  18.  
  19. for p in permutations('xyzw'): # p[i] - переменная в (i+1)-м столбце
  20. ok = True
  21. for row in rows:
  22. found = False
  23. for x, y, z, w in product((0, 1), repeat=4):
  24. d = {'x': x, 'y': y, 'z': z, 'w': w}
  25. vals = [d[c] for c in p] + [f1(x, y, z, w), f2(x, y, z, w)]
  26. good = True
  27. for r, v in zip(row, vals):
  28. if r != '?' and r != v:
  29. good = False
  30. if good:
  31. found = True
  32. if not found:
  33. ok = False
  34. if ok:
  35. print(''.join(p))
Success #stdin #stdout 0.02s 9468KB
stdin
Standard input is empty
stdout
yzxw