Compsci 101, Fall 2016, 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. 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?
  6. 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.
  7. Write such a datafile and enter it here.
  8. Cut and paste your classAverage function here.
  9. 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?
  10. What is that number from 1991 to 1994?
  11. Cut and paste your howManyInRange function here.

Part 3: APT and loop practice(optional, if time)

This part is optional and does not have to be turned in if you don't have time for it in lab. Completing it may help you solve the APT.

You'll work on loops and strategies to solve one APT: ScoreIt which is part of the APT 3 group of APTs, so you can snarf them for testing.

  1. What value should maxPoints return for the call maxPoints([2,2,4,5,4]) and why?

  2. The number of occurrences of 1 in a list toss is given by the expression toss.count(1) --- what expression gives the number of 2's that occur in the list toss?

  3. The code below sets variable best correctly to be the best score for a list toss if the only values in the list are 2 or 4. Explain in words why the second if CANNOT be an elif --- provide an example that would fail to set best correctly for a list of only 2's and 4's if an elif statement is used (explain your reasoning). best = 0 if 2*toss.count(2) > best: best = 2 * toss.count(2) if 4*toss.count(4) > best: best = 4 * toss.count(4)
  4. How do you generalize the code above by using a for loop and a loop variable roll ? for roll in [1,2,3,4,5,6]:

After answering the questions together, you can individually work on completing this APT.