Compsci 6/101, Spring 2012, Lab 2

This lab has two parts. In the first part you'll practice with String slicing operators and loops. In the second part you'll work on a face-generation program to create aristic (or otherwise) faces automatically using a program to create and combine different parts of a face.

Turn in this page for your group

String Slicing and Dicing

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

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


You'll be given several strings. 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.

  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.


Faces (toward Totem Poles)

In this part of the lab you'll be using the Python file SimpleFace.py that you can snarf as part of Lab 2. You will be creating a face, then a sequence of faces that could be considered to be a totem pole. You'll work again on this for the first non-APT assignment.

After you've snarfed the code for Lab 2 you should run it. You'll see a printed face like the one below.



||||||||||||
||||||||||||
 ___    ___
/   \  /   \ 
  o  ||  o   
\___/  \___/ 
|          |
|   ____   |
|          |
\__________/

You'll be asked some questions about the code; some of the questions will require you to make changes and run the program again. You answer the questions on the handin page.
  1. First modify the program to create a picture like the one below in which there are two identical heads. In making the modification you should try to minimize modifying existing code and add as few lines as possible. Often these are competing goals, so not everyone will do this the same way. After getting the program to work, document what you did on the handin pages. If you can explain how to create three faces on top of each other, do that too.

    
    
    ||||||||||||
    ||||||||||||
     ___    ___
    /   \  /   \ 
      o  ||  o   
    \___/  \___/ 
    |          |
    |   ____   |
    |          |
    \__________/
    ||||||||||||
    ||||||||||||
     ___    ___
    /   \  /   \ 
      o  ||  o   
    \___/  \___/ 
    |          |
    |   ____   |
    |          |
    \__________/
    
  2. (change your code back so only one head is created).

    The existing hair_fright function returns two "lines" of hair. Modify it to return three lines. Explain part of your reasoning by answering the question on the handin pages.

    
    
    ||||||||||||
    ||||||||||||
    ||||||||||||
     ___    ___
    /   \  /   \ 
      o  ||  o   
    \___/  \___/ 
    |          |
    
  3. Make the head longer by modifying only the function head, not the function sides to create a head like the one below.
    
    
    ||||||||||||
    ||||||||||||
    ||||||||||||
     ___    ___
    /   \  /   \ 
      o  ||  o   
    \___/  \___/ 
    |          |
    |          |
    |   ____   |
    |          |
    |          |
    \__________/
    
    
    
    
  4. Do not modify mouth, but create a new function mouth_smile and use it to create a smiling head, something like the one below.
    
    
    ||||||||||||
    ||||||||||||
    ||||||||||||
     ___    ___
    /   \  /   \ 
      o  ||  o   
    \___/  \___/ 
    |          |
    |  \____/  |
    |          |
    \__________/
    
    Create two other eye-ball-drawing functions and then modify the program to produce the heads below, or ones reasonably similar to them with eyes ahead, left, and right. Do this in stages, not all at once. First create a eyes_left function and try it. Then create two heads -- one with eyes_left. The idea is to change your code incrementally and test-as-you-go.

    
    
    ||||||||||||
    ||||||||||||
    ||||||||||||
     ___    ___
    /   \  /   \ 
      o  ||  o   
    \___/  \___/ 
    |          |
    |  \____/  |
    |          |
    \__________/
    ||||||||||||
    ||||||||||||
    ||||||||||||
     ___    ___
    /   \  /   \ 
    o    || o    
    \___/  \___/ 
    |          |
    |  \____/  |
    |          |
    \__________/
    ||||||||||||
    ||||||||||||
    ||||||||||||
     ___    ___
    /   \  /   \ 
       o ||    o 
    \___/  \___/ 
    |          |
    |  \____/  |
    |          |
    \__________/
    
  5. Create a nose function and add it to one of the heads. Make sure the function returns a string of the right size.

  6. Make some change of your own design, share it with the class.

  7. EXTRA If there is extra time: In my program to print three heads with different eyes, I had three different head functions with lots of common code. Here are two of them. Parameters and functions can sometimes arise from taking what's different and turning it into a parameter.
    
    
    def head_left():
        print hair_fright()
        print eyes_left()
        print sides()
        print mouth()
        print sides()
        print chin()
    
    def head_right():
        print hair_fright()
        print eyes_right()
        print sides()
        print mouth()
        print sides()
        print chin()
    
    What's different above is the name of the eye-ball-generating function. Suppose the name of the function is a parameter! as below:
    def makehead(eye_function):
        print hair_fright()
        print eyes_function()
        print sides()
        print mouth()
        print sides()
        print chin()
    
    
    Then a call looks like makehead(eyes_right). Make such a function and use it to generate three different heads (with eyeballs different and all else in each head the same.)