Compsci 6, Fall 2011, Classwork 11

PRINT Names and NetID of students in group (min 2, max 3)
Name: __________________________   NetID: _____________ Name: __________________________   NetID: _____________

Name: __________________________   NetID: _____________

Problem 1:

The function sizes has a parameter named words that is a list of strings. This function returns a list of the sizes of each string. For example, sizes(['This', 'is', 'a', 'test']) should return the list [4, 2, 1, 4]

Does the following code work or not? If not, what is wrong with it?

def sizes(words): nums = [] for w in words: nums = len(w) return nums

Problem 2:

The function buildword has a parameter words that is a list of strings. This function returns a string that is made up of the first character from each word in the list. For example, buildword(['This', 'is', 'a', 'test']) returns 'Tiat'

Does the following code work or not? If not, what is wrong with it?

def buildword(words): answer = '' for w in words: answer += w[:1] return answer

Problem 3:

The function middle has a parameter names that is a list of strings, which each string in the format "firstname:middlename:lastname". This function returns a list of strings of the middlenames.

Does the following code work or not? If not, what is wrong with it?

def middle(names): middlelist = [] for name in names: name.split(":") middlelist.append(name[1]) return middlelist

Problem 4:

The function removeOs has one string parameter named names. This function returns a string equal to names but with all the lowercase o's removed.

Does the following code work or not? If not, what is wrong with it?

def removeOs(word): position = word.find("o") while position != -1: word = word[:position] + word[position+1:] return word