Spring Semester 2007


Lab Notes Two: A201 review continued.
What is the lab assignment for today? Write a program that produces a scalable "4":


Consider other letters, such as:

Consider using a method with two parameters: width and height.

When is this due? It's due next Wednesday after the lecture.

Do we know what Homework One is already? Yes, you need to write a program that produces magic squares?

What is a magic square? You can read all about it here.

Whoa, baby! When is this due? It's due next week on Friday night.

What else do we do today? Review some of the programs listed in the document handed out yesterday.

Great. Let's get started. Here's one of the programs we developed yesterday in class:
sluggo.cs.indiana.edu%cat one.py
#!/usr/bin/python

size = int(raw_input("What size: "))

for lin in range(size):
    for col in range(size):
        if (lin == 0 or col == 0 or lin==size-1 or (lin == size/2 and col < size/2)):
            print "*",
        else:
            print " ",
    print
sluggo.cs.indiana.edu%./one.py
What size: 15
* * * * * * * * * * * * * * *
*
*
*
*
*
*
* * * * * * *
*
*
*
*
*
*
* * * * * * * * * * * * * * *
sluggo.cs.indiana.edu%

I remember that. Here's another, based on the first:
sluggo.cs.indiana.edu%cat one.py
#!/usr/bin/python

size = int(raw_input("What size: "))

for lin in range(size):
    for col in range(size):
        if col < size/2:
            print "*",
        else:
            print " ",
    print
sluggo.cs.indiana.edu%./one.py
What size: 15
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
sluggo.cs.indiana.edu%

This gave us the program for the pattern we started from: A triangle pointing nortwest.
#!/usr/bin/python

size = int(raw_input("What size: "))

for lin in range(size):
    for col in range(size):
        if col < lin:
            print "*",
        else:
            print " ",
    print

Actually this prints a triangle pointing southwest. Here's the change you need to make:
sluggo.cs.indiana.edu%cat one.py
#!/usr/bin/python

size = int(raw_input("What size: "))

for lin in range(size):
    for col in range(size):
        if col < size -lin:
            print "*",
        else:
            print " ",
    print
sluggo.cs.indiana.edu%./one.py
What size: 15
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
sluggo.cs.indiana.edu%

OK, I understand this now. What else did we talk about?

We talked about sequences. Things like: strings, tuples, lists.

Let's take strings first. Here's how you work with them:

I see you have included tuples, and presented mostly slicing. True, since the syntax is the same.

What else is the same? The fact that you can't change tuples or strings.

They're called immutable. Precisely.

Let's take a look at lists. Slicing lists is done in the exact same way.

Lists however are mutable. Yes, I saw that.

Getting the length of sequences is done using len (a method). Correct. Since lists are mutable take a look though in what new ways slicing can be used:

I see you can delete list elements too. We mentioned we could use nested structures to implement matrices:

Lists have useful methods. Here are some examples:

The meaning of these methods should be clear, to avoid surprises: Not all these methods are necessary, some are just a convenience.

We also discussed dictionaries.

Here's the program that profiles a string, implemented with dictionaries. A good overview of the most important methods.
    message = raw_input("Give me the word: ")
    dictionary = {}
    for e in message:
        if dictionary.has_key(e):
            dictionary[e] = dictionary[e] + 1
        else :
            dictionary[e] = 1
    print "I have built the dictionary:"
    for e in dictionary.keys():
        print "Letter " + e + " occurs " + str(dictionary[e])
    letter = raw_input("Give me the letter: ")
    if dictionary.has_key(letter):
        print "Letter " + letter + " occurs " + str(dictionary[letter]) + " times in " + message
    else:
        print "Letter " + letter + " does not occur in " + message

    print "Again:"

    count = 0
    for e in message:
        if letter == e:
            count = count + 1
    print count

Here's what we said about implementation of matrices using dictionaries. It's included as a screenshot below:
luggo.cs.indiana.edu%python
Python 2.3.4 (#1, Sep 26 2006, 17:23:01)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1,2
(1, 2)
>>> d = {}
>>> d[1,2] = 1
>>> d
{(1, 2): 1}
>>> d[0,0] = 3
>>> d
{(0, 0): 3, (1, 2): 1}
>>> d[0,0] -= -2
>>> d
{(0, 0): 5, (1, 2): 1}
>>>                        

The text says this is how you implement sparse matrices. Indeed, an entry is considered zero if a key for it is missing.

You can use this method for Homework One. Dictionaries can implement non-sparse matrices just as well.

I suppose this leaves methods for next week. Might as well.

Try to solve all the problems in the list handed out yesterday. Do thelab assignment, work on homework one, then we start methods.

Updated by Adrian German for A202/A598