def checkio(data):
bins = ["".join(bin(int(x))[2:].zfill(8) for x in line.split(".")) for line in data]
mask = 0
binary = ""
for i, v in enumerate(zip(*bins), 0):
if len(set(v)) > 1:
mask = i
break
else:
binary += v[0]
binary += ("0" * (32 - mask))
return ".".join(str(int(binary[i:i+8], 2)) for i in range(0, 32, 8)) + "/" + str(mask)
from itertools import takewhile, chain
def checkio(adds):
binint = lambda strn: '{:0=8b}'.format(int(strn))
binseqs = (''.join(map(binint, a.split('.'))) for a in adds)
iscommon = lambda binset: len(binset) == 1
prefix = ''.join(chain(*takewhile(iscommon, map(set, zip(*binseqs)))))
filled = '{:0<32}'.format(prefix)
summary = '.'.join(str(int(filled[i: i + 8], 2)) for i in range(0, 32, 8))
return '{}/{}'.format(summary, len(prefix))
Python checkio "IP Network: Route Summarization"解决方案
最新推荐文章于 2024-01-27 10:00:00 发布