Compsci 101, Fall 2014, Lab 3

The lab

In this lab you will:

Getting Credit for Lab 3

To get credit for this lab, 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 3

(the same questions are accessible here as well).

Part 1 : String Slicing and Dicing

Assume you've typed these three string values and associated variables into your Python interpreter/console window:

a = "computational thinking"
b = "duke university"
c = "python code"


You may want to actually type these into a console window, though you should first try to do the problems below using paper, pencil, discussion, and thinking (no computer)

Using these strings only, you should use the slicing operator, string concatenation, string multiplication (by an int, as in "abc" * 3), or indexing to form each of the words below. See the examples for details. Each use of any operator (slicing, indexing, concatenation, and multiplication) has a cost of one. Indicate the cost of your construction.

  1. "honk" formed from c[3:6]+b[2]

    This has a cost of three: one slice, one catenation, one index.

    The value of c[3:6] is hon because the slice starts at the character whose index is 3 which is 'h' and goes up to, but doesn't include the character whose index is 6.

    the value of b[2] is k because it's the single character whose index is 2. Adding or concatenating these strings together makes "honk".

  2. "puking" formed from a[3:5]+a[18:], again the cost is three: two slices and one catenation with +.

    Note that the value of a[18:] starts at the index-18 character and goes until the end of the string, i.e., is king. You can also multiply strings by a number at a cost of 1, i.e., a[3:5]*2 is "pupu" at a cost of 2: one slice and one multiply.

  3. Create "docking" and provide its cost.

  4. Create "cocoa" and provide its cost.

  5. Create "thought" and provide its cost.

  6. Create "ratatatat" and provide its cost.

  7. Create "duke is cool" and provide its cost.

  8. Create "diversity nation" and provide its cost.

  9. Create "money honey" and provide it's cost.


Part 2: APT Acronym

A list can be used with a for loop to have the body of the loop repeat the number of times equal to the number of items in the list.

Sometimes it is helpful to repeat a loop for the number times for which there are words in the string. In that case it is helpful to generate a list of the words in the string and then use the list with the for loop.

Discuss the following code in your group to see what it does, then type it in and run it to see if you were correct.

phrase = "Do Re Me Fa So La Ti Do"
phraseList = phrase.split()    
# note that  phraseList = [Do, Re, Me, Fa, So, La, Ti, Do]
answer = ""
for word in phraseList:
    if word[1] != 'a':
        answer = answer + word
print answer

Read the Acronym APT statement. Then with your group come up with a general plan for how to form the acronym of the parameter phrase --- your plan should be in English, not in code.

Reminder, even though we are discussing this APT in lab, you still need to complete and submit this APT as part of the APT problem set.

Answer these questions on the online form.

  1. What string method returns a list that is the words represented by the string, e.g., as separated by spaces? Write code to create a list variable that is the words in parameter phrase using this method. Name the list variable words.

  2. What is a for loop that will loop over each element of the list of words in phrase (use variable words from above) -- don't write the loop body yet, just the loop.

  3. Use the loop, explain how to index each word in the body of the loop to access the first character of the word, i.e., what's the Python code to do that and append the first character to the end of a string as shown below in the blank
         acro = ""
         for loop-you-wrote-above:
             acro = acro + ______________
         return acro
    


Part 3: Turtles in the house

Snarf the lab3 project into eclipse or get the code for Squares.py here. If you snarf it will appear in eclipse as fall14_compsci101_lab03. Run the program Squares.py to see what it does. When you want to get rid of the popup window, click on it.

Here are a few new Turtle commands used in the program. Suppose your turtle is named foo.

Part 3a:

Look over the code and answer the following questions about the code.

Part 3b:

Create a new pydev module called House in the same project folder. You might want to copy the code from Squares here and them modify it.

Write a function to draw a window. A window looks like the four windows in the house below. Copy your window function to the online form

Draw a house with a door and windows. Your house does not have to look like the sample below, your house must have at least one door and two windows. Have fun being creative. You should show the UTAs how much of the house you get completed in lab.