Skip to content

Hashing it out

November 2, 2009

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…

  1. will the cars get unhitched—with their contents still inside—and reshuffled?
  2. will the cars stay where they are while the contents are removed, reshuffled, and reinserted (as before)?
  3. 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.  :-)

About these ads
3 Comments leave one →
  1. Alex permalink
    November 13, 2009 2:54 pm

    You’re correct. Decoupling the names and scores into separate arrays causes major problems as soon as you sort one of them.

    Dictionaries are indeed the Python analog to your PHP “genies”. By storing key/value pairs (where the value can be whatever you want it to be, even another dictionary) you really do get into the “Phenomenal Cosmic Powers” arena.

  2. Yong permalink
    June 4, 2010 1:48 am

    Hi Julie,

    Thanks so much for blogging this – I’m working through the book, too, and love the help!

    I’m on chapter 5 and have a question about this bit:


    for each_score in scores.keys():
    print('Surfer ' + scores[each_score] + ' scored ' + each_score)

    What I’m confused about is this:

    if each_score is an array, and scores.keys() returns an array which contains the keys from the scores hash, how does the line

    scores[each_score]

    result in the values of the scores hash?

    Hope that makes sense, any help would be great!

    • Aradalf permalink
      April 23, 2011 2:43 pm

      This works because the for loop goes through the array, processing each element one by one. In addition, if you recall, scores[8.45] returns the value associated with the key. So when the key is used i.e. scores[each_score], the value associated with the score i.e. the name of the surfer, is returned.

      What wonder about is the:
      for key in scores.keys():
      print (scores[key] + ‘ had a score of ‘ + str(name_part))
      example shown. Where does the random ‘name_part’ variable come from? Shouldn’t the variable ‘key’ be substituted for the ‘name_part’ variable instead?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: