Turn in this page for your group
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)
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.
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.
string method | purpose |
---|---|
s.upper() | returns string upper case version of string s |
s.count(sub) | returns int number of (non-overlapping) occurrences of sub in s |
s.endswith(sub) | returns boolean depending on whether s ends with sub |
s.find(sub) | returns int: first index at which sub occurs in s or -1 if no occurrences |
s.split() | returns list of s split on whitespace |
s.split(sep) | returns list of s split on sep, a delimiter |
s.strip() | returns copy of s withOUT leading and trailing whitespace |
list method | purpose |
---|---|
lst.count(elt) | returns number of occurrences of elt in lst |
lst.index(elt) | returns first/least index at which elt occurs in lst, generates error if elt not in lst |
lst.append(elt) | append elt to end of lst, None returned |
Functions are applied to objects. For example, len
returns
an int: the length of a string or list or other
sequence/iterable. So len("apple")
is 5 and
len([1,2,3])
is 3.
Other functions for lists include: sum (returns total, an int, of elements in a list); max,min which return largest and smallest elements in a list; sorted which returns a sorted version of a list; reversed which returns a reversed version of a list.
For example:
function call | result returned | type returned |
---|---|---|
len([1,2,3,"apple"]) | 4 | int |
max([5,4,1,2,9,3]) | returns 9 | same as list elements |
max("ape", "bee", "zebra", "wildebeast"] | "zebra" | same as list elements |
min([5,4,1,2,9,3]) | returns 1 | same as list elements |
sorted([5,4,1,2,7]) | [1,2,4,5,7] | list |
reversed([1,2,3,6]) | [6,3,2,1] | list |
max
returns the maximal value of a string
as well as a list. What is max("science")
and why?
reversed(sorted([5,4,1,2,8]))
and
why?
sorted(lst).index(min(lst))
for
any list lst
? Why? What's the value if
min
is replaced by max
?
str.endswith(str[-3:])
True?
st.upper().endswith(st)
True?
(Provide a specific example of when it's true and try to generalize)
in
operator determines if its left operand occurs
in its right operand and returns a boolean value. This means
'a' in 'stranger'
evaluates to True. What
is the value of 5 in [1,2,3,4]
?
st[0:2] in st
is False?
(Provide a specific example of when it's true and try to generalize)
st[0:2]*2 in st
is True?
(Provide a specific example of when it's true and try to generalize)
lst.count(lst[0])
the same
as the value of len(lst)
for a list lst
?
(Provide a specific example of when it's true and try to generalize)
phrase
--- your plan should be in English, not in code.
Answer these questions on the handin sheet.
Turn in this page for your group
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