[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 expressions is valid if value
is both a
string and a list, only for a string, only for a list or if it depends.
What is the value of x
after this code executes?
x = []
for i in range(3):
x.insert(0, len(x))
The first argument to insert
is the index to insert the second argument before. A value of 0 inserts items at the beginning of the list. The function call evaluates all of its arguments before invoking the function, so on the first iteration the list is empty (and thus the length is 0). Then during the next iteration, the length is 1, … Thus this code inserts 0, 1, and then 2 at the beginning of the list, i.e., the value of x
is [2, 1, 0]
.
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)
I typically teach two classes a semester. At the beginning of the semester I
want to find out how many students are enrolled in both classes. Assume that
I have two files containing the respective class rosters (one name per
line). Write a function named common_students
that takes the two files as
arguments and returns the number of students enrolled in both courses.
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