Compsci 101, Fall 2017, Lab 4

MIX IT UP

For this lab you must work with a partner that you have not previously submitted a lab with.

The lab

In this lab you will:

You will need to snarf files for this lab. Those files are also here

Getting Credit for Lab 4

To get credit for lab, you will need to do the following by Sunday night:


Part 1: String and List Vocabulary

A method is a function invoked on an object. For example, the table below shows some string methods.

string methodpurpose
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

Vocabulary Questions

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.
  1. What do you think the name of the string method is that returns the lower case version of a string? (see the String methods above and generalize).

  2. The function max returns the maximal value of a string as well as a list. What is max("science") and why?

  3. What is the value of reversed(sorted([5,4,1,2,8])) and why?

  4. What is the value of sorted(lst).index(min(lst)) for any list lst? Why? What's the value if min is replaced by max?

  5. Why is the value of str.endswith(str[-3:]) True?

  6. When is the value of st.upper().endswith(st) True? (Provide a specific example of when it's true and try to generalize)

  7. The 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]?

  8. Explain when the value of st[0:2] in st is False? (Provide a specific example of when it's true and try to generalize)

  9. Explain when the value of st[0:2]*2 in st is True? (Provide a specific example of when it's true and try to generalize)

  10. When is the value of 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)


Part 2: Files

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:

  1. Look in eclipse to see how the files are stored for this project. Where is the Calculate.py and grades.txt with respect to folders in Eclipse?
  2. Explain how Calculate.py knows how to find the datafile grades.txt?
  3. What type does the function process return?
  4. Explain what the function process does?
  5. Modify the function 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.

  6. Write the function 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?
  7. How do you know you have the correct answer? Explain what type of datafile would be helpful to test your program on to give you confidence that your program works correctly.
  8. Write such a datafile and enter it here.
  9. Cut and paste your classAverage function here.
  10. Write the function 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?
  11. Cut and paste your maxGrade function here.
  12. Write the function 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?
  13. What is that number from 1991 to 1994?
  14. Cut and paste your howManyInRange function here.
  15. Write the function 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)?
  16. What are those names from 90 to 95 (cut and paste them in the form?
  17. Cut and paste your namesForGrades function here.