CSSE 120 -- Intro. to Software Development

Homework 8

Please note: Due to the exam on Tuesday evening (and cancelled class session 9) , this assignment is due at the beginning of class session 10

  1. Complete the assigned reading for the next session (Zelle sections 7.4-7.6, 8.1-8.2).
  2. (12 pts) Complete the Angel quiz over this reading. You'll find this on the course Angel page, under Lessons → Homework → Homework 8 → Exception Handling and Loops
  1. (40 pts) Robot Functions. Checkout the HW08Robot project in your repository. Place all of the following function definitions in one Python source file, robotFunctions.py:

    1. (10 pts) Create a function called wander() that drives the robot in a random motion to explore an environment. The wander() function has three parameters, the robot, and linear and angular velocity. The parameters should be in the following order:
      1. robot
      2. [optional] Linear Velocity in cm/s, default = 15
      3. [optional] Angular Velocity in deg/s, default = 20
      To implement this function, select a random angle between 0 and 360 degrees, turn the robot, select a random distance between 10 and 30 cm, and move the robot forward. Repeat this random sequence of actions 5 times or until its cliff sensor is triggered (i.e. pick it up): see WanderVideo.wmv for an example. Hint: You will need to import an appropriate function from the random library to generate a random number: randrange(a,b) returns a random integer from a to (b-1), and random() returns a float between 0 and 1 (including 0 but not 1).
    2. (15 pts) Create a function called circle() that has three parameters and drives the robot in a circle with the given radius. The parameters should be in the following order
      1. robot
      2. Radius in cm
      3. [optional] Linear Velocity in cm/s, default = 15
      4. [optional] Direction=’CCW’
    3. Note that in your function you will need to calculate the angular velocity by using the formula: radius (cm) = linear velocity (cm/s)/ angular velocity (rad/s). This angular velocity is given in radians, so you will need to convert to degrees before calling the appropriate function from the pycreate library.
    4. (5 pts) Create a function figureEight() that calls the circle() function and has two parameters, the robot and the radius in cm. This function drives the robot in a figure eight by forming two circles with the given radius. It drives with a linear velocity of 15 cm/sec.
    5. (10 pts) Create a function kittLights() that takes two parameters: the robot and numRepeats, and turns on the robot’s power, play and advance lights using the sequence given below. The sequence should repeat numRepeats times (see LightSequence, LightsVideo.wmv). Look up the setLEDs() function in the pycreate library documentation for details on how to turn on Create’s LEDs. For example:
      		r.setLEDs(0,0,0,0) #turn off all of the robot's LEDS 
      		r.setLEDs(0,255,0,0)#turn on only the robot's power LED to green (see documentation)
      		r.setLEDs(0,0,1,0)#turn on only the robot's play LED
      
  2. (15 pts) Finish the individual-programming exercise you started in class: countFailPass.py, and commit it to your SVN repository.
  1. (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.

    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

    Check out  the HW8 project from your SVN repository.  It contains mostly-empty Python source files to use for this problem. Note that we are NOT doing the MoveToClick problem, just the mutantFunctions one. 

    Within the mutantFunctions module write and test functions 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.