题解 | #整数与IP地址间的转换#
整数与IP地址间的转换
http://www.nowcoder.com/practice/66ca0e28f90c42a196afd78cc9c496ea
def transfor_ip(ip):
if '.' in ip:
l=ip.split('.')
ll= [bin(int(i)).replace('0b','').rjust(8,'0') for i in l ]
return int(''.join(ll),2)
if ip.isdigit():
bin_string=bin(int(ip)).replace('0b','').rjust(32,'0')
return '.'.join([str(int(bin_string[i:i+8],2)) for i in range(0,32,8)])
import sys
for line in sys.stdin:
ip=line.strip()
res=transfor_ip(ip)
print(res)