NOTE: Labs are to be done during lab with a partner. Do not start the lab until you are in lab.
There are no files to snarf for this lab.
To get credit for lab, you will need to do the following by Sunday night:
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.
You'll put each answer on the google form. The first two examples show you how to slice an index, you'll answer starting from question 3 below. The strings and indexes are shown below to help you think and reason about slicing and indexing.
a = "computational thinking" 0123456789012345678901 b = "duke university" 012345678901234 c = "python code" 01234567890
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.
In the following three examples, a higher fee is suppose to be assigned to a higher speed. For each of the following three choices, decide if the higher fee is assigned to the higher speed and if not explain why. For example, if the speed is 85, is the fee assigned always 60? The fees are 0, 20, 40, and 60 --- think of these as speeding fines, for example. Each example is supposed to work regardless of the value of variable speed.
# Example A speed = 85 fee = 0 if speed > 35: fee = 20 if speed > 50: fee = 40 if speed > 75: fee = 60 print fee # Example B speed = 85 fee = 0 if speed > 75: fee = 60 if speed > 50: fee = 40 if speed > 35: fee = 20 print fee # Example C speed = 85 if 35 < speed and speed <= 50: fee = 20 if 50 < speed and speed <= 75: fee = 40 if 75 < speed: fee = 60 print fee
To which of the following is this expression equivalent:
not ((x > y) and (y <= 3))
(x > y) and (y <= 3)
(x > y) or (y <= 3)
(x < y) or (y >= 3)
(x <= y) or (y > 3)
(x <= y) and (y > 3)
This means 1800, 1900, and 2100 are not leap years but 2000 and 2004 are leap years.
Which of the following implementations of a
method is_leap
returns true if year is a leap year and
false otherwise. For each one list whether or not it is correct.
If a method is not correct, provide a value for year for which it returns the wrong value.
A. def is_leap1 (year): if year % 400 == 0: return True if year % 100 == 0: return False if year % 4 == 0: return True return False B. def is_leap2 (year): return year % 400 == 0 or (year % 4 == 0 and year % 100 != 0) C. def is_leap3 (year): if year % 100 == 0: return False if year % 400 == 0 or year % 4 == 0: return True return False
all_same
that returns true only if all three of the given values are the same. For example, a call to all_same
with the values 3, 128, 255 should return False
; while the values 128, 128, 128 should return True
. def all_same (value1, value2, value3):
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.
Here is an example:
words = ["phone","book","baby","punch","fox","elf","country","sun"] for w in words: print w,pluralize(w)
You'll learn much more about the type list, a powerful Python type, a list is a collection of values between brackets [ and ]. The String method .split() creates a list by splitting a string into the values separated by whitespace. For example:
val = "this is the way the world ends" words = val.split() print len(val), len(words), words
Will generate the output:
30 7 ['this', 'is', 'the', 'way', 'the', 'world', 'ends']
phrase = "Do Re Me Fa So La Ti Do" phraseList = phrase.split() # note that phraseList is equal to ['Do', 'Re', 'Me', 'Fa', 'So', 'La', 'Ti', 'Do'] answer = "" for word in phraseList: if word[1] != 'a': answer = answer + word print answer
Explain what this code does.
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.
Describe briefly your plan in words for solving this APT.
Using Eclipse create a new python project (or use one project for all your APTs for this week.). Then create a python module with the name CreateAcronym.py (the .py extension will be automatically added). In that module you should create a function named acronym with the parameter indicated by the wording in the APT, reproduced below:
filename: CreateAcronym.py def acronym(phrase): """ return a string that is an acronym of the string parameter phrase """ # you write code here
Write code to create a list
variable that is the words in parameter phrase
.
Name the list variable words
. What would this line be?
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
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.