How to check if it is a valid netmask?

975 views Asked by At

My code must return a valid netmask. It must check the binary representation whether it is True or False.

In my code there is something wrong. when I check for 1 it says the first is true and the second is false... All 1 must return True and all 0 must return False.

def is_valid_netmask(numberlist):
    ips = numberlist
    result = True
    #for ip in ips:
        #num = int(ip)
        #if not (num >= 0 and num <= 255):
            #result = False
    if len(ips) != 4:
        return False

    if result == False:
        print("not valid")
    else:
        print("valid")
        octet = ips
        octet_bin = [format(int(i), '08b') for i in octet]
        binary_netmask = ("").join(octet_bin)
        print(binary_netmask)

        checking_ones = True
        for symbol in binary_netmask:
            print("the current symbol is ", symbol)
            print(f"I only encountered one so far: {checking_ones}")
            if checking_ones and symbol == "0":
                print("so i know that the netmask is not valid")
                return False
            elif symbol == "1":
                checking_ones = False
        print("I'm done so I know the netmask is valid")
        return True

Output

valid
11111111111111111111111100000000
the current symbol is  1
I only encountered one so far: True  #correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #not correct
the current symbol is  1
I only encountered one so far: False #notcorrect

the current symbol is  0
I only encountered one so far: False #correct
the current symbol is  0
I only encountered one so far: False #correct 
the current symbol is  0
I only encountered one so far: False #correct
the current symbol is  0
I only encountered one so far: False #correct
the current symbol is  0
I only encountered one so far: False #correct
the current symbol is  0
I only encountered one so far: False #correct
the current symbol is  0
I only encountered one so far: False #correct
the current symbol is  0
I only encountered one so far: False #correct

I'm done so I know the netmask is valid
True
1

There are 1 answers

3
Patrick On

[edit] My apologies, I got confused in my original answer.

When we start to check the bits, we need the first digit to be 1. Then we can accept as many ones as possible. As soon as we encounter a 0 however, we can only accept zeros for the remaining bits.

accept_zero_only = False
first_bit = True
for symbol in binary_netmask:
    print("the current symbol is ", symbol)
    print("Did I encounter a 0 so far?" accept_zero_only)
    if accept_zero_only and symbol == "1":
        print("so i know that the netmask is not valid")
        return False
    elif symbol == "0":
        print("Encountered a zero! From now on we are only accepting zeros."
        accept_zero_only = True
    if first_bit and symbol == "0":
        print(" First bit should be a 1!")
        return False
    first_bit = False

[edit2] I integrated the above inside your original script with a few minor changes and ran it for the following outputs.

def is_valid_netmask(numberlist):
    ips = numberlist

    if len(ips) != 4:
        print "Input array should contain 4 numbers, was:" , len(ips)
        return False

    for ip in ips:
        num = int(ip)
        if not (num >= 0 and num <= 255):
            print "Array contained number", ip, "which is not in [0,255] range."
            return False

    octet = ips
    octet_bin = [format(int(i), '08b') for i in octet]
    binary_netmask = ("").join(octet_bin)
    print(binary_netmask)

    accept_zero_only = False
    first_bit = True
    for symbol in binary_netmask:
        if accept_zero_only and symbol == "1":
            print("Netmask is not valid, contains alternating 1s and 0s and 1s again")
            return False
        elif symbol == "0":
            accept_zero_only = True
        if first_bit and symbol == "0":
            print("First bit should be a 1, netmask is not valid")
            return False
        first_bit = False
    print("Netmask is valid")
    return True

print is_valid_netmask([255,255,0,0,0])    # False
print is_valid_netmask([256,255,255,255])  # False
print is_valid_netmask([255,0,0,0])        # True
print is_valid_netmask([192,0,0,0])        # True
print is_valid_netmask([0,255,255,255])    # False
print is_valid_netmask([255,0,255,0])      # False

It is possible that I missed an edge case. If the program above does not give you the output you want, can you give us the problematic input in the comments below?