用Python和awk实现二分法查找

ateacup 2012-02-25

实现根据ip查找出对应的地区code,

对应的查找文本内容格式如下

iparea 22165248 22165503 CN6109
iparea 22165504 22347775 CN6100
iparea 22347776 22413311 CN6101
iparea 22413312 22544383 CN6100
iparea 22544384 23068671 CN1102
iparea 24379392 24641535 CN0000
iparea 27262976 28311551 CN9100
iparea 28573696 28835839 CN1500
iparea 28835840 28966911 CN4401
..............................

areas_arr是保存了上面文本的字典/数组,其key是对应的在文本中的行号

awk实现函数

def search_newarea(areas_arr, ip, s, e):
start=s;
end=e;
ips = ip.split(".")
long_ip=int(ips[0])*256*256*256 + int(ips[1])*256*256 + int(ips[2])*256 + int(ips[3]);
while(start <= end and start >= s and end <= e):
middle=int((start+end)/2);
ip_range = areas_arr[middle].split(",")
if(long_ip>=int(ip_range[0]) and long_ip<=int(ip_range[1])):
return ip_range[2];
if(long_ip>int(ip_range[0])):
start=middle+1;
else:
end=middle-1;
return '';

Python实现代码

def search_newarea(areas_arr, ip, s, e):
start=s;
end=e;
ips = ip.split(".")
long_ip=int(ips[0])*256*256*256 + int(ips[1])*256*256 + int(ips[2])*256 + int(ips[3]);
while(start <= end and start >= s and end <= e):
middle=int((start+end)/2);
ip_range = areas_arr[middle].split(",")
if(long_ip>=int(ip_range[0]) and long_ip<=int(ip_range[1])):
return ip_range[2];
if(long_ip>int(ip_range[0])):
start=middle+1;
else:
end=middle-1;
return '';

相关推荐