python - indexing a dictionary with key hash values -


i going through learn python hard way, , find self stuck on dictionary exercise: http://learnpythonthehardway.org/book/ex39.html

what gives me problems following code:

def hash_key(amap, key):     """given key create number , convert     index amap's buckets."""     return hash(key) % len(amap) 

how can sure wont duplicate values hash_key function? since modulo being used, prevents hash() returning values such after modulo used on them return same hash_key.

ex. len(amap)=10, hash(key1) = 20, hash(key2) = 30, therefore hash_key both dict keys 0, though not equal.

i'm having trouble grasping concepts behind hashing, if have reading material suitable skill level, please share. i'm not afraid work hard.

thank help.

the hashmap, proposed in linked exercise, intends generate key-collisions.

the data structure list of lists, key's hash-modulus value determines 2nd-level list data goes.

imagine structure array of n buckets, each own key. if put datastructure, hash_key() method finds appropriate bucket , appends new data bucket's contents. actually, random bucket receive data, since it's hash function, same bucket same key.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -