fork download
  1. res = lambda a, b: [(a[i], b[(i+1)%2]) for i in [0,1] if a[(i+1)%2] == b[i]]
  2.  
  3. def isconsistent(inequations):
  4. pending, done, hashes = [inequations[:], [], set()]
  5. while pending:
  6. p = pending.pop()
  7. if p[0] == p[1]:
  8. return False
  9. for n in (n for d in done for n in res(p, d) if "%s<%s"%n not in hashes):
  10. hashes.add("%s<%s"%n)
  11. pending.append(n)
  12. done.append(p)
  13. return True
  14.  
  15. print(isconsistent([("A", "B"), ("B", "C"), ("C", "D")]))
  16. print(isconsistent([("A", "B"), ("B", "C"), ("C", "A")]))
Success #stdin #stdout 0.04s 9320KB
stdin
Standard input is empty
stdout
True
False