Turn in this page for your group
There are three parts to this lab:
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?
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?
st[0:2]*2 in st
is True?
lst.count(lst[0])
the same
as the value of len(lst)
for a list lst
?
print [x*2 for x in range(0,10)] [0,2,4,6,8,10,12,14,16,18] print [s[0] for s in ['apple', 'termite', 'elephant']] ['a','t','e'] print [n % 2 == 0 for n in [2,4,6,1,3,5,8]] [True,True,True,False,False,False,True] print [n for n in [2,4,6,1,3,5,8] if n % 2 == 0] [2,4,6,8]Without typing on any computer, (so by thinking) show what each line below prints. Try to reason about the code shown. Write your answers on the turn in sheet for lab.
print [x**2 for x in range(1,10,2)]
print [s[1] for s in ["big", "brown", "bare", "stem", "pea"]]
print ['po'*i for i in range(0,5)]
print [i*i for i in range(1,10) if i % 2 == 0]
print sum([1 for p in [1,2,3,4,5,6,7,8]])
print sum([1 for x in "ostentatiously" if "aeiou".find(x) > -1])
sum
that returns the sum of all the odd integers
in a list of integers named num
-- so for [1,3,2,4,5] your
expression should evaluate 9 --- fill in the parentheses:
sum(...)
.
sum
and len
write an expression that returns
the average length of all the strings in a list of strings
named strs
. So you'll
call two list functions: one with strs
as an
argument
and one with a list comprehension using strs
as an
argument: combining in one expression
sum(...)
and len(...)
Some of these APTs can be solved using list comprehensions by transforming the data for the problem. Other APTs are probably better solved by modifying the data.
This week you worked on the Bagels APT. Four student solutions are shown below, you'll be asked some vocabularly questions and some analysis questions about these solutions.
|
|
|
|
orders
rather than accessing the values directly? How do
you know?
bagelCount
?
for
loop consists of one assignment statement to
bagels
-- i.e., so that one of the three assignment
statements in the code is used and no others are used, and no if
statments are used?
orders
so that the function
bagelCount
can be written as below, where you fill
in the blank with an expression that uses variable b
:
First consider this APT: Yahtzee/Scoreit
maxPoints
return for the list [4,3,3,5,2]
? Why?
count
that evaluates to the number of occurrences
of the value 5 in the list toss
. How does the expression
change to evaluate to the number of occurrences of the value 3? 6?
toss
. For example, the list comprehension should create
the list [0,1,2,0,2,0]
for toss = [3,2,5,3,5]
.
Think about how you are transforming a list of dice rolls to a list of the number of occurrences of each possible die-roll.
toss = [5,3,5,3,3]
the list comprehension should create [0,0,9,0,10,0]
(why?)
maxPoints
using only list comprehensions
and list functions, e.g., using a transformative approach.
occs
(occurrences) values that
represent the number of occurrences
of each value 0,1,2,...,100 -- in that order-- in the
list scores
. The list created will have 101 values. You
should
use the expression range(0,101)
and the list method count
.
For example, if scores = [2,2,2,1,100]
the list
comprehension
should create occs = [0,1,3,0,0,...,0,1]
(why?)
max
, sum
, or len
and the variable
occs
--- store the maximal number of occurrences of
the mode in a variable named mode
.
findMode
. This list should use
for i in range(0,101)
and an if statement that uses values
of
occs
and the value you stored in mode
?