mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-18 01:00:15 +00:00
Adding ELFHash Algorithm (#6731)
* 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>
This commit is contained in:
parent
00fc53de97
commit
6b6d8cc111
23
hashes/elf.py
Normal file
23
hashes/elf.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
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()
|
Loading…
Reference in New Issue
Block a user