Computation and Cognition - Fall 2006

Homework 5

Due by class time Wednesday, October 11

Programming Problems

Important: Do these problems in order.

  1. An archery target consists of a central circle of yellow surrounded by concentric rings of red, blue, black, and white. Each ring has the same "width", which is the same as the radius of the yellow circle. Write a program that draws such a target. Hint: objects drawn later will appear on top of the objects drawn earlier. Call your program target().

  2. Write a program to draw some sort of face. Call your program drawFace().

  3. Write a program that draws an abstract artistic design, some of whose elements are determined randomly (i.e., by using Python's random number functions). This could include randomly choosing the sizes and positions of lines and other shapes, or randomly choosing colors. Be creative! Call your program opus().

  4. Write a program called drawRow() that draws a horizontal row of 8 squares in a graphics window, as shown below. Each square should be 50 pixels wide. Hint: use a for-loop.

  5. Rewrite drawRow so that it takes the following parameters as input:

    drawRow(gw, cornerX, cornerY, numSquares, squareSize)

    where gw is a GraphWin object, cornerX and cornerY are the coordinates of the lower left corner of the leftmost square, numSquares is the number of squares to draw, and squareSize is the size in pixels of each square. For example, drawRow(win, 10, 300, 8, 50) would draw the above picture in a window called win with the lower left corner at location (10, 300). Next, write a program called drawGrid() that creates a new graphics window and draws the image below by calling drawRow three times with different values for cornerX and cornerY.

  6. Rewrite drawGrid so that it takes the following parameters as input:

    drawGrid(gw, cornerX, cornerY, numRows, numColumns, squareSize)

    where numRows and numColumns specify the number of rows and columns to draw. Hint: use a for-loop. Next, write drawCheckerboard(), which creates a new 500 × 500 pixel graphics window and uses drawGrid to draw an 8 × 8 grid with lower left corner at (50, 450).

  7. Now add one more input parameter called startColor to your definition of drawRow, which specifies the color of the leftmost square in the row. This color will be either red or white. The other squares in the row should alternate between red and white depending on the color of the leftmost square. Hint: use an if/else. For example, drawRow(win, 50, 450, 8, 50, "red") would draw:

  8. Now add a startColor parameter to drawGrid, which specifies the color of the leftmost square in the bottom row of the grid. The other rows should alternate their colors accordingly, in a checkerboard fashion. Update drawCheckerboard() so that it draws a red and white checkerboard pattern with the lower left square colored red.

  9. Rewrite drawFace to take three input parameters:

    drawFace(win, x, y, size)
    which draws a face centered on (x, y) of the specified size. Your function can draw a simple smiley (or grim) face, or something more complex. Demonstrate your function by writing a program called faces() that draws several faces of varying size in a single window.

  10. Rewrite the acronym program from week 3 as a function that takes a phrase as input and returns the corresponding acronym. Your function should work exactly as shown:

    >>> print "Do this", acronym("as soon as possible")
    Do this ASAP
  11. Rewrite the nameVal program (see Homework 3) as a function that takes a single input parameter called name, which will be a string, and returns the corresponding numeric value of name. Your function should work as shown:

    >>> print "The name Zelle has value", nameVal("Zelle")
    The name Zelle has value 60

    Next, rewrite fullnameVal so that it takes a full name as an input parameter and returns its total value. Hint: use string.split to break up the full name into pieces and then call nameVal on each of the pieces. Examples:

    >>> print fullnameVal("John Marvin Zelle")
    >>> print fullnameVal("Hillary Rodham Clinton") + fullnameVal("Laura Bush")

Turning in Your Homework

Put all of your program definitions into a single file called Print out this file and turn it in to me in class on Wednesday. If you have questions about anything, don't hesitate to ask!