Question

Python: Write a program that inserts numbers from a file into a quadratic probing hash table...

Python: Write a program that inserts numbers from a file into a quadratic probing hash table and records the amount of collisions that occur after each insertion.

Hash Table size is 191, so 100 random numbers are to be inserted to the table to collect number of collisions.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python:

which are in string format by open("data1.txt") as f:

   lines = f.read().splitlines()
keys = []#keys will store numbers in the file in int format
for line in lines:
   keys.append(int(line))#changing string to int and storing in keys[] list
#HashTable
HashTable = dict()
size = 191#hash table size
for i in range(0,size):#initially store -1 as value for every key in the hashtable
   HashTable[i] = -1;
collisions = []#to record collisions
#for every key in the keys
for key in keys:
   temp = key
   collision = 0#initially collision is zero
   #until we place key in hash table
   while(True):
       #if there is no value in the key
       if HashTable[temp % size] == -1:
           collisions.append(collision)#store collision number for that key
           HashTable[temp % size] = key#store that value
           break
       #if collision occurs
       else:
           collision = collision + 1#increment collision number
           temp = ( temp % size ) + collision * collision#quadratic probing
print "Collisions reacord:"
print collisions

Add a comment
Know the answer?
Add Answer to:
Python: Write a program that inserts numbers from a file into a quadratic probing hash table...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT