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 hypehr96
def cbin(x):
x=bin(int(x))
x=x[2:]
l=8-len(x)
if(l>0):
app=""
for z in range(l):
app+="0"
x=app+x
return x
def checkio(data):
l = []
for ip in data:
xd = ip.split('.')
wyn = ""
for x in xd:
wyn+=cbin(x)
l.append(wyn)
ile=0
for a in l[0]:
takieSame=True
for w in l:
if(a!=w[ile]):
takieSame=False
if not takieSame:
break;
ile+=1
for s in l:
print(s)
out = l[0][0:ile]
xdd = 32 - ile
if(xdd>0):
app=""
for z in range(xdd):
app+="0"
out=out+app
wynik = str(int(out[0:8],2))+"."+str(int(out[8:16],2))+"."+str(int(out[16:24],2))+"."+str(int(out[24:32],2))+"/"+str(ile)
return wynik
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert (checkio(["192.168.97.0","192.168.100.0"]) == "192.168.96.0/21"), "First Test"
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"
Jan. 10, 2017