为什么80%的码农都做不了架构师?>>>
1.面试题
例如: ip计算后的值=53文本内容:1,100,北京
101,1000,上海
1001,3001,广州
...求ip=53对应的省份
2.代码如下:
#!/usr/bin/python
# coding: utf8def ip_find(ip):f_content="""1,100,北京\n101,1000,上海\n1001,3000,深圳\n3001,10000,广州\n10001,20001,杭州"""# print f_content.strip()dict1={}for i in f_content.split('\n'):l1= i.split(',')dict1[int(l1[0])] = (int(l1[1]), l1[2])# print dict1# print dict1[1][1]start,end = 0, len(dict1)sort(start, end, dict1)def sort(start, end, dict1):if ip>=sorted([i for i in dict1.keys()])[-1]:print dict1[sorted([ i for i in dict1.keys()])[-1]][1]return len_d = (start+end)/2# print 'len_d=%s' % len_d# print sorted([i for i in dict1.keys()])ip_num_1=sorted([i for i in dict1.keys()])[len_d]ip_num_2=sorted([i for i in dict1.keys()])[len_d+1]# print "ip_num_1 %s, ip_num_2 %s" % (ip_num_1, ip_num_2)if ip_num_1 <= ip < ip_num_2 :print dict1[ip_num_1][1]elif ip<ip_num_1:sort(start, len_d, dict1)elif ip >= ip_num_2:sort(len_d, end, dict1)ip=101
ip_find(ip)
结果:
上海
[Finished in 0.0s]