码迷,mamicode.com
首页 > 其他好文 > 详细

hashcode.py

时间:2018-12-10 18:25:23      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:print   span   turn   bsp   int   ==   color   hash   style   

def convert_n_bytes(n, b):
    bits = b * 8
    return (n + 2 ** (bits - 1)) % 2 ** bits - 2 ** (bits - 1)


def convert_4_bytes(n):
    return convert_n_bytes(n, 4)


def getHashCode(s):
    h = 0
    n = len(s)
    for i, c in enumerate(s):
        h = h + ord(c) * 31 ** (n - 1 - i)
    return convert_4_bytes(h)

if __name__=="__main__":
测试一下:
print(getHashCode(123))

 

hashcode.py

标签:print   span   turn   bsp   int   ==   color   hash   style   

原文地址:https://www.cnblogs.com/by170628/p/10096981.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!