Compsci 101, Fall 2016, Lab 2

The lab

In this lab you will:

You will need to snarf files for this lab. Those files are also here.

Getting Credit for Lab 2

To get credit for lab 2, you will need to do the following by Sunday night:


Part 1: APT

You'll solve the gravity APT. Create a Python module named Gravity, in that module create a function falling with the appropriate parameters (two). APTs are run on the APT page here.

Answer the following questions on the online form.

  1. Make the body of the function return 0.0

    Note that this is not the correct code. This is just for testing the APT.

    Run the APT by loading it into the web page, selecting the button by Gravity and then clicking on the test/run button.

    How many test cases does this pass? (how many greens do you get?).

  2. In the function before returning, print one line that identifies the value of the velocity, identifies the value of the time and identifies the product of the two.

    For example if the velocity is 3.0 and the time is 2.0 then that line might print:

    velocity is 3.0, time is 2.0, velocity*time is 6.0

    When you run the APT with the APT tester, explain where the output is printed?

  3. Now solve the Gravity APT with your group. Each of you should be writing your own code (typing the code on your laptop, in Eclipse or in just a file, or writing the code on paper if you do not have a laptop with you), but can discuss what you are doing and help each other debug.

    Cut and paste just your function into the form.

  4. In Eclipse in your file, below the function, add two print statements starting in the first column. That is, add two lines to call and print the falling method twice, once with time set to 9.0 and velocity set to 3.0, and once with time set to 14.0 and velocity set to 40.0.

    Run your program in Eclipse. What are the two answers?

  5. Now run your program with the APT Tester. How many total times was your function called?
  6. Modify your program below the function to add the line if __name__ == '__main__':

    and then make the two print statements the body of this if, both need to be indented four spaces.

    Run your program in Eclipse, and then run your program on the APT tester. What is the difference between them? What is the purpose of this special if statement with main?

Even though you are working on this APT in lab, you will still have to turn it in on the APT page and fill out the README that goes with it. Also make sure your name is on it with a comment.

In the README you will note that it was solved as part of lab 2 and who you solved it with.

Part 2: A Song

Consider the following common children's song:

Here we go 'round the mulberry bush,
The mulberry bush, the mulberry bush.
Here we go 'round the mulberry bush,
So early in the morning.

This is the way we wash our clothes,
We wash our clothes, we wash our clothes.
This is the way we wash our clothes,
So early Monday morning.

This is the way we iron our clothes,
We iron our clothes, we iron our clothes.
This is the way we iron our clothes,
So early Tuesday morning.

This is the way we scrub the floor,
We scrub the floor, we scrub the floor.
This is the way we scrub the floor,
So early Wednesday morning.

This is the way we mend our clothes,
We mend our clothes, we mend our clothes.
This is the way we mend our clothes,
So early Thursday morning.

This is the way we sweep the house,
We sweep the house, we sweep the house.
This is the way we sweep the house,
So early Friday morning.

This is the way we bake our bread,
We bake our bread, we bake our bread.
This is the way we bake our bread,
So early Saturday morning.

This is the way we go to church,
We go to church, we go to church.
This is the way we go to church,
So early Sunday morning.

This song was originally posted here .

Write a Python module/program to print out the lyrics to this song.

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

Each of you should be writing your own code, but discussing how you are doing it and help each other debug their code.

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. Your output of the song lyrics should be as similar as possible to what appears above, including line breaks and blank lines. Although if you cannot easily match the capitalization or punctuation, that is okay.

To start, use Eclipse and snarf the sample file for lab today called Farmer.py. Run this file to see what it does. Then create a new file named Mulberry.py for this program.

Answer the following questions. For the last question, the person filling out the form would cut and paste their code to give an example of what the group came up with.

  1. In the sample program Farmer.py, how many lines are printed when this program runs? Discuss first, then run your program to see.
  2. For a function, what is the difference between a parameter and an argument?
  3. For the Mulberry song, is there a phrase that repeats in every verse. It doesn't have to be exact. Describe a function you could write for this phrase. How many parameters would it need?
  4. Look at the first verse of the Mulberry song. How many functions could be used that could take advantage of repetition? Describe what those functions might be.
  5. Cut and paste your code for the Mulberry song into the code box after it is working.

Part 3: Experimenting with Functions

When you snarf lab2, you'll find the Python module Multiprint.py, the code is shown here:

word = "wonderful"
count = 1

def printit():
    global count,word
    print count,"\t",word
    count = count + 1

def f1():
    printit()
    printit()
    
def verse():
    f1()
    f1() 
    
if __name__ == "__main__":
    verse()

When this code is run, the output is:

1     wonderful
2     wonderful
3     wonderful
4     wonderful

If you add one more call of printit() in the body of f1() and one more call of f1() in the body of verse(), so that there are three identical lines in f1() and three identical lines in verse() the output changes to:

1     wonderful
2     wonderful
3     wonderful
4     wonderful
5     wonderful
6     wonderful
7     wonderful
8     wonderful
9     wonderful
You'll do two things with Multiprint.py. Answer these questions as you go along.
  1. First, experiment and generalize to make an hypothesis about what happens when there are 11 calls of f1() in verse() and 11 calls of printit() in f1() -- what will the output be? You should try to answer this question without actually creating a program with 11 lines in each of the functions, although you may do that.
  2. Describe the output when there are N identical lines in f1() and M identical lines in verse().
  3. Now start with the original Multiprint.py program in which there are four lines printed, with two calls in each of f1() and verse(). If you change the name of function f1 to f2, the program won't work because verse() calls f1() --- but you can then create a new function f1 to call f2, so that the program looks like this (without printit and main which do not change)
    def f1():
        f2()
        f2()
    
    def f2():
        printit()
        printit()
        
    def verse():
        f1()
        f1() 
    

    When you run this program there are 8 lines printed. Your goal is to modify the program so that it prints 256 lines.

    You can add more functions, rename functions, but the body of each function other than printit() must consist of the same function call repeated -- as shown above, for example where each line is repeated twice in the functions shown.

    With these modifications, how many total functions does your program have?

  4. Cut and paste your final code for Multiprint.py in the box.