Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Ethernet Ring Dimensioning by mortonfox
from collections import defaultdict
from math import ceil
ETHERNET = (100, 40, 10, 1, 0.1) # Ethernet bandwidth capacity in Gbps
def path(ring, pair):
i = ring.index(pair[0])
ring1 = ring[i:] + ring[:i]
path1 = ring1[:ring1.index(pair[1])]
i = ring.index(pair[1])
ring2 = ring[i:] + ring[:i]
path2 = ring2[:ring2.index(pair[0])]
return path2 if len(path2) < len(path1) else path1
def checkio(ring, *flows):
bandwidths = defaultdict(int)
for pair, flow in flows:
p = path(ring, pair)
for node in p:
bandwidths[node] += flow
links = [0] * len(ETHERNET)
for bandw in bandwidths.values():
if bandw > ETHERNET[0]:
links[0] += ceil(bandw / ETHERNET[0])
continue
for i in range(len(ETHERNET) - 1, -1, -1):
if ETHERNET[i] >= bandw:
links[i] += 1
break
return links
if __name__ == '__main__':
# These "asserts" are used only for self-checking and not necessary for auto-testing
assert checkio("AEFCBG", ("AC", 5), ("EC", 10), ("AB", 60)) == [2, 2, 1, 0, 0]
assert checkio("ABCDEFGH", ("AD", 4)) == [0, 0, 3, 0, 0]
assert checkio("ABCDEFGH", ("AD", 4), ("EA", 41)) == [4, 0, 3, 0, 0]
Nov. 3, 2017