[CentralOH] More on ex 39, dump()

jep200404 at columbus.rr.com jep200404 at columbus.rr.com
Sat Dec 6 16:51:26 CET 2014


On Sat, 6 Dec 2014 07:16:55 -0800, timothy spencer <smashing_good_show at hotmail.com> wrote:

> I think I figured out what the '[0]' is for in that dump() I wrote:
> 
> def dump(aMap):
>     for bucket in aMap:
>         if bucket:
>             for k, v in bucket:
>                 bucket_index = hash_key(aMap, k)
>                 slot_index = get_slot(aMap, k)[0]
>                 print bucket_index, slot_index, k, v
> 
> With get_slot(), a tuple of 3 items is returned, and putting 
> the [0] there specifies to only have the '0th' item accessed 
> in the tuple for the dump().

You correctly figured out what the [0] is for. 

Even empty buckets have indexes. 
Requiring a key to figure out a bucket index is very awkward, 
and will not work at all for empty buckets (which have no keys),
(neverminding that you do not print out anything for empty buckets).
Eliminate the hash_key() and get_slot() calls. 
Use the enumerate() function instead. 
Search the web for examples of how enumerate() is used. 

What's the purpose of the "if bucket:" statement? 
Try your code without that statement. What happens? Why?
other folks: please let him figure it out instead of telling 
him the answers

    (do nothing) gracefully


More information about the CentralOH mailing list