मराठी

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

Advertisements
Advertisements

प्रश्न

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.

थोडक्यात उत्तर
Advertisements

उत्तर

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
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
पाठ 6: Searching - Exercise [पृष्ठ ९६]

APPEARS IN

एनसीईआरटी Computer Science [English] Class 12
पाठ 6 Searching
Exercise | Q 8. | पृष्ठ ९६
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×