CSC108H Assignment 1, Summer 2009

Introduction

The purpose of this assignment is to give you practice with tracing Python programs and writing your own programs. You will practise with variables, if-statements, functions, and with the processing of images.

You'll use our media module (called Pygraphics when you install it) for several parts of this assignment. You should make sure that you have properly installed that module. You can test that it is working by typing import media at the Python shell. Then, please place this picture.py and color.py into the same directory in which you are working on your assignment. These picture and color modules exist as part of the media module, but I have made a couple of minor fixes. By placing these files in your assignment directory, Python will use them instead of the ones supplied as part of the media module.

Part 1: Tracing (25%)

The program graphic.py produces and displays a tiny graphic image. Notice that the lines of code do not execute from the top of the file to the bottom, and some lines execute multiple times. In a file called trace.txt, list the line numbers of graphic.py in the order in which they execute. If a line is executed multiple times, list it multiple times. Each time line 8 has been executed (that is, each time a pixel is about to be made blue), specify the current values of x and y.

In order for us to tell whether or not you traced the program correctly, you must follow some strict rules about the format of your file trace.txt:

As an example of the format, a piece of your file trace.txt might look like this:

7
8
x: 6, y: 19
9
11
2
8
x: 1, y: 42
9
    
(The numbers in this example are not correct! The example here is only to show you the correct format.)

Note that I made the size of the graphic image very small -- variable dimension is set only to 3, meaning that the resulting image is only 3 pixels by 3 pixels in size. This was just so that your trace wouldn't be too long and tedious. You might find it interesting to make a copy of the program with a larger dimension size and see what the graphic looks like when you run the modified program.

Part 2: Writing functions (35%)

In a file called functions.py, write the following functions. Include a docstring comment for each function.

Function Name Description
vert_line(pic) Given a Picture object, draw a red vertical line in the middle of the picture that goes from the top of the picture to the bottom of the picture. (Hint: what methods in media can help you draw the line?)
total_red(pic) Given a picture pic, return an integer equal to the number of pixels that are pure red (i.e. maximum red component, with no blue or green components).
gradate(size) Create a picture that is size pixels wide and size pixels high. Make each pixel in the first (leftmost) column of pixels in the picture blue. Make each pixel in the second column a slightly darker blue (check the media module for how to darken a color.) Continue to do this for each column of pixels. Do not worry if the color reaches its darkest point prior to exhausting all columns; darkening the color at that point will simply have no effect. Be very careful that you don't inadvertently change the definition of what it means to be the color blue!
num_leap(year1, year2) Given two years, determine how many leap years will occur between them. You should include the two endpoints in your range; if year1 or year2 is a leap year, it should be counted. (Hint: use a function in Python's calendar module. How can you learn about the features this module offers?)

Part 3: Writing a program (40%)

ISBN, the International Standard Book Number, is a special code printed on most books, such as:
0205080057
123456789X
(We are ignoring the dashes that are usually printed between groups of digits in the ISBN numbers.) The first 9 digits are assigned by a book publisher, and the last symbol, which we will call the check symbol, is computed from these previous digits as follows. First, we create a weighted sum: take the first digit, add 2 times the second digit, add 3 times the third digit, add 4 times the fourth digit, and so on, until you add 9 times the ninth digit. Then, divide this sum by 11. If the remainder resulting from this division is less than 10 then that remainder becomes the check symbol; otherwise the check symbol is the symbol "X".

For example, consider the first nine digits of the above ISBN:
020508005
The weighted sum is:
sum = 0*1 + 2*2 + 0*3 + 5*4 + 0*5 + 8*6 + 0*7 + 0*8 + 5*9
= 4 + 20 + 48 + 45
= 117
When we divide 117 by 11, the remainder is 7, so the check symbol (the last digit in the ISBN) is 7. You should work out the second example above to see why the check symbol is "X".

For this part of the assignment, you will submit a file called isbn.py. In this file, your task is to write a function check_symbol(isbn) that takes an integer representing the first nine digits of a book's ISBN number, and returns the check symbol. Do not use a loop or a string to solve this problem; instead, use as many variables as required to perform numeric operations on the ISBN digits. In particular, think about the mathematical operators you can use to extract each digit from the ISBN number. Here is a sample interaction with the function:

>>> check_symbol (20508005)
7
>>> check_symbol (123456789)
'X'
>>> check_symbol (190498783) # a cool ISBN :)
4
>>>

Note that when an ISBN number begins with the digit 0, as in the first sample execution of the function here, we do not include that leading 0 when calling the function (notice that we passed an integer with only eight digits). The reason is that Python would then treat our ISBN number as an octal (base 8) number, not a decimal (base 10) number. However, your function must work properly for such ISBN numbers with implicit leading 0 digits!

Further Guidelines

Marking

These are the aspects of your work that we will focus on in the marking:

What to Hand In

Hand in the following files:

Remember that spelling of filenames, including case, counts: your files must be named exactly as above.