Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for IP Network: Route Summarization by sawako.oono
import re
def checkio(data):
bits=[]
for d in data:
nums=map(int,d.split("."))
bit=""
for num in nums:
bit+=bin(num)[2:].rjust(8,"0")
bits.append(bit)
for i in range(len(bits[0]),0,-1):
common=bits[0][:i]
found=1
for j in range(1,len(bits)):
if re.match(common,bits[j])==None:
found=0
if found==1:
break
if i==1:
common=""
length=len(common)
common=common.ljust(32,"0")
ans=[]
for i in range(4):
ans.append(str(int(common[8*i:8*i+8],2)))
return ".".join(ans)+"/"+str(length)
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert (checkio(["172.16.12.0", "172.16.13.0", "172.16.14.0", "172.16.15.0"]) == "172.16.12.0/22"), "First Test"
assert (checkio(["172.16.12.0", "172.16.13.0", "172.155.43.9"]) == "172.0.0.0/8"), "Second Test"
assert (checkio(["172.16.12.0", "172.16.13.0", "172.155.43.9", "146.11.2.2"]) == "128.0.0.0/2"), "Third Test"
July 15, 2021