[PP Problem 8.4] The value of the variable ids
is the list [4353, 2314,
2956, 3382, 9362, 3900]
. Using list
methods,
do the following:
a. ids.remove(3382)
b. ids.index(9362)
Note that list
doesn’t have a find method.
c. ids.insert(ids.index(9362)+1, 4499)
d. ids.extend([5566, 1830])
e. ids.reverse()
f. ids.sort()
Recall that the last two methods are “in place”, that is they modify the
list on which they are invoked (and return None
).
Since strings and lists are both sequences, many Python expressions can be
applied to both string values and list values. For each of the expressions
below, determine whether the expression is valid both when value
is a string or a list; or only if value
is a string; or only if value
is a list.
Write docstrings for the following functions describing what each of them do:
Mystery Function 1
def mystery1(num1, num2):
"""
Create a list containing num2 repeated num1 times
Args:
num1: Length of the list to be created
num2: Value to populate the list
Returns:
List with num1 copies of num2
"""
result = []
for i in range(num1):
result.append(num2)
return result
Mystery Function 2
def mystery2(num1, num2):
"""
Create a list containing num2 repeated num1 times
Args:
num1: Length of the list to be created
num2: Value to populate the list
Returns:
List with num1 copies of num2
"""
return [num2]*num1
Mystery Function 3
def mystery3(a_list):
""""
Reverses the list parameter in place
Args:
a_list: Input list
""""
for i in range(len(a_list) // 2):
other_index = len(a_list)-i-1
temp = a_list[i]
a_list[i] = a_list[other_index]
a_list[other_index] = temp
[PP Problem 8.7] Write a function named same_first_last
that takes a list
as a parameter and returns True
if and only if the first and last element
of the list are equal. You can assume that the list has at least one
element.
def same_first_last(a_list):
return a_list[0] == a_list[-1]
[PP Section 8.8] You are experimenting with the following code to provide a
list of colors in sorted order, but you are getting an error when your for
loop executes. What is the problem and how would you fix it.
colors = 'red orange yellow green blue purple'.split()
sorted_colors = colors.sort()
for color in sorted_colors:
print(color)
Recall that sort
modifies the list in place, that is modifies the list on
which it is invoked, but doesn’t actually return anything (more precisely
it returns None
). Thus sorted_colors
is assigned None
, and you will
get a TypeError
when you try to use None
as the loop sequence. You can
fix by iterating through the modifed value of colors
.
colors = 'red orange yellow green blue purple'.split()
colors.sort()
for color in colors:
print(color)
A professor typically teaches two courses a semester. Suppose at
the beginning of the semester they want to find out how many
students are enrolled in both their courses. Assume they have
two files containing the respective class rosters. Write a function
named common_students
that takes the two files as arguments and
returns the number of students whose names appear on both rosters. Recall
that you can use the in
operator to test if a value is present in
a list.
The best way to approach this problem is to break it into two parts, 1) reading the files into lists, and 2) checking for shared membership. For the first we can write a helper function that reads the names into a list.
def read_names(filename):
with open(filename, "r") as file:
names = []
for name in file:
# Assume one name per line
names.append(name.strip())
return names
def common_students(filename1, filename2):
names1 = read_names(filename1)
names2 = read_names(filename2)
common = 0 # Use this as a counter of common students
for name in names1:
if name in names2:
common += 1 # Increment counter if name1 in other list of students
return common