fork download
  1. def get_chain(d):
  2. start_nodes = {}
  3. end_nodes = {}
  4. nodes = []
  5. for k, (end, start) in d.items():
  6. node = [k, (start, end), None]
  7. start_nodes[start] = node
  8. end_nodes[end] = node
  9. nodes.append(node)
  10.  
  11. for node in nodes:
  12. start, end = node[1]
  13. if start in end_nodes:
  14. end_nodes[start][2] = node
  15. if end in start_nodes:
  16. node[2] = start_nodes[end]
  17. del start_nodes[end]
  18.  
  19. lst = list(start_nodes.values())[0]
  20. final = [lst[1][0]]
  21. while lst is not None:
  22. final.append(lst[1][1])
  23. lst = lst[2]
  24. return final
  25.  
  26. print(get_chain({
  27. 'A': ['tg', 17],
  28. 'B': [59, 60],
  29. 'C': [60, 61],
  30. 'D': [57, 'tt'],
  31. 'E': [61, 'tg'],
  32. 'F': ['tt', 59]
  33. }))
Success #stdin #stdout 0.07s 14092KB
stdin
Standard input is empty
stdout
[17, 'tg', 61, 60, 59, 'tt', 57]