mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
6b6d8cc111
* Adding ELFHash Algorithm Adding a new Hash Algorithm. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update elf.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update elf.py * Update elf.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update elf.py * Apply suggestions from code review Co-authored-by: Caeden <caedenperelliharris@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Caeden <caedenperelliharris@gmail.com>
24 lines
483 B
Python
24 lines
483 B
Python
def elf_hash(data: str) -> int:
|
|
"""
|
|
Implementation of ElfHash Algorithm, a variant of PJW hash function.
|
|
|
|
Returns:
|
|
[int] -- [32 bit binary int]
|
|
>>> elf_hash('lorem ipsum')
|
|
253956621
|
|
"""
|
|
hash = x = 0
|
|
for letter in data:
|
|
hash = (hash << 4) + ord(letter)
|
|
x = hash & 0xF0000000
|
|
if x != 0:
|
|
hash ^= x >> 24
|
|
hash &= ~x
|
|
return hash
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|