Skip to content

Instantly share code, notes, and snippets.

@Ivana-
Created June 29, 2020 21:32
Show Gist options
  • Save Ivana-/ef16a5bfc84856e1383d893945db0191 to your computer and use it in GitHub Desktop.
Save Ivana-/ef16a5bfc84856e1383d893945db0191 to your computer and use it in GitHub Desktop.
"""Minimal subnetwork."""
def minSubNetMask(ips):
m = 0
for ip in ips: m |= ips[0] ^ ip
full = (1 << 32) - 1 # 128 for IPv6
return (full << m.bit_length()) & full
def tst(ips, res):
r = minSubNetMask(ips) == res
if not r:
s = "error - min subnet mask of ips:\n"
for ip in ips: s += str(bin(ip)) + "\n"
print(s + "not equals to:\n" + str(bin(res)) + "\n")
return r
res = True
res = res and tst([],
0b11111111111111111111111111111111)
res = res and tst([
0b10010101100111010010011001110111],
0b11111111111111111111111111111111)
res = res and tst([
0b10010101100111010010011001110111,
0b10010101100111010010011001110111],
0b11111111111111111111111111111111)
res = res and tst([
0b10010101100111010010011001110111,
0b10010101100111010010011001110110],
0b11111111111111111111111111111110)
res = res and tst([
0b10010101100111010010011001110111,
0b00010101100111010010011001110111],
0b00000000000000000000000000000000)
res = res and tst([
0b10010101100111010010011001110111,
0b10110101100111010010011001110111],
0b11000000000000000000000000000000)
res = res and tst([
0b10010101100111010010011001110111,
0b10010101100111010010001001110111,
0b10010101100111010010011011100111,
0b10010101100111010010011101100101],
0b11111111111111111111100000000000)
res = res and tst([
0b10010101100111010010011001110111,
0b10010101100111010010001001110111,
0b10010101100111010010011011100111,
0b10010101100111010010011101100101,
0b10010101100110010010011001110111],
0b11111111111110000000000000000000)
print("All tests are passed" if res else "Tests are failed")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment