# A will convert these IP addresses to binary format, align them and find the boundary line between the common prefix on the left , and the remaining bits on the right.
# Address 1 10101100 00010000 000011 00 00000000
# Address 2 10101100 00010000 000011 01 00000000
# Address 3 10101100 00010000 000011 10 00000000
# Address 4 10101100 00010000 000011 11 00000000
Здесь и описано всё.
Берёшь свои адреса, конвертишь в бинари.
Получается у тебя список из нескольких "010001..." строк.
pre_mask = ""
mask = 0
mask_founded = False
for bits in zip(bin_reprezentations):
new_bit = "1"
if bits[1:]==bits[:-1] and not founded:
mask += 1
pre_mask+=bits[0]
elif founded:
pre_mask += "0"
elif bits[1:]!=bits[:-1]:
founded = True
pre_mask += "0"
На выходе у тебя mask это цифра которая будет после слеша, а pre_mask - бинарная строчка адреса до слеша.
P.S. Я где-то здесь мог ошибиться, так что не гарантирую работоспособность.
P.P.S. Спасибо
@k4m454k за способ быстрой проверки списка на одинаковость элементов.