Compsci 101, Fall 2017, 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 by first testing it in Eclipse with a sample input, and then using the APT test page to test it on a lot of inputs.

APTs are run on the APT page here.

Create a Python module named Gravity (the Capital "G" is very important!). Note that when you create it, Eclipse will automatically add the .py extension to name the file Gravity.py. Also when creating it, you will see this window appear:

Select the line "Module: Main" and click OK.

Your file should then look similar to this:

The first four lines are a comment that puts the current date and tries to guess what your name is, using your username. The line

if __name__ == '__main__':

is a way for you to indicate where you want your program to start executing Python code. The word "pass" doesn't do anything. You would replace it with a line of python code that would need to be indented four spaces as "pass" is.

Now create a function named falling with the appropriate parameters (two). Strongly suggest that you cut and paste the def line under the specification of the APT definition for Gravity.

Put this function between the date/author comment and the if statement.

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 to verify that you have named the file and the def statement correctly.

    All code in the body of a function must be indented, this line needs to be indented four spaces.

    Your code should look similar to this:

    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 falling function, before the return statement, print one line that identifies the value of the velocity, identifies the value of the time and identifies the product of the two. (This line goes between lines 7 and 8 above and must be indented four spaces to indicate it is part of the function body).

    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

    Run the APT with the APT Tester. When you run this, explain where this line of output is printed on the web page.

  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.

    When you are ready to test your function, you will test it in Eclipse first with two sample inputs.

    Below the "if" statement and indented, add two print statements. That is, replace the word "pass" (on line 11 above) with two lines to call and print the falling method twice, once with time set to 3.0 and velocity set to 5.0, and once with time set to 3.0 and velocity set to 0.0.

    Run your program in Eclipse. What are the two answers? They should match the answers on the examples below (this picture is from the bottom of the page describing the Gravity problem).

  4. Put the two answers you get in the form.

  5. Add another print statement below the other two print statements to print the result when time = 5.2 and velocity = 11.5.

    Put that answer in your form

  6. Now run your program with the APT Tester. How many total times was the falling function called?
  7. Cut and paste just your function into the form.

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 REFLECT that goes with it. Also make sure your name is on it with a comment.

In the REFLECT 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 (OPTIONAL, IF TIME)

(If you don't finish Part 3 during lab, you can submit just the part you have completed).

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.