fork(1) download
  1. class Graph():
  2. def __init__(self):
  3. self.graph = {};
  4. self.all_path_list = [];
  5.  
  6. def add_node(self, node, child):
  7. if ( node not in self.graph ):
  8. self.graph[node] = [];
  9. if ( child not in self.graph ):
  10. self.graph[child] = [];
  11.  
  12. self.graph[node].append(child);
  13.  
  14. def echo(self, init_node, end_node):
  15. self.find_all_path(init_node, end_node, '');
  16.  
  17. return ','.join(self.all_path_list);
  18.  
  19. def find_all_path(self, cur_node, end_node, path):
  20. path += cur_node;
  21.  
  22. if ( cur_node == end_node ):
  23. self.all_path_list.append(path);
  24. return;
  25.  
  26. if ( not self.graph[cur_node] ):
  27. return;
  28.  
  29. for node in self.graph[cur_node]:
  30. self.find_all_path(node, end_node, path);
  31.  
  32.  
  33.  
  34. graph = Graph();
  35.  
  36. stash = '1{2,3}4{5,6}7';
  37.  
  38. sep = ['{', ',', '}'];
  39. init_node = end_node = None;
  40. parents = {};
  41.  
  42. def deploy(i = 0, node = None, deep = 0):
  43. global stash, sep, init_node, end_node, parents;
  44.  
  45. parent, node = node, '';
  46. back = False;
  47.  
  48. while ( i < len(stash) ):
  49. ch = stash[i];
  50.  
  51. if ( ch not in sep ):
  52. node += ch;
  53. i += 1;
  54. continue;
  55.  
  56. elif ( ch == sep[0] ):
  57. if ( init_node is None ):
  58. init_node = node;
  59.  
  60. if ( parent is not None ):
  61. graph.add_node(parent, node);
  62.  
  63. i, parents[deep + 1] = deploy(i + 1, node, deep + 1);
  64.  
  65. back = True;
  66. node = '';
  67.  
  68. elif ( ch == sep[1] ):
  69. if ( init_node is None ):
  70. init_node = node;
  71.  
  72. if back:
  73. for item in parents[deep + 1]:
  74. graph.add_node(item, node);
  75. del parents[deep + 1];
  76. else:
  77. graph.add_node(parent, node);
  78.  
  79. parents[deep] = [];
  80. parents[deep].append(node);
  81.  
  82. node = '';
  83. i += 1;
  84. continue;
  85.  
  86. elif ( ch == sep[2] ):
  87. if back:
  88. for item in parents[deep + 1]:
  89. graph.add_node(item, node);
  90. else:
  91. graph.add_node(parent, node);
  92.  
  93. parents[deep].append(node);
  94.  
  95. return i + 1, parents[deep];
  96.  
  97.  
  98. end_node = node;
  99. for item in parents[deep + 1]:
  100. graph.add_node(item, node);
  101.  
  102.  
  103.  
  104. deploy();
  105.  
  106. out_str = graph.echo(init_node, end_node);
  107. print(out_str); # abcdefgxywer,abcdehxywer,abstuvdwer,abstzxdwer
Success #stdin #stdout 0.02s 27712KB
stdin
Standard input is empty
stdout