geohash编码
- 编辑时间: 2020-05-10 00:02:43
- 浏览量: 1601
- 作者: makerroot
- 文章分类: 算法
- 评论数: 1
geohash编码题目
geohash编码:geohash常用于将二维的经纬度转换为字符串,分为两步:第一步是经纬度的二进制编码,第二步是base32转码。 此题考察纬度的二进制编码:算法对纬度[-90, 90]通过二分法进行无限逼近(取决于所需精度,本题精度为6)。 注意,本题进行二分法逼近过程中只采用向下取整来进行二分,针对二分中间值属于右区间。算法举例如下: 针对纬度为80进行二进制编码过程:
1) 区间[-90, 90]进行二分为[-90, 0),[0, 90],成为左右区间,可以确定80为右区间,标记为1;
2) 针对上一步的右区间[0, 90]进行二分为[0, 45),[45, 90],可以确定80是右区间,标记为1;
3) 针对[45, 90]进行二分为[45, 67),[67,90],可以确定80为右区间,标记为1;
4) 针对[67,90]进行二分为[67, 78),[78,90],可以确定80为右区间,标记为1;
5) 针对[78, 90]进行二分为[78, 84),[84, 90],可以确定80为左区间,标记为0;
6) 针对[78, 84)进行二分为[78, 81), [81, 84),可以确定80为左区间,标记为0;
我做的答案(使用递归调用的思想)
def test(args, start, end): temp = (start + end) / 2 left = [start, temp] right = [temp, end] if args > temp: result_list.append(1) if len(result_list) < 6: test(args, right[0], right[1]) else: return result_list else: result_list.append(0) if len(result_list) < 6: test(args, left[0], left[1]) else: return result_list return ''.join([str(x) for x in result_list]) if __name__ == '__main__': result_list = [] start = -90 end = 90 print(test(80, start, end))
提交评论