May 2017
Intermediate to advanced
310 pages
8h 5m
English
To test our hash table, we create a HashTable, put a few elements in it, then try to retrieve these. We will also try to get() a key that does not exist. Remember the two strings ad and ga which returned the same hash value by our hashing function? For good measure, we throw those in as well, just to see that the collision is properly resolved:
ht = HashTable() ht.put("good", "eggs") ht.put("better", "ham") ht.put("best", "spam") ht.put("ad", "do not") ht.put("ga", "collide") for key in ("good", "better", "best", "worst", "ad", "ga"): v = ht.get(key) print(v)
Running this returns the following:
% python hashtable.pyeggshamspamNonedo notcollide
As you can see, looking up the key worst returns None, since the ...