Practice Problems - Set 2

These problems are for practice. They are optional, but you are encouraged to complete them and discuss them with members of the course staff during lab and office hours.

Some of these problems will appear on the final exam, and others are very similar to final exam problems. However, not all final exam problems will be telegraphed.

Problem 0

What is the printed output of this program?

def swap_ends(L):
  temp = L[0]
  L[0] = L[len(L)-1]
  print(L)
  L[len(L)-1] = temp
  
M = ["Costa Rica", "United States", "Canada", "Mexico"]
N = swap_ends(M)
print(N)

Problem 1

What is the printed output of this program?

def swap_ends(L):
  temp = L[0]
  L[0] = L[len(L)-1]
  L[len(L)-1] = temp
  return L

M = ["Montana", "Alaska", "Maryland", "Vermont"]
N = swap_ends(M)
print(N)

Problem 2

What is the printed output of this program? (The function is the same as in Problem 1)

def swap_ends(L):
  temp = L[0]
  L[0] = L[len(L)-1]
  L[len(L)-1] = temp
  return L

M = ["Annapolis", "Richmond", "Albany", "Boise"]
N = swap_ends(M)
print(M)

Problems 3-7 reference the function below.

def sum_ends(L):
  temp = L[0] + L[len(L)-1]
  L.append(temp)
  return L

Problem 3

What is the printed output of this program?

M = [22, 23, 24, 16]
N = sum_ends(M)
print(N)

Problem 4

What is the printed output of this program?

M = ["Atchafalaya", "Potomac", "Illinois", "Mississippi"]
N = sum_ends(M)
print(N)

Problem 5

Modify the sum_ends function so that instead of adding (or concatenating) the first and last elements of the input list, the function performs addition/concatenation on the first and second-to-last elements of the list.

With the modified function, the program below should have the output shown.

print(sum_ends([4, 6, 1, 7]))
print(sum_ends([5, 1, 2, 8]))
[4, 6, 1, 7, 5]
[5, 1, 2, 8, 7]

Problem 6

Modify the original sum_ends function so that it replaces the last element of the original list with the sum of the (original) first and last elements, and returns the modified list.

With the modified function, the program below should have the output shown.

print(sum_ends([4, 6, 1, 7]))
print(sum_ends([5, 1, 2, 8]))
[4, 6, 1, 11]
[5, 1, 2, 13]

Problem 7

Modify the original sum_ends function so that it returns the same thing as Problem 6, but have your function do so without modifying the original list.

With the modified function, the program below should have the output shown.

M = [4, 6, 1, 7]
print(sum_ends(M))
print(M)
[4, 6, 1, 11]
[4, 6, 1, 7]

Problem 8

This problem is substantially harder than any exam problem.

def character_checker(input_string):
  for i in range(65, 91):
    if chr(i) in input_string:
      return "String contains capital letters."
  for j in range(97, 122):
    if chr(j) in input_string:
      return "String contains lower case letters."

The above function is meant to:

  • Return "String contains capital letters." if the input string contains capital letters (but no lower case letters)
  • Return "String contains lower case letters." if the input string contains lower case letters (but no capital letters)
  • Return "String contains capital letters."\n"String contains lower case letters." if the input string contains both capital and lower case letters.

Example output should be:

print(character_checker('Hello')) yields

String contains capital letters.
String contains lower case letters.

print(character_checker('hello')) yields

String contains lower case letters.

print(character_checker('OK')) yields

String contains capital letters.

Fix the function so that it works as intended.

Problem 9

This problem is substantially harder than any exam problem.

The following program is intended to take as input as list input_list and a value value_to_remove and, if the value is present in the list at all, remove all instances of it.

def remove_value(input_list, value_to_remove):
    if value_to_remove in input_list:
        input_list.remove(value_to_remove)
    return input_list
  • remove_value([3, 4, 5, 3], 3) should return [4, 5]
  • remove_value(["the", "start", "of", "the"], "the") should return ['start', 'of']

Fix the function so that it works as intended.