Python/hashes/elf.py
sadiqebrahim 94b51f6a91
Added Builtin Voltage (#7850)
* Added Builtin Voltage

* Update builtin_voltage.py

* Update electronics/builtin_voltage.py

Co-authored-by: Caeden Perelli-Harris <caedenperelliharris@gmail.com>

* Update electronics/builtin_voltage.py

Co-authored-by: Caeden Perelli-Harris <caedenperelliharris@gmail.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Apply suggestions from code review

Co-authored-by: Caeden Perelli-Harris <caedenperelliharris@gmail.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Create elf.py

Co-authored-by: Caeden Perelli-Harris <caedenperelliharris@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
2022-10-30 12:52:20 +01:00

22 lines
440 B
Python

def elf_hash(data: str) -> int:
"""
Implementation of ElfHash Algorithm, a variant of PJW hash function.
>>> 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()