题解 | #判断两个IP是否属于同一子网#
判断两个IP是否属于同一子网
http://www.nowcoder.com/questionTerminal/34a597ee15eb4fa2b956f4c595f03218
import sys
def check_ip(ip):
try:
list_a = list(map(int, ip.split(".")))
for i in list_a:
if 0 <= i <= 255:
continue
else:
return False
return True
except:
return False
def check_yanma(yanma):
try:
list_a = list(map(int, yanma.split(".")))
for i in list_a:
if 0 <= i <= 255:
continue
else:
return False
yanma_bin = []
for i in list_a:
yanma_bin.append(bin(i)[2:].zfill(8))
yanma_str = "".join(yanma_bin)
index = yanma_str.find("0")
if index == -1:
return True
else:
zero_str = yanma_str[index:]
if "1" in zero_str:
return False
else:
return True
except:
return False
def get_zhujihao(ip, yanma):
try:
list_a = list(map(int, yanma.split(".")))
yanma_bin = []
for i in list_a:
yanma_bin.append(bin(i)[2:].zfill(8))
yanma_str = "".join(yanma_bin)
list_b = list(map(int, ip.split(".")))
ip_bin = []
for i in list_b:
ip_bin.append(bin(i)[2:].zfill(8))
ip_str = "".join(ip_bin)
list_zhujihao = []
for i in range(len(yanma_str)):
list_zhujihao.append(
"1" if yanma_str[i] == "1" and ip_str[i] == "1" else "0"
)
str_zhujihao = "".join(list_zhujihao)
return str_zhujihao
except:
pass
lines = []
while True:
line = sys.stdin.readline().strip()
if line == "":
break
else:
lines.append(line)
# print(lines)
is_yanma = False
is_ip = False
yanma = ""
ip = ""
for i in range(len(lines)):
if i % 3 == 0:
if check_yanma(lines[i]):
is_yanma = True
yanma = lines[i]
else:
is_yanma = False
is_ip = False
elif i % 3 == 1:
if is_yanma and check_ip(lines[i]):
is_ip = True
ip = lines[i]
else:
is_ip = False
else:
if is_ip and check_ip(lines[i]):
if get_zhujihao(ip, yanma) == get_zhujihao(lines[i], yanma):
print(0)
else:
print(2)
else:
print(1)
def check_ip(ip):
try:
list_a = list(map(int, ip.split(".")))
for i in list_a:
if 0 <= i <= 255:
continue
else:
return False
return True
except:
return False
def check_yanma(yanma):
try:
list_a = list(map(int, yanma.split(".")))
for i in list_a:
if 0 <= i <= 255:
continue
else:
return False
yanma_bin = []
for i in list_a:
yanma_bin.append(bin(i)[2:].zfill(8))
yanma_str = "".join(yanma_bin)
index = yanma_str.find("0")
if index == -1:
return True
else:
zero_str = yanma_str[index:]
if "1" in zero_str:
return False
else:
return True
except:
return False
def get_zhujihao(ip, yanma):
try:
list_a = list(map(int, yanma.split(".")))
yanma_bin = []
for i in list_a:
yanma_bin.append(bin(i)[2:].zfill(8))
yanma_str = "".join(yanma_bin)
list_b = list(map(int, ip.split(".")))
ip_bin = []
for i in list_b:
ip_bin.append(bin(i)[2:].zfill(8))
ip_str = "".join(ip_bin)
list_zhujihao = []
for i in range(len(yanma_str)):
list_zhujihao.append(
"1" if yanma_str[i] == "1" and ip_str[i] == "1" else "0"
)
str_zhujihao = "".join(list_zhujihao)
return str_zhujihao
except:
pass
lines = []
while True:
line = sys.stdin.readline().strip()
if line == "":
break
else:
lines.append(line)
# print(lines)
is_yanma = False
is_ip = False
yanma = ""
ip = ""
for i in range(len(lines)):
if i % 3 == 0:
if check_yanma(lines[i]):
is_yanma = True
yanma = lines[i]
else:
is_yanma = False
is_ip = False
elif i % 3 == 1:
if is_yanma and check_ip(lines[i]):
is_ip = True
ip = lines[i]
else:
is_ip = False
else:
if is_ip and check_ip(lines[i]):
if get_zhujihao(ip, yanma) == get_zhujihao(lines[i], yanma):
print(0)
else:
print(2)
else:
print(1)