Hashing it out
Well it turns out that all but three of us had other plans tonight, so we decided to postpone Meeting #3 until next week. We’re working through chapters on our own in the meantime, so tonight I sat down with Chapter 5.
If you recall, Chapter 4 had us split a list of Surf-A-Thon results into names and scores, then sort the scores and print the top three. But we need to know who those scores belong to, so we need a way to associate the names with the scores. I’ve been thinking about this.
If you also recall, I’ve recently read a chapter on PHP arrays, and thus have just enough knowledge to be dangerous (or plain stupid). According to that chapter, PHP allows you to store a whole other array (array’, if you will) inside an array element. [This makes me think of a universe within a universe, or, in other words: "Phenomenal cosmic power... itty-bitty living space."] In any case, it seems to me that one way to solve our name-score problem is to create an array whose elements are scores, and then to create a one-element array’ for each of those, whose one element would be the associated name.
Actually, if Python allows strings as keys, perhaps we could just make each name the key and each score the value? One possible catch comes to mind here: In the array with numerical keys, the values became “detached” from these keys when they were sorted. If the same phenomenon occurs with strings as keys, that won’t work.
Here’s another way to say it: Think about each element in the array as a train car, with the keys painted on the side and the values stored within. When the keys were numerical (0, 1, 2, etc.) and we sorted the contents, the train cars stayed hitched in the same order, and the contents were removed, reshuffled, and reinserted. Then we were able to print the contents of cars 0, 1, 2 and we received the three highest scores. So I wonder: if we paint strings on the sides of the cars, names in this case…
- will the cars get unhitched—with their contents still inside—and reshuffled?
- will the cars stay where they are while the contents are removed, reshuffled, and reinserted (as before)?
- or, perhaps, will the contents be removed, reshuffled, and reinserted while someone scrubs off the paint and repaints the cars so that the strings are also sorted (in some kind of order—alphabetical?)?
It’s difficult for me to think of more than one or two scenarios in which option 3 would be useful, so I hope that’s not the way it was designed.
Chapter 5 begins by asking us to be like the fox, who makes more tracks than necessary, some in the wrong direction: it suggests that we create two arrays (one for names and one for scores). Well it takes me all of two seconds to recognize that this is a thoroughly useless approach; the minute we cleave this data and shuffle it, we’re done for. It’s like cutting a stack of papers at the margins containing the page numbers, and then dropping the resulting stacks on the floor.
So, okay, flipping ahead… I’m told there’s this thing called a hash. Well, actually, the Pythonic term is a dictionary. (Python peeps, enough of this alternative vocabulary. What are you, special? So some of you have a pony. Good for you.*) Well, actually, I’m told there’s a hash, and a set (no problems), and a queue (no problems), and a linked list, and a multi-dimensional array. Perhaps this last is the equivalent of the PHP genie? But no matter, the hash has exactly two columns, so it appears to be what I really need.
At this point, potential dinner plans appear and I decide to seize the moment, so tune in later to see how all this works out…
*Nuthin’ but love, peeps. Nuthin’ but love. :-)