Compsci 101, Fall 2014, Lab 2

The lab

In this lab you will:

Getting Credit for Lab 2

To get credit for lab 2, you will need to enter your names and netids, and the answers to several questions on an online form.

Complete this form for credit for lab 2

(the same questions are accessible here as well).

Part 1: APT

Snarf the gravity APT. Create a Python module named Gravity, in that module create a function falling with the appropriate parameters (two). Make the body of the function return 0.0

This is not the correct code.

Run the APT. See instructions on how to run an APT here.

How many test cases does this pass? (how many greens do you get?). Note that on the online form for lab.

Now solve the Gravity APT with your group. Each of you should be running the APT on your computers, but can discuss what you are doing and help each other debug.

Note that this APT you will also turn in with the first APT problem set. Then you will note that it was solved as part of lab and who you solved it with.

Part 2: Song Problems

Write two separate Python modules/programs, one to print out the lyrics to each of the following common children's songs:

Do not use more advanced features than what we have covered in class (statements and functions).

To start, use Eclipse to make a new project for this lab. Within this project, you will create two separate Python modules, one for each song listed above. This page describes how to do this for a previous version of PyDev (i.e., some of the menus look slightly different).

Using Functions

It is easy, of course, to write the program using a series of print statements for each line of the song. But the goal of this lab is to take advantage of the cumulative structure of the song to avoid redundancy. Look for ways to use functions to avoid simple redundancy and functions with parameters to avoid the structural redundancy within the songs. In particular, make sure that you use no more than one print statement for each distinct line of the song.

For example, the line in the song The Farmer in the Dell:

Hi-ho, the derry-o.

appears several times, but your program that generates the song should have only one print statement that produces this line which is defined in a function that is called multiple times within your program.

Likewise, the following lines from the song Here We Go 'Round the Mulberry Bush:

So early Monday morning
So early Tuesday morning

are structurally the same, with only the words Monday and Tuesday different. Again, your program that generates the song should have only one print statement that produces these lines which is called multiple times within your program with different parameters.

Your output of the song lyrics should be as similar as possible to what appears on the websites, including line breaks and blank lines. Although if you cannot easily match the capitalization or punctuation, that is okay.


Part 3: Random-Walking

Hopefully you'll have time for this. If not, no worries.

Snarf the code for this part, or copy the file LabWalk.py here.

Run the program LabWalk that simulates and visualizes a one-dimensional random-walk. Random walks are used in scientific and financial simulations. A one-dimensional walk can be thought of as a bug or drunkard starting at the origin of a number line and walking left or right with equal probability, taking one step each time.

The program you're given shows a bug, with the letter 'B' in the console and prints the location of the bug and the time-step at which the location occurs in the simulation. Here's the beginning of a run, the Bug starts at the origin in the middle of the screen (that's the visualization. In the run below the bug walks right, then left (back to the origin) then right, right, and so on.

 
|                                        B
| 0    1
|                                         B
| 1    2
|                                        B
| 0    3
|                                         B
| 1    4
|                                          B
| 2    5 
|                                         B
| 1    6 
|                                        B
| 0    7 
|                                         B
| 1    8 
|                                        B
| 0    9 
|                                       B
| -1   10 
 
  1. Run the program twice. The value printed after the visualization is the final location of the bug. Enter that on the online form.
  2. The simulation is done in the function walk. How many Python functions are defined in the LabWalk module? What are their names?
  3. Which line of code updates the variable pos to simulate the bug moving to the right (increasing the value of its location).
  4. Currently the function walk returns the distance of the bug from the origin at the end of the simulated walk. Create a variable zcrosses, initalize it to zero, and update it by increasing it by one whenever the bug's location is zero. zcrosses = zcrosses + 1 Put the statement above inside an if statement body that checks to see when the value of pos is zero, e.g., if pos == 0. Return the value of zcrosses instead of the bug's position. Run the program five times. What's the maximal number of times the bug is at the origin during the five runs?
  5. Uncomment the lines in walk that begin with a hashtag and have a call to plt.plot and plt.show. This means you should erase/remove the hashtags. What happens when you run the program?