Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
graphlib.TopologicalSorter solution in Speedy category for Determine the Order by juestr
from graphlib import TopologicalSorter
from itertools import pairwise
class ExtendedTopologicalSorter(TopologicalSorter):
def static_order_depth_first(self):
self.prepare()
stack = []
while self.is_active():
match self.get_ready():
case x, *more: stack.extend(more[::-1])
case _: x = stack.pop()
yield x
self.done(x)
def checkio(data):
ts = ExtendedTopologicalSorter()
dependencies = [b+a for word in data for a, b in pairwise(word) if a != b]
dependencies.extend(word[0] for word in data)
dependencies.sort() # insertion order is tie breaker
for x, *ys in dependencies:
ts.add(x, *ys)
return ''.join(ts.static_order_depth_first())
Dec. 6, 2021
Comments: