UNB/ CS/ David Bremner/ teaching/ cs3383/ lectures/ 50.0-demos/ unit-prop.py
#!/usr/bin/env python3
def UnitProp(S):
  Q = [c for c in S if len(c)==1]; V=[]
  while len(Q)>0:
    z = Q.pop()[0]; T = [];  V.append(z)
    for C in S:
      C = [j for j in C if j!=-z]
      if len(C)==0: return (False,V)
      if len(C)==1: Q.append(C)
      if not z in C: T.append(C)
    if len(T)==0: return (True,V)
    S = T
  return S

if __name__ == "__main__":
  assert UnitProp([[1],[-1,2],[-1,3,4]]) == [ [3,4] ]