Advertisements
Advertisements
Question
Use the hash function: h(element) = element%11 to store the collection of numbers: [44, 121, 55, 33, 110, 77, 22, 66] in a hash table. Display the hash table created. Search if the values 11, 44, 88, and 121 are present in the hash table, and display the search results.
Answer in Brief
Advertisements
Solution
def hashFind(key,hashTable):
if (hashTable[key % 11] == key):
return ((key % 11))
else:
return None
hashTable=[None, None, None, None, None, None, None, None, None, None]
print("We have created a hashTable of 10 positions:")
print(hashTable)
L = [44, 121, 55,33, 110, 77, 22, 66]
print("The given list is", L[::] )
for i in range(0,len(L)):
hashTable[L[i]%10] = L[i]
print("The hash table contents are: " )
for i in range(0,len(hashTable)):
print("hashindex=", i," , value =", hashTable[i])
key = int(input("Enter the number to be searched:"))
position = hashFind(key,hashTable)
if the position is None:
print("Number", key, "is not present in the hash table")
else:
print("Number ",key," present at ",position, " position")
11, and 88 are not present but 44 and 121 are present at index 4 and 1 respectively.
shaalaa.com
Search by Hashing in Python
Is there an error in this question or solution?
