Skip to content

Commit ec2d900

Browse files
authored
implement sdbm hash algorithm (TheAlgorithms#2094)
* implement sdbm hash algorithm * fix bug: styling * fix styling for decimal_to_any
1 parent 2264244 commit ec2d900

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

‎conversions/decimal_to_any.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ def decimal_to_any(num: int, base: int) -> str:
9999
for base in range(2, 37):
100100
for num in range(1000):
101101
assert int(decimal_to_any(num, base), base) == num, (
102-
num, base, decimal_to_any(num, base),
103-
int(decimal_to_any(num, base), base)
104-
)
102+
num,
103+
base,
104+
decimal_to_any(num, base),
105+
int(decimal_to_any(num, base), base),
106+
)

‎hashes/sdbm.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
This algorithm was created for sdbm (a public-domain reimplementation of ndbm) database library.
3+
It was found to do well in scrambling bits, causing better distribution of the keys and fewer splits.
4+
It also happens to be a good general hashing function with good distribution.
5+
The actual function (pseudo code) is:
6+
for i in i..len(str):
7+
hash(i) = hash(i - 1) * 65599 + str[i];
8+
9+
What is included below is the faster version used in gawk. [there is even a faster, duff-device version]
10+
The magic constant 65599 was picked out of thin air while experimenting with different constants.
11+
It turns out to be a prime.
12+
This is one of the algorithms used in berkeley db (see sleepycat) and elsewhere.
13+
14+
source: http://www.cse.yorku.ca/~oz/hash.html
15+
"""
16+
17+
18+
def sdbm(plain_text: str) -> str:
19+
"""
20+
Function implements sdbm hash, easy to use, great for bits scrambling.
21+
iterates over each character in the given string and applies function to each of them.
22+
23+
>>> sdbm('Algorithms')
24+
1462174910723540325254304520539387479031000036
25+
26+
>>> sdbm('scramble bits')
27+
730247649148944819640658295400555317318720608290373040936089
28+
"""
29+
hash = 0
30+
for plain_chr in plain_text:
31+
hash = ord(plain_chr) + (hash << 6) + (hash << 16) - hash
32+
return hash

0 commit comments

Comments
 (0)