2023-06-16 21:20:57 +08:00
|
|
|
"""
|
|
|
|
File: simple_hash.py
|
|
|
|
Created Time: 2023-06-15
|
2024-02-07 22:21:18 +08:00
|
|
|
Author: krahets (krahets@163.com)
|
2023-06-16 21:20:57 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def add_hash(key: str) -> int:
|
|
|
|
"""加法哈希"""
|
|
|
|
hash = 0
|
|
|
|
modulus = 1000000007
|
|
|
|
for c in key:
|
|
|
|
hash += ord(c)
|
|
|
|
return hash % modulus
|
|
|
|
|
|
|
|
|
|
|
|
def mul_hash(key: str) -> int:
|
|
|
|
"""乘法哈希"""
|
|
|
|
hash = 0
|
|
|
|
modulus = 1000000007
|
|
|
|
for c in key:
|
|
|
|
hash = 31 * hash + ord(c)
|
|
|
|
return hash % modulus
|
|
|
|
|
|
|
|
|
|
|
|
def xor_hash(key: str) -> int:
|
|
|
|
"""异或哈希"""
|
|
|
|
hash = 0
|
|
|
|
modulus = 1000000007
|
|
|
|
for c in key:
|
|
|
|
hash ^= ord(c)
|
|
|
|
return hash % modulus
|
|
|
|
|
|
|
|
|
|
|
|
def rot_hash(key: str) -> int:
|
|
|
|
"""旋转哈希"""
|
|
|
|
hash = 0
|
|
|
|
modulus = 1000000007
|
|
|
|
for c in key:
|
|
|
|
hash = (hash << 4) ^ (hash >> 28) ^ ord(c)
|
|
|
|
return hash % modulus
|
|
|
|
|
|
|
|
|
|
|
|
"""Driver Code"""
|
|
|
|
if __name__ == "__main__":
|
2023-06-21 19:26:16 +08:00
|
|
|
key = "Hello 算法"
|
|
|
|
|
|
|
|
hash = add_hash(key)
|
2023-06-16 21:20:57 +08:00
|
|
|
print(f"加法哈希值为 {hash}")
|
|
|
|
|
2023-06-21 19:26:16 +08:00
|
|
|
hash = mul_hash(key)
|
2023-06-16 21:20:57 +08:00
|
|
|
print(f"乘法哈希值为 {hash}")
|
|
|
|
|
2023-06-21 19:26:16 +08:00
|
|
|
hash = xor_hash(key)
|
2023-06-16 21:20:57 +08:00
|
|
|
print(f"异或哈希值为 {hash}")
|
|
|
|
|
2023-06-21 19:26:16 +08:00
|
|
|
hash = rot_hash(key)
|
2023-06-16 21:20:57 +08:00
|
|
|
print(f"旋转哈希值为 {hash}")
|