Compsci 06/101, Spring 2011, Lab 3

By entering your name/net-id below you indicate you are present for Lab 3 to answer these questions and that you were part of the process that resulted in answers being turned in.

Name: ______________    Net id: _____________ || Name: ______________    Net id: _____________

Name: ______________    Net id: _____________ || Name: ______________    Net id: _____________

Song Questions

  1. Briefly, what are the differences between functions third and fourth? What are the similarities?
    
    
    
    
    
    
    
    
  2. What information do you need to write the functions to print a fifth (or any other) verse?
    
    
    
    
    
    
    
    

  3. The functions cat and bird are similar, what are the differences in the functions?
    
    
    
    
    
    
    
  4. Give a reason why the function birdverse cannot be moved inside the function third. If you cut/copy/paste it inside third, what does Eclipse tell you?
    
    
    
    
    
    
    
    
  5. Variables with the names a1 and a2 appear in both bird and third -- provide a plausible explanation of why this is ok and doesn't cause confusion in the Python interpreter.
    
    
    
    
    
    
    
    
  6. Create a new version of the function fourth using the the nesting technique in your working version of the program. What are the last three lines of the function you wrote?
    
    
    
    
    
    
    
    
  7. Modify the Python code by adding appropriate functions in the same style as those you're given to print the new verse below in addition to all the verses that are already printed. What are the names of the new functions you wrote? What are the last three lines of the function you wrote that contains a nested function?

    
    
    
    
    
    
    
    

Random Walk Questions

  1. Modify the program so that instead of taking 50 steps, the bug takes 100 steps. Then 1000 steps. Does the bug get to the left in these runs (e.g., is the bug's position zero)?
    
    
    
    
  2. What happens if the variable location is initialized to 0 and to 80 when the program is run? Why?
    
    
    
    
    
    
    
  3. Modify the program to make it twice as likely that the bug moves right than left. What did you to to make this happen? What happens to the visualization of the random walk?
    
    
    
    
    
    
  4. For this question you'll write some functions to analyze the visualization of the walk that's printed, e.g., the string visual in the function main. Here's the function you should write in RandomWalk.py -- then call this function appropriately from main:
    1. What is the purpose of the call to split and the variable steps in the code above?
      
      
      
    2. What is the call to analyze from main?
      
      
      
    3. In words, how would you find the location farthest to the right in the bug's walk as part of the analysis? Use words, not code.
      
      
      
      
      
      
      
  5. Modify the function analyze so that it prints the bug's location that's farthest to the right using the code below. Then answer the questions about the code.
    1. What does the string method find do?
      
      
      
      
    2. In words, what are the contents of list locs in the code above?
      
      
      
      
      
      
    3. What does the function max do?
      
      
      
      
      
    4. How would you find the farthest left location? What do you expect it to be?
      
      
      
      
      
    5. The function sum returns the sum of all numbers in a list. Find the average value of the bug's location during a walk by writing Python code. What's the code? What's the average value in a random walk of 1000 steps?
      
      
      
      
      

Song III

  1. The function make_verse returns two things, both are functions. What are the clues in the return statement and in the function call that two things are returned?
    
    
    
    
    
    
    
  2. What are the clues in the return statement and in how the returned values are used in main that the return values are functions and not strings?
    
    
    
    
    
    
  3. What is the call to make_verse that will result in generating the verse about the woman eating a dog? How will you use the returned values to print the verse?
    
    
    
    
    
  4. The final step in this abstraction is to use lists to avoid the redundancy in the names used to store the returned functions in the many calls to make_verse. Here's a new version of main that you should try to finish and about which you'll answer questions.

    1. There are three parameters to add in the function call to make_verse. Add them so the program runs using this loop. What did you add?
      
      
      
      
      
    2. What is the purpose of the variable lastverse? What type of value does it hold?
      
      
      
      
    3. How would you add a verse about a cow about which I don't know how she swallowed a cow?
      
      
      
      
    4. Use words to describe how to add a new verse in the code above.