From cc6068b42432cd47172e7935770ffab2be0b1a91 Mon Sep 17 00:00:00 2001 From: Louie Tan Date: Fri, 20 Oct 2017 05:19:15 +0800 Subject: [PATCH] Test cases for Rabin-Karp hash --- utilities/python/rabin_karp_hash.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/utilities/python/rabin_karp_hash.py b/utilities/python/rabin_karp_hash.py index 368b26c2..9f0fd105 100644 --- a/utilities/python/rabin_karp_hash.py +++ b/utilities/python/rabin_karp_hash.py @@ -24,3 +24,18 @@ def rk_hash_update(curr_hash, size, add_n, rem_n): add_n: The integer appended to the right rem_n: The integer removed from the left''' return (curr_hash - (rem_n * BASE ** (size - 1))) * BASE + add_n + + + +abc_hash = rk_hash_init(tuple(map(ord, 'abc'))) # Init the hash with 'abc' +print('abc:', abc_hash) +bcd_hash_1 = rk_hash_update(abc_hash, 3, ord('d'), ord('a')) # Add a 'd' to the right, remove an 'a' from the left +print('bcd 1:', bcd_hash_1) + +zbc_hash = rk_hash_init(tuple(map(ord, 'zbc'))) # Init the hash with 'zbc' +print('zbc:', zbc_hash) +bcd_hash_2 = rk_hash_update(zbc_hash, 3, ord('d'), ord('z')) # Add a 'd' to the right, remove a 'z' from the left +print('bcd 2:', bcd_hash_2) + +# Notice that both hash values are the same despite arriving via different paths +print(bcd_hash_1 == bcd_hash_2)