CSSE 120 -- Intro. to Software Development

Homework 9 -- This is a substantial assignment: Start early.

  1. Complete the assigned reading for the next session (Zelle Chapter 7).
  2. (14 pts) Complete the  ANGEL quiz over this reading. You'll find them on the course ANGEL page, under Lessons → Homework → Homework 9 → Decisions

    Note:  All the work for the programming problems is to be done in the Session09 project, which you should have checked out from your SVN repository.
  3. (15 pts) Finish the pair-programming exercise you started in class: threeSquares.py, and commit it to your SVN repository.
    Note:  Because of the break, you and your partner can decide whether to continue working together on the homework (this problem and/or the others) or to work separately.
    Here are the instructions (more details and a picture in the PowerPoint slides):
      1.Run the threeSquares program to be sure it works. Put both students' names in the initial comment.
      2.Add a function, stats, that takes a Rectangle, r, as a parameter and returns the area of r
      3.Modify the program so that it displays the area of each rectangle inside the rectangle
      4.Finally, change stats to return both the area and perimeter
      5.Commit your project back to your repository; also email threeSquares.py to your partner.
  4. (30 pts) Finish the second pair-programming exercise you started in class: triangles.py, and commit it to your SVN repository.
    Graphics window for triangle program Correct output to the console from the completed program::
       Centroid of t1 is (100, 160).
       Area of t1 is 7200.00.
       Now we'll move the triangle
       Centroid of t1 is (210, 310).
       Area of t1 is 7200.00.
       Centroid of t2 is (276, 70).
       Area of t2 is 2400.00.
       Now we'll move the triangle
       Centroid of t2 is (296, 170).
       Area of t2 is 2400.00.
       Centroid of t2 is (340, 340).
    
  5. (40 pts) Mutable Parameters. This exercise deals with the concepts in section 6.5 of Zelle. You may wish to re-read that section before doing this exercise.  
    You may do it alone or with your partner from class. 

    Background

    In class we saw that functions can assign to their formal parameters:

    def nextSquare(x):
        x = x + 1
        return x * x
    

    But we also saw that the calling code cannot see the effect of this assignment:

    y = 8
    next = nextSquare(y)
    print "y = %d, next = %d" % (y, next)
    # prints:
    # y = 8, next = 81
    

    This is because the assignment to x in nextSquare() only changes the value referred to by x. Informally, it just moves the post-it note for x onto the number 9, but the post-it note for y remains stuck to 8.

    We've also seen that we can modify lists without using assignment:

    squares = []
    for n in range(10):
        squares.append(n**2)
    print squares
    # prints:
    # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
    

    Here the append() method tells the squares list to add the given square to its "tail". Informally, the squares post-it note remains stuck to the same list throughout the code, but the list is being mutated by the append() method.

    We can also mutate a list by assigning to its individual elements:

    numbers = [2,3,5,7,11]
    for i in range(len(numbers)):
        numbers[i] = numbers[i] ** 2
    print numbers
    # prints:
    # [4, 9, 25, 49, 121]
    

    In this example the numbers post-it note never moves once it is stuck to the original list. The assignment to numbers[i] in the body of the loop mutates the list by changing elements inside it. It might help to think of the list like an egg carton. We put the numbers 2, 3, 5, 7, and 11 into the first five pockets of the egg carton, then we affix the numbers post-it note to the carton. In the loop we take out each number and replace it with its square, one pocket at a time. The carton and the post-it note never change.

    We can use this idea of mutating a list to create a function that changes its parameters. Here's a function that does just that:

    def factEach(nums):
        """Replaces each element of nums with its factorial."""
        for i in range(len(nums)):
            nums[i] = factorial(nums[i])  # factorial defined as in class
    

    And some code demonstrating the function:

    numbers = [2,3,5,7,11]
    factEach(numbers)
    print numbers
    # prints:
    # [2, 6, 120, 5040, 39916800]
    

    To be turned in

    Use the Session09 project that you checked out in class.  Write and test functions in the module mutantFunctions.py that meet each of the following specifications.

    1. (10 Points) squareEach(nums), where nums is a list of numbers. Modifies the list by squaring each entry.
    2. (10 Points) sumList(nums), where nums is a list of numbers. Returns the sum of the numbers in the list.
    3. (20 Points) toNumbers(strList), where strList is a list of strings, each of which represents a number. Modifies each entry in the list by converting it to a number. Here's an example use:
      numList = [10,20,30]
      print "sum = ", sumList(numList)
      squareEach(numList)
      print "squared: ", numList
      print "sum = ", sumList(numList)
      strList = ["17", "1.5", "2.718"]
      print "As strings: ", strList
      toNumbers(strList)
      print "As numbers: ", strList
      
      #prints the following output:
      #sum = 60
      #squared: [100, 400, 900]
      #sum = 1400
      #As strings: ['17', '1.5', '2.718']
      #As numbers: [17, 1.5, 2.718]
      

    Commit your code to your SVN repository.

  6. (OPTIONAL -- Not for credit! Do this if you want more practice with interactive graphics) Graphics as Mutable Parameters.  Do programming exercise 6.16 from Zelle. The code for clickMe.py that you worked on in class during session 8 (available in your Session08 project), will be extremely helpful in solving this problem.

    Do your work in the clickToMove.py file in the Session09 project.  It is not necessary commit your code to your SVN repository (for grading purposes).