For this lab you must work with a partner that you have not previously submitted a lab with.
You will need to snarf files for this lab. Those files are also here
To get credit for lab, you will need to do the following by Sunday night:
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 |
For each of the following questions, keep in mind that the variable in the question could represent a string or a list.
Answer these questions on the online form.
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)
Look at the file Calculate.py and the datafile grades.txt (you can snarf the lab with the file or click these links to see them).
Each line in the datafile has three parts of information about a person, with each part separated by a semi-colon: name;grade;yearBorn.
For example, some of the data lines are:
Blazing, Megan;87;1997 Ying, Xiaobai;83;1999 Acosta, Mary Lou;96;1994 Hoover, Alina;73;1996 Goff, Frank James;66;1996 ... Rest of data not shown ...
Answer the following questions in the google form:
Calculate.py
and grades.txt
with respect to folders in Eclipse?
Calculate.py
knows how to find the datafile grades.txt
?
process
return?
process
does?
process
so that it returns a list of
lists, where each list is the data split up into three strings, so it looks like:
[ ['Blazing, Megan', '87', '1997'], ['Ying, Xiaobai', '83', '1999'], ['Acosta, Mary Lou', '96', '1994'], ...]
Cut and paste the resulting function in the form.
classAverage
that returns the average grade of all the
people in the file. You'll need to uncomment out the call to
classAverage
in the main part of the program. That call will show you how
many parameters you should use. What is the average decimal value
calculated using the file grades.txt
?
classAverage
function here.
maxGrade
that returns the highest grade
of all the
people in the file. You'll need to uncomment out the call to
maxGrade
in the main part of the program. That call will show you how
many parameters you should use. What is the max grade
from the file grades.txt
?
maxGrade
function here.
howManyInRange
that returns the number of people in the
given file that have a birth date in the range year1
to year2
including
year1
and year2
. This function can assume that year1
is less than or equal
to year2
. What is that number from 1995
to 1997
?
1991
to 1994
?
howManyInRange
function here.
namesForGrades
that returns a list of names
from the file that have a grade from grade1
to grade2
including
grade1
and grade2
. This function can assume that grade1
is less than or equal
to grade2
. What are those names for grades 80
to 89
(cut and paste them in the form)?
90
to 95
(cut and paste
them in the form?
namesForGrades
function here.