fork(2) download
  1. resolve = lambda a, b: [(a[0], b[1])] if a[1] == b[0] else []
  2.  
  3. def isconsistent(inequations):
  4. pending = inequations[:]
  5. done = []
  6. hashes = set()
  7. while pending:
  8. c = pending.pop()
  9. if c[0] == c[1]:
  10. return False
  11. for i in done:
  12. n = resolve(c, i) + resolve(i, c)
  13. for e in n:
  14. h = "%s<%s"%e
  15. if h in hashes:
  16. continue
  17. hashes.add(h)
  18. pending.append(e)
  19. done.append(c)
  20. return True
  21.  
  22. print(isconsistent([("A", "B"),("B", "C"),("C", "D")]))
  23. print(isconsistent([("A", "B"),("B", "C"),("C", "A")]))
  24.  
Success #stdin #stdout 0.03s 9456KB
stdin
Standard input is empty
stdout
True
False