Compsci 6/101, Spring 2011, Lab 2

This lab has two parts. 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. You'll also solve some String puzzles in Python using the slicing operator.

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:

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.

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

  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. 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.
  1. First modify the program to create a picture like this -- 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 fright_hair 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 function and use it to create a smiling head, something like this?
    
    
    ||||||||||||
    ||||||||||||
    ||||||||||||
     ___    ___
    /   \  /   \ 
      o  ||  o   
    \___/  \___/ 
    |          |
    |  \____/  |
    |          |
    \__________/
    
    Create two other eye-ball-drawing functions and then modify the program to produce these heads, or ones close 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. 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 headleft():
        print fright_hair()
        print eyes_left()
        print sides()
        print mouth()
        print sides()
        print chin()
    
    def headright():
        print fright_hair()
        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 is a parameter:
    def makehead(eye_function):
        print fright_hair()
        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.)