Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Counter solution in Clear category for Ethernet Ring Dimensioning by gyahun_dash
from bisect import bisect_left
from collections import Counter
from math import ceil
def checkio(ring, *flows, ETHERNET=(100, 40, 10, 1, 0.1)):
if not flows: return [0] * 5
half = len(ring) // 2
pairs, rates = zip(*flows)
def arrange(indices):
d = abs(indices[1] - indices[0])
return indices if d == half else sorted(indices, reverse=(d > half))
combs = (arrange([ring.index(n) for n in nodes]) for nodes in pairs)
paths = (ring[s: t] if s < t else ring[s:] + ring[: t] for s, t in combs)
flows = (Counter({v: r for v in vs}) for vs, r in zip(paths, rates))
aggrs = sum(flows, Counter()).values()
num, maxr, ascent = len(ETHERNET), ETHERNET[0], ETHERNET[::-1]
links = Counter(bisect_left(ascent, r) for r in aggrs if r <= maxr)
links[num - 1] += sum(ceil(r / maxr) for r in aggrs if r > maxr)
return [links[i] for i in range(num)][::-1]
April 2, 2016