Turn in this page for your group
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.
c[3:6]+b[2]
This has a cost of three: one slice, one catenation, one index.
a[3:5]+a[18:]
, again the cost is
three: two slices and one catenation with +.
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.
|||||||||||| |||||||||||| ___ ___ / \ / \ o || o \___/ \___/ | | | ____ | | | \__________/ |||||||||||| |||||||||||| ___ ___ / \ / \ o || o \___/ \___/ | | | ____ | | | \__________/
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 \___/ \___/ | |
head
, not the function
sides
to create a head like the one below.
|||||||||||| |||||||||||| |||||||||||| ___ ___ / \ / \ o || o \___/ \___/ | | | | | ____ | | | | | \__________/
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 \___/ \___/ | | | \____/ | | | \__________/
nose
function and add it to one of the
heads. Make sure the function
returns a string of the right size.
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.)