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
[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.
[edit2] I integrated the above inside your original script with a few minor changes and ran it for the following outputs.
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?