From ce255fd59ae0709e322c8f9de29f9cabc314175a Mon Sep 17 00:00:00 2001 From: bnMikheili Date: Thu, 11 Jun 2020 22:16:35 +0400 Subject: [PATCH 1/3] implement sdbm hash algorithm --- hashes/sdbm.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 hashes/sdbm.py diff --git a/hashes/sdbm.py b/hashes/sdbm.py new file mode 100644 index 000000000000..7bbb864622cb --- /dev/null +++ b/hashes/sdbm.py @@ -0,0 +1,31 @@ +""" + This algorithm was created for sdbm (a public-domain reimplementation of ndbm) database library. + It was found to do well in scrambling bits, causing better distribution of the keys and fewer splits. + It also happens to be a good general hashing function with good distribution. + The actual function (pseudo code) is: + for i in i..len(str): + hash(i) = hash(i - 1) * 65599 + str[i]; + + What is included below is the faster version used in gawk. [there is even a faster, duff-device version] + The magic constant 65599 was picked out of thin air while experimenting with different constants, and turns out to be a prime. + This is one of the algorithms used in berkeley db (see sleepycat) and elsewhere. + + source: http://www.cse.yorku.ca/~oz/hash.html +""" + + +def sdbm(plain_text: str) -> str: + """ + Function implements sdbm hash, easy to use, great for bits scrambling. + iterates over each character in the given string and applies function to each of them. + + >>> sdbm('Algorithms') + 1462174910723540325254304520539387479031000036 + + >>> sdbm('scramble bits') + 730247649148944819640658295400555317318720608290373040936089 + """ + hash = 0 + for plain_chr in plain_text: + hash = ord(plain_chr) + (hash << 6) + (hash << 16) - hash + return hash From fe0174ad5ed0f56ea8451ad4808ff18fc47cda2c Mon Sep 17 00:00:00 2001 From: bnMikheili Date: Thu, 11 Jun 2020 22:26:08 +0400 Subject: [PATCH 2/3] fix bug: styling --- hashes/sdbm.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/hashes/sdbm.py b/hashes/sdbm.py index 7bbb864622cb..f80941306a74 100644 --- a/hashes/sdbm.py +++ b/hashes/sdbm.py @@ -1,5 +1,5 @@ """ - This algorithm was created for sdbm (a public-domain reimplementation of ndbm) database library. + This algorithm was created for sdbm (a public-domain reimplementation of ndbm) database library. It was found to do well in scrambling bits, causing better distribution of the keys and fewer splits. It also happens to be a good general hashing function with good distribution. The actual function (pseudo code) is: @@ -7,7 +7,8 @@ hash(i) = hash(i - 1) * 65599 + str[i]; What is included below is the faster version used in gawk. [there is even a faster, duff-device version] - The magic constant 65599 was picked out of thin air while experimenting with different constants, and turns out to be a prime. + The magic constant 65599 was picked out of thin air while experimenting with different constants. + It turns out to be a prime. This is one of the algorithms used in berkeley db (see sleepycat) and elsewhere. source: http://www.cse.yorku.ca/~oz/hash.html From 0c8591bc5ca86efdce6cfbdf1d4387415d34422f Mon Sep 17 00:00:00 2001 From: bnMikheili Date: Thu, 11 Jun 2020 22:31:26 +0400 Subject: [PATCH 3/3] fix styling for decimal_to_any --- conversions/decimal_to_any.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/conversions/decimal_to_any.py b/conversions/decimal_to_any.py index b91d84b9db98..e3fb4e5d3f08 100644 --- a/conversions/decimal_to_any.py +++ b/conversions/decimal_to_any.py @@ -99,6 +99,8 @@ def decimal_to_any(num: int, base: int) -> str: for base in range(2, 37): for num in range(1000): assert int(decimal_to_any(num, base), base) == num, ( - num, base, decimal_to_any(num, base), - int(decimal_to_any(num, base), base) - ) + num, + base, + decimal_to_any(num, base), + int(decimal_to_any(num, base), base), + )