fork download
  1. from collections import deque
  2.  
  3. def well_connections(wells):
  4. common = set.intersection(*({name for *_, name in well} for well in wells))
  5. wells = list(map(deque, wells))
  6. this = 0
  7. while all(wells):
  8. if all(well[0][2] in common for well in wells):
  9. yield tuple(well[0][1] for well in wells)
  10. if len(wells[this]) > 1 and wells[this][1][2] in common:
  11. this = 1 - this
  12. wells[this].popleft()
  13. while wells[this] and wells[this][0][2] not in common:
  14. yield tuple(well[0][1] for well in wells)
  15. wells[this].popleft()
  16. this = 1 - this
  17. wells[this].popleft()
  18.  
  19. wells = [
  20. [
  21. [0, 0.4, 'pop'],
  22. [0.4, 0.8, 'sup'],
  23. [0.8, 1.2, 'muka'],
  24. [1.2, 2, 'sand'],
  25. [2, 2.4, 'buz']
  26. ],
  27. [
  28. [0, 0.4, 'pop'],
  29. [0.4, 1.6, 'sand'],
  30. [1.6, 2, 'graviy'],
  31. [2, 3.2, 'galka'],
  32. [3.2, 3.6, 'buz']
  33. ]
  34. ]
  35. print(list(well_connections(wells)))
Success #stdin #stdout 0.03s 9756KB
stdin
Standard input is empty
stdout
[(0.4, 0.4), (0.8, 0.4), (1.2, 0.4), (2, 1.6), (2, 2), (2, 3.2), (2.4, 3.6)]