Files
hello-algo/ja/codes/python/chapter_hashing/simple_hash.py
Ikko Eltociear Ashimine 954c45864b docs: add Japanese translate documents (#1812)
* docs: add Japanese documents (`ja/docs`)

* docs: add Japanese documents (`ja/codes`)

* docs: add Japanese documents

* Remove pythontutor blocks in ja/

* Add an empty at the end of each markdown file.

* Add the missing figures (use the English version temporarily).

* Add index.md for Japanese version.

* Add index.html for Japanese version.

* Add missing index.assets

* Fix backtracking_algorithm.md for Japanese version.

* Add avatar_eltociear.jpg. Fix image links on the Japanese landing page.

* Add the Japanese banner.

---------

Co-authored-by: krahets <krahets@163.com>
2025-10-17 05:04:43 +08:00

58 lines
1.1 KiB
Python

"""
File: simple_hash.py
Created Time: 2023-06-15
Author: krahets (krahets@163.com)
"""
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:
"""XORハッシュ"""
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__":
key = "Hello algorithm"
hash = add_hash(key)
print(f"加法ハッシュ値は {hash}")
hash = mul_hash(key)
print(f"乗法ハッシュ値は {hash}")
hash = xor_hash(key)
print(f"XORハッシュ値は {hash}")
hash = rot_hash(key)
print(f"回転ハッシュ値は {hash}")