题解 | #判断两个IP是否属于同一子网#
判断两个IP是否属于同一子网
https://www.nowcoder.com/practice/34a597ee15eb4fa2b956f4c595f03218
import operator def judge(i, jm=False): all_le_256 = all(map(lambda x: x in range(256), i)) mask_valid = True if jm: bin_mask = "".join([bin(x)[2:].ljust(8, '0') for x in i]) mask_valid = bin_mask.index("0") > bin_mask.rindex("1") return all_le_256 and mask_valid def calc(i, j): return [iv & jv for iv, jv in zip(i, j)] def main(m, i, j): if not all([judge(m, True), judge(i), judge(j)]): return 1 return 0 if operator.eq(calc(m, i), calc(m, j)) else 2 while True: try: mask = list(map(int, input().split("."))) ip1 = list(map(int, input().split("."))) ip2 = list(map(int, input().split("."))) print(main(mask, ip1, ip2)) except: break