Question

In: Computer Science

Python question Define a function called hash_string_weighted_folding(string_to_hash, modulus) that takes two parameters: a string variable string_to_hash...

Python question

Define a function called hash_string_weighted_folding(string_to_hash, modulus) that takes two parameters: a string variable string_to_hash and an integer called modulus. The function returns an integer that is the hash value of string_to_hash.

This hash function processes the string in blocks of 4 characters (the final block may be shorter depending on the length of string_to_hash) and multiplies each character’s ASCII value by a weight. The weight depends on the position of the character in the block of 4 characters: the ASCII value of the character in the first position is multiplied by 256 to the power of zero (i.e., by 1); the character in the second position has its ASCII value multiplied by 256 to the power of 1 (i.e., by 256); the third character has its ASCII value multiplied by 256 to the power of 2; and the fourth character in each block has its ASCII value multiplied by 256 to the power of 3. All of the resulting weighted values are summed and the function returns the result of this sum modulus the parameter modulus.

Note: You can use the inbuilt Python ord() function to return the ASCII value of a string character.

For example:

Test Result
string_to_hash = "Kea"
print(hash_string_weighted_folding(string_to_hash, 5519))
2959
string_to_hash = "Piwaiwaka"
print(hash_string_weighted_folding(string_to_hash, 6089))
5997
string_to_hash = "Kaki"
print(hash_string_weighted_folding(string_to_hash, 3797))
2339

Solutions

Expert Solution

Firstly the string should be divided into blocks of length 4 and stores these blocks into a list then iterate each block in the list and calculate the weighted sum.

Code:

def hash_string_weighted_folding(string_to_hash, modulus):
n=4 #each block length is 4
blocks=[] #stores all blocks
#if length of sting is a multiple of 4
if len(string_to_hash)%4==0:
for i in range(0, len(string_to_hash), n):
blocks.append(string_to_hash[i:i+n])
#length of a string is not a multiple of 4
else:
d=len(string_to_hash)%4
i=0
if len(string_to_hash)>4:
for i in range(0, len(string_to_hash)-d, n):
blocks.append(string_to_hash[i:i+n])
blocks.append(string_to_hash[i+n:])
else:
blocks.append(string_to_hash)
total=0
#calculate the sweighted sum of the chars
for k in blocks:
for m in range(len(k)):
total+=(ord(k[m])*256**m)
return total%modulus

#driver code
#to test the above function
string_to_hash = "Kea"
print(hash_string_weighted_folding(string_to_hash, 5519))
string_to_hash = "Piwaiwaka"
print(hash_string_weighted_folding(string_to_hash, 6089))
string_to_hash = "Kaki"
print(hash_string_weighted_folding(string_to_hash, 3797))

Output:

2959
5997
2339

Please refer to the screenshots below for correct indentations


Related Solutions

This is an intro to python question. #Write a function called search_for_string() that takes two #parameters,...
This is an intro to python question. #Write a function called search_for_string() that takes two #parameters, a list of strings, and a string. This function #should return a list of all the indices at which the #string is found within the list. # #You may assume that you do not need to search inside the #items in the list; for examples: # # search_for_string(["bob", "burgers", "tina", "bob"], "bob") # -> [0,3] # search_for_string(["bob", "burgers", "tina", "bob"], "bae") # -> []...
Python question Define a function called selection_order(items, interval) which takes two parameters: items is a list...
Python question Define a function called selection_order(items, interval) which takes two parameters: items is a list of elements and interval is an integer larger than 0. Imagine that the elements in items were arranged in a circle. Including the first element, count off the elements in items up to the interval position and then remove that element from the circle. From that position, begin counting the positions of the elements again and remove the element that has the next interval...
Write a function in c++, called afterAll that takes two parameters, a vector of string and...
Write a function in c++, called afterAll that takes two parameters, a vector of string and a string. The function returns true if the 2nd parameter comes after all of the strings in the vector, order-wise, false if not. As an example, "zoo" comes after "yuzu".
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the...
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use at...
Sovle with python 3.8 please. 1, Write a function called same_without_ends that has two string parameters....
Sovle with python 3.8 please. 1, Write a function called same_without_ends that has two string parameters. It should return True if those strings are equal WITHOUT considering the characters on the ends (the beginning character and the last character). It should return False otherwise. For example, "last" and "bask" would be considered equal without considering the characters on the ends. Don't worry about the case where the strings have fewer than three characters. Your function MUST be called same_without_ends. You...
Python question Define a function called max_value_length(tree) that takes a Binary Tree object as a parameter...
Python question Define a function called max_value_length(tree) that takes a Binary Tree object as a parameter and returns an integer that contains the longest character length of all the values in that tree. Use recursion. For example, the character length of value 123 is 3, because there are three digits. The character length of value “Ruru” is 4, because it has four characters. Further, the max_value_length() function would return 4 if the only node values in the tree were 123...
In PYTHON Write an algorithm for a function called removeAll which takes 3 parameters: an array...
In PYTHON Write an algorithm for a function called removeAll which takes 3 parameters: an array of array type, a count of elements in the array, and a value. As with the remove method we discussed in class, elements passed the count of elements are stored as None. This function should remove all occurrences of value and then shift the remaining data down. The last populated element in the array should then be set to None. The function then returns...
python 3 please Define a function voweliest that takes as input a string and returns as...
python 3 please Define a function voweliest that takes as input a string and returns as output a tuple where the string that has the most vowels in it is the first element and the second is the number of vowels in that string. Note: don't worry about ties or capital letters Hint: consider defining and using a separate function that counts the number of vowels in a given string
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT