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).
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.
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".
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.
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.
phrase
using
this method. Name the list variable words
.
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.
acro = "" for loop-you-wrote-above: acro = acro + ______________ return acro
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.
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.