Practice Exercises 5 Solutions

  1. [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).

  2. 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.

    1. Both. It will either concatenate the list or the strings.
    2. Both. It will either produce a list with a string element, or list with another list as its element.
    3. Both. It will evaluate to the number of characters or the number of elements in the list.
    4. Both. It will slide the first 4 characters or first 4 list elements.
    5. Only a string. We can’t concatenate a string and list (even if the list only contains strings).

  3. Write docstrings for the following functions describing what each of them do:

    1. 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
      
    2. 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
      
    3. 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
      
  4. [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]
    
  5. [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)
    
  6. 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