CS0007 Assignment 1

Due: Monday September 28 by 11:00 pm

This Assignment Was Modified Slightly 9-15-09 at 5:30pm

Partners

You may do the assignment with a partner or alone. In either case, please complete the partner sign-up sheet before the due date. The signup form is here. If you are not working with a partner, please enter "none" for your partner.

Only one partner should submit the assignment so that we don't get confused about what to grade!

Both of you should do the entire assignment, and work together to create your final solutions. You'll both need the experience/practice to prepare you for the exams.

Introduction

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

You and your partner should spend at least one hour reading through this handout to make sure you understand what is required.

Part 1: Tracing

The program graphic.py produces and displays a tiny graphic image. Notice that the lines of code do not execute from top 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 is executed, 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
11
2
8
x: 1, y: 42
    
(The numbers in this example are not correct! The example here 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

Write the following functions. Include a docstring comment for each comment.

Work on your functions one by one. Test and debug each before going on to the next one. Working in separate files during development might be easier. Be sure to include a docstring for each function.

Write extra functions to make your code more readable. If your code is more readable, you'll solve the problem sooner - you'll understand better what you are doing!

After you are done verifying your functions, but them and the extra functions into one file, called functions.py. This is the file you will submit.

Function Name Description
typing_speed(words, seconds) Given an integer (with value at least 0) indicating a number of words typed and another integer (with value at least 1) indicating the number of seconds it took to type that many words, return the typing speed, in words per minute (as a float).
month(code) Given an string representing a date in yymmdd format, where the year is at least 1, return the month number, an int between 1 and 12. (Hint: You don't need to use strings for this. Convert 'code' into an int. Division by numbers that are powers of 10, and modulo by numbers that are powers of 10, will be very helpful.)
average_red(pic) Given a picture pic, return a float that is the average red value among all its pixels.
average_intensity(pic) Given a picture pic, return a float that is the average intensity value among all its pixels. The "intensity" of a pixel is the sum of its red, green, and blue values.
warhol(pic) Given a picture pic, change each pixel whose intensity is less than the average in that picture into media.yellowgreen, and each pixel whose intensity is at least the average into media.powderblue. Do not return any value.
add_border(pic, thickness, color) Given a picture pic, add a border that is thickness pixels wide and in the given color. Do not return any value. The parameter thickness is an integer that is at least 0, and at most the height or width of the picture, whichever is smaller. The parameter color is one of the colors defined in the media module. Hint: Functions add_rect_filled, get_height, and get_width from media will be helpful. Also, while debugging your code, you might find it useful to use media.create_picture to create your own simple, small picture to work with.

Part 3: Writing a program

Write a program in a file called frame.py that allows a user to "frame" a picture by adding a border around it. You may use any of the functions from Part 2 in your solution; you can import them into your framing program with the statement import functions. You will need to use the function raw_input(), which you have seen in class. Recall that it prompts a user to type something, waits for the user's input, and then returns what they typed.

Your program should do the following:

  1. Use raw_input() to get the filename of the picture the user wants to frame and media.load_picture() to load it.
  2. Use raw_input() to prompt the user for the following:
  3. Add the desired border to the picture object.
  4. Use media.save() to save the modified picture (with its new border) in the same file it came from.
Your program must not display the picture on the screen. It saves the modified picture in its file, so the user can view that file to see what the program did. Because your program modifies the actual picture file you give it, be sure to duplicate your picture before running your program. If you don't, you will lose the original picture! I recommend that you don't run your program on any picture that you care about, just in case you make a mistake.

Here is a sample interaction where user input appears in bold. You must use this format, including the exact wording, punctuation, and upper/lower case letters:

Picture to frame: IMG_7440.JPG
Border width: 25
Border color (b/w/g): g
    

If the user gives an invalid color choice (anything other than "b", "w" or "g"), print the message Invalid color choice. and do nothing more.

More requirements:

  1. Your program must have absolutely no input other than the input described above -- nothing that the user types, and no choosing of filenames with media.choose_file(), for example. And your program must have absolutely no output other than the output described above -- no messages to the user (not even a "welcome!") and no displaying of pictures.
  2. Your averaging should be as accurately as possible (both in this borders program and in your functions for Part 2). Use floating-point arithmetic and don't round the result.

Why such strict requirements?

Usually, many users contribute to writing a program. Each has to strictly follow the "specifications" for his or her part of the program, otherwise, the different pieces won't fit together! This assignment will give you practice following precise specifications. Also, the TA will write a program to automatically test the correctness of your program. So, all of your programs need to have the same behaviour.

Grading

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

What to Hand In

Hand in the following files:

You will use the digital drop box in courseweb. Instructions will be placed here soon.