Compsci 6/101, Spring 2012, Lab 3

There are three parts to this lab:

Turn in this page for your group

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

Answer these questions on the handin sheet.
  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)


APT Acronym

Read the Acronym APT statement. Then with your group come up with a general plan for how to form the acronym of the parameter phrase --- your plan should be in English, not in code.

Answer these questions on the handin sheet.

Turn in this page for your group

  1. What string method returns a list that is the words represented by the string, e.g., as separated by spaces? Write code to create a list variable that is the words in parameter phrase using this method. Name the list variable words.

  2. What is a 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.

  3. Use the loop, explain how to index each word in the body of the loop to access the first character of the word, i.e., what's the Python code to do that and append the first character to the end of a string as shown below in the blank
         acro = ""
         for loop-you-wrote-above:
             acro = acro + ______________
         return acro
    


Bagel APT

For next week you'll work on the Bagels APT. Two solutions are given below from a previous semester. You'll be asked some questions about these solutions.

# solution A def bagelCount(orders): bagels = 0 for i in orders: if i < 12: bagels = bagels + i if i == 12: bagels = bagels + i + 1 if i > 12: bagels = bagels + i + (i/12) return bagel #solution B def bagelCount(orders): t = sum(orders) for int in orders: if int == 60: t = 5 + t elif int >= 48: t = 4 + t elif int >= 36: t = 3 + t elif int >= 24: t = 2 + t elif int >= 12: t = 1 + t else: t = t return t

  1. Explain whether the second two if statements in solution A can be replaced by elif and have the code still work correctly. Explain briefly why.

  2. Explain why the three if statements in solution A can be replaced by the last statement, e.g., so that the only statement in the loop body is bagels = bagels + i + (i/12)

  3. Explain why solution B isn't as general as solution A?

  4. Explain why int isn't a good name for the variable used in the for-loop of solution B.

  5. What could be improved in solution B?