Skip to main content

Section 11.17 Assessment: Lists 2

Subgoals for Evaluating Lists.

  1. Declaring and initializing a list
    1. Set up a one dimensional table (i.e., one row) with 0 to size - 1 elements.
    2. Upon instantiation of a list, all elements contain values from the initializer list (i.e., the list of values inside the square brackets). If no initializer list is provided, the list is empty.
  2. Determining access, slicing, changing, adding, or whole list actions.
    1. Determine the list name and the action to be performed.
    2. If the list is on the left hand side of an assignment statement WITHOUT square brackets, it is a Whole List Action for List Assignment.
    3. If the list is on the left hand side of an assignment statement WITH square brackets, it is a Changing value of a List Element.
    4. If the append or insert method is being used on an instance of a list, it is an Adding a Value to a List Element.
    5. If the list is an expression WITHOUT square brackets, it is a Whole List Action for Passing a List as an Argument.
    6. If the list is an expression WITH square brackets, then check if there is a colon in the square brackets. If there is a colon inside of the square brackets, it is a Slicing Multiple Values from a List. Otherwise, it is an Accessing List Element.
  3. Accessing list element
    1. Determine value of index for element to be accessed; a positive value if counting from the beginning, or a negative value if counting from the end.
    2. listName[index] returns value stored at that index.
    3. Index must be between 0 and len(listName)-1, inclusive, or a negative value; otherwise an IndexError exception occurs at runtime.
  4. Slicing multiple values from a list
    1. Determine the range of indexes for the elements to be sliced
    2. listName[startIndex:endIndex] returns a new list containing the elements from startIndex to endIndex-1 (inclusive)
    3. Negative numbers can be used for startIndex and endIndex to count from the end of the list
    4. Omitting startIndex starts from the beginning of the list, and omitting endIndex goes to the end of the list
  5. Changing value of a list element
    1. Evaluate expression within [] brackets to determine the index of the element to be changed, and the list to change.
      Determine value of index for element to be changed; a positive value if counting from the beginning, or a negative value if counting from the end.
    2. Determine the expression of RHS (right-hand side) of the assignment statement.
    3. The lists’ value is now changed to match the value calculated from the RHS of the assignment statement.
  6. Adding a value to a list element
    1. Check whether the append or insert method is being used. Note that either way you do not use an assignment statement, it is just a method call.
    2. If append is used, the new value is added to the end of the list.
    3. If adding a value elsewhere in the list, use the insert method to add the new value at the specified index. Existing values starting from that index are shifted to the right.
  7. Traversing a List
    1. Determine the list that is being iterated over. If the expression also involves a range(len(list_name)), then the list is being traversed by index. The range function can either take one argument (the length of the list) or 3 arguments (the starting index, ending index, and step size). If the range function takes one argument, it will start at 0 and go to the length of the list - 1. If the range function takes 3 arguments, it will start at the first argument and go to the second argument - 1, incrementing by the third argument. Otherwise, if range is not used, then the list is being traversed by value.
    2. Determine the loop control variable that is being used to iterate over the list. The loop control variable will take on each value or index in the list, one at a time, depending on whether we are iterating by value or by index.
    3. The loop control variable is used to access the list element in the body of the loop. If iterating by index, the loop control variable is used as an index to access or update the list element. If iterating by value, the loop control variable is used directly to access the list element (no updates are possible).
    4. The list can also be added to with the append or insert methods. The append method adds a new value to the end of the list, while the insert method adds a new value at the specified index. Existing values starting from that index are shifted to the right.
  8. Whole list actions
    1. Passing a list as an argument
      1. Determine that the entire list must be passed as an argument to a method by consulting documentation.
      2. When calling a function, put variable name that represents the list as an argument in the method call. Remember that when passing a list as an argument that changes made by the function to the list are persistent. The list itself is not copied, so the function does not have its own copy of the list. However, the one exception to this is if you assign the argument to reference a different list in memory; then you will no longer be modifying the original list.
    2. List Assignment
      1. Determine that the reference to the list needs to be changed, not just its contents.
      2. The LHS of the assignment is the list reference needing to be changed.
      3. The RHS of the assignment is the new list reference.

Exercises Exercises

    1.

      Q1: The following code is intended to store the sum of all the values in the integer list arr in the variable total. Which of the following code segments can be used to replace # missing code/ so that the code works as intended?
      total = 0
      # missing code
      print(total)
      
      # I.
      for pos in range(len(arr)):
          total += arr[pos]
      # ----
      # II.
      for pos in range(len(arr), 0, -1):
          total += arr[pos]
      # ----
      # III.
      pos = 0
      while pos < len(arr):
          total += arr[pos]
          pos += 1
      
    • I only
    • II only
    • III only
    • I and III only
    • II and III only

    2.

      Q2: Assuming that nums has been declared and initialized as a list of integer values, which of the following best describes what this code does?
      index = 0
      count = 0
      m = -1
      for outer in range(len(nums)):
          count = 0
          for inner in range(outer+1, len(nums)):
              if nums[outer] == nums[inner]:
                  count += 1
          if count > m:
              index = outer
              m = count
      print(index)
      
    • Prints the maximum value that occurs in the list nums
    • Prints the index of the maximum value that occurs in the list nums
    • Prints the number of times that the maximum value occurs in the list nums
    • Prints the value that occurs most often in the list nums
    • Prints the index of the value that occurs the most often in the list nums

    3.

      Q3: The following code is intended to store the largest value in the integer list arr in the variable max_val. Which of the following best describes the conditions under which the code will not work as intended?
      max_val = 0
      for val in arr:
          if val > max_val:
              max_val = val
      
    • The largest value in arr occurs only once and is in arr[0].
    • The largest value in arr occurs only once and is in arr[len(arr)-1].
    • The largest value in arr is negative.
    • The largest value in arr is 0.
    • The largest value in arr occurs more than once.

    4.

      Q4: The following code segments are supposed to find the maximum value in a list of integers. Assuming that the list arr has been declared and contains integer values, which of the following code segments will correctly assign the maximum value in the list to the variable max?
      # I.
      max = 100
      for value in arr:
          if max < value:
              max = value
      # ----
      # II.
      max = 0
      first = True
      for value in arr:
          if first:
              max = value
              first = False
          elif max < value:
              max = value
      # ----
      # III.
      max = arr[0]
      for k in range(1, len(arr)):
          if max < arr[k]:
              max = arr[k]
      
    • I only
    • II only
    • III only
    • II and III
    • I, II, and III

    5.

      Q5: The following function is intended to return the index of the first occurrence of the value val beyond the position start in the list arr.
      # returns index of first occurrence of val in arr after
      # position start;
      # returns len(arr) if val is not found
      def find_next(arr, val, start):
          pos = start + 1
          while (CONDITION):
              pos += 1
          return pos
      
      For example, the execution of the following code segment should result in the value 4 being printed:
      arr = [11, 22, 100, 33, 100, 11, 44, 100]
      print(find_next(arr, 100, 2))
      
      Which of the following expressions could be used to replace (CONDITION) so that find_next will work as intended?
    • (pos < len(arr)) and (arr[pos] != val)
    • (arr[pos] != val) and (pos < len(arr))
    • (pos < len(arr)) or (arr[pos] != val)
    • (arr[pos] == val) and (pos < len(arr))
    • (pos < len(arr)) or (arr[pos] == val)
You have attempted 1 of 6 activities on this page.