CS 240: Data Structures and Algorithms
James Madison University, Fall 2012

HW #2

For this assignment you will use Blackboard to submit a single .py file containing solutions to the following programming exercises. You may use the file hw2.py as a template. Carefully test each function to make sure that it works as expected. These exercises will be graded both on functionality and code quality.

  1. Building on your work from Friday's activity, create a new function named polygonXY that takes an x and y position to begin drawing. The new version of the function should have the signature polygonXY(x, y, sides, size). Use penup and pendown to ensure that there are no lines drawn as the turtle is moving to the new starting position. (5pts)
  2. Complete the following Python function so that it conforms to the docstring. (Python docstrings are similar to Javadoc comments in Java. If the first line in a function is a string, then that string is assumed to provide documentation for the function.) (5pts)
    
    def drawPolygons( polygons ):
        """Use turtle graphics to draw a collection of polygons.
    
            Arguments: polygons - A list containing polygon locations and 
                      specifications.  Each entry in the list should be a
                      list with four entries: xpos, ypos, sides, size. 
                      For example: 
    
                      drawPolygonList([ [0, 0, 3, 50], [50, 100, 4, 20] ])
    
    		  would draw a triangle at position (0, 0), and a 
    		  square at position (50, 100).                 
        """
    
        #Your code goes here...
    
        #iterate through the list of polygons using a for loop.
        #For each entry, call polygonXY.
    
    
  3. Complete the following Python function so that it conforms to the docstring. There is a helpful section on Python file I/O in section A.6.3 of our textbook. (5pts)
    
    def readPolygonFile( name ):
        """ Returns the list of polygons stored in the provided file.
    
            Arguments:  name - A string containing a file name.  Each line of the
            indicated file must contain four integers specifying the position
            and shape of a polygon.  The format for one polygon is:
            xpos ypos sides shape
    
            For example, the following two lines:
    
            0 0 3 50
            50 100 4 20
    
            would encode a triangle at position (0, 0), and a square at
            position (50, 100).  The appropriate return value would be:
            [ [0, 0, 3, 50], [50, 100, 4, 20] ]
            
            Returns: a list of polygon specifiers, each of which is a list
            containing four integers. 
        """
        #your code goes here. 
    
    
  4. Create a function with the signature drawPolygonFile( name ) that takes a file name as a parameter, and draws the polygons stored in that file. If you have finished the previous two exercises, this should only require two lines of code. You can test your function on the following two files: simple.txt and picture.txt. Since the second file contains over four hundred polygons, it will take a long time to draw. You can speed up the process using the turtle module's speed function. (2pts)
  5. Famous Last Words Create a function with the signature lastWords(string). This function should take a string argument containing multiple lines of text as input, and should return a new string containing the last word from each line of the original string. For example, after the following code segment
    text = """My main goal is to convince everyone 
    to  recycle at least one aluminum can
    every day.  Following this program
    protects wildlife, including the rare Python. """
    
    message = lastWords(message)
    
    the variable message should contain the string "everyone can program Python."

    Hints:
    • Python strings have a splitlines method that returns a list of lines in the string.
    • The easiest way to get the last item from a list is to use a negative list index.