Session 29

Dictionaries and Kevin Bacon

CS 1510
Introduction to Computing

Opening Exercise: Crazy Dictionaries

What is printed by this Python script?
    dict01 = {}
    dict01[2] = 10
    dict01[3] = 5
    dict01['2'] = dict01

    dict02 = {}
    dict02[2] = dict01
    dict02[3] = 4
    dict02['2'] = dict02

    print( dict02['2']['2'][2][2] )

The answer is simple enough. Understanding why requires that we know that a dictionary can have a dictionary as a value, even itself. Dictionaries are mutable, the 'value' of the dictionary itself is a reference.

Questions About the Homework

Only a few people sent me questions about Homework 12 as asked in e-mail. One was about HTML:

What is HTML? Is it like the programming language for websites?

Yes, that is kinda what it is. HTML is a markup language for describing web pages. Even with Javascript, Python, and Ruby code driving much of the web, HTML is still the primary language used to define web pages. You can read a bit more about it in this introduction.

One was about Python:

I'm not sure what line 37 in the make_html_word function does:
    return word_str % (str(fontsize), word)
Isn't % an arithmetic operator? How does this work with a string?

% is the old Python string formatting operator. The two values in the parens -- str(fontsize) and word -- are substituted into word_str at the two %s locations. We learned the more modern way of formatting strings, using {} and the format() method:

    word_str = "<span style=\"font-size:{}px;\">{}</span> "
    return word_str.format(str(fontsize), word)

The same operator is used in all three of the module's functions. I have put a modernized version of html_tag_cloud in today's code file, if you would prefer to use it.

The rest of the questions were about the specification itself and how to write the program:

These are the kinds of things you want to think about as early as possible, so you have time to solve them -- and ask questions when you aren't sure! ... discuss possibilities.


When I asked what you all would like to do over the last three weeks of the course, several of you requested that we do something more interactive, perhaps a game. Others asked if we could create a double-clickable app, so that you could run your programs later. I would love to do something like that, too, but time and tools constrain us. Maybe next time.

With an assignment that generates web pages and a familiar graphic, I hope that we can approach the 'real world' feel of a game or an app. At least you can put the result on the web and show your family something you made. I hope this assignment is an enjoyable way to finish the course.

... running your script without IDLE ...

    > python3.4

    > cp crazy_dictionaries
    # ... add the shell intrepreter directive ...
    > chmod 755 crazy_dictionaries
    > crazy_dictionaries 

The executable version of the crazy_dictionaries script is in today's code file. Modify it to run on your system!

A New Kind of Data Structure

As I mentioned last time, with only the two dictionaries we built in Lab 14 we can begin to answer many interesting questions related to actors and movies. Drawing on the pop culture fad of The Six Degrees of Kevin Bacon, a movie fan/computer programmer wrote The Oracle of Bacon to mine the database at IMDB and answer fundamental questions about the universe. We have a much smaller database of actors. Can we use our two dictionaries to implement something similar?

Never challenge a programmer. He or she will take the bait. Even if the programmer is yourself.

So, given that we have two dictionaries that define a network that looks in part like this:

a part of the movie network in movies.txt

How can we implement the Oracle of Bacon, which finds a path from one actor (by default, Kevin Bacon) to any other, through movies in which they appear?

... what we start with:

... sketch out the process as an idea:

To keep track of this, build a 'connections' dictionary that keeps track of the actors we have seen. The value for each actor key is a tuple of (a) the distance from the starting actor and (b) a list of actors and movies that get us from the starting actor to the key.

... sketch out the process as an algorithm for find_path_between(actor_1, actor_2):

    1.  load the two databases
    2.  put actor_1 in a connections db with d == 0 and p == []
    3.  while actor_2 is not in the connections db,
        a.  select actors at the next distance
        b.  for each of these actors,
            add its partners to the connections db,
            extending the distance by 1
                  and the path by the actor and their common movies

That's the basic idea. Making it works takes a little bit more work...

... look at the code. You may have a hard time writing this code from scratch, but you know enough Python to read it. You can puzzle your way through this code, recognizing a little and learning a little.

When I asked what you all would like to do over the last three weeks of the course, a couple of you requested that we learn a little about artificial intelligence.

    breadth-first search
    data structures course
    algorithms course
    AI course
    game trees
    depth-first search
    evaluating moves
    basic technique

All it takes is baby steps. And a little hard work.

Those Crazy Self-Referencing Dictionaries


(return to session)

Wrap Up

Eugene Wallingford ..... ..... December 9, 2014