compsci-notes-spring-2024/notes/fund-prog-3/chapter-5/programs/mult_str_hashing.py

21 lines
401 B
Python
Raw Normal View History

2024-03-11 17:44:38 -05:00
import sys
initial = int(sys.argv[-4])
multiplier = int(sys.argv[-3])
size = int(sys.argv[-2])
string = sys.argv[-1]
def multiplicative_string_hash(string):
string_hash = initial
hash_multiplier = multiplier
for character in string:
string_hash *= hash_multiplier
string_hash += ord(character)
return string_hash % size
print(multiplicative_string_hash(string))