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 tom-tom
ETHERNET = (100, 40, 10, 1, 0.1) # Ethernet bandwidth capacity in Gbps
def checkio(ring, *flows):
def path(ring, ingress, egress):
ind = ring.index(ingress)
ring = ring[ind:] + ring[:ind]
return ring[:ring.index(egress) + 1]
def route_indices(ingress, egress):
cw_path = path(ring, ingress, egress)
ccw_path = path(ring[::-1], ingress, egress)
if len(cw_path) <= len(ccw_path):
return (ring.index(c) for c in cw_path[:-1])
else:
return (ring.index(c) for c in ccw_path[:0:-1])
traffic = [0.0] * len(ring)
for nodes, flow in flows:
for i in route_indices(nodes[0], nodes[1]):
traffic[i] += flow
links = [0] * len(ETHERNET)
for flow in traffic:
link = min((e for e in ETHERNET if e >= flow), default= ETHERNET[0])
while flow > 0:
flow -= link
links[ETHERNET.index(link)] += 1
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]
Oct. 31, 2016