CPSC120B
Fundamentals of Computer Science I

Final Review

A practice test to review for the final exam.

  1. How would you write a program that allows for data to persist across executions of the program?

  2. For each of the following snippets of Python code, give what would be printed to the command line if run. If the snippet will not print anything because of an error, just put error.

    1. spam = {1: 3, 2: 4, 3: 2, 4: 1}
      print(spam[spam[1]])
              
    2. import string
      dictionary = {}
      
      for character_index in range(len(string.ascii_lowercase)):
          curr_character = string.ascii_lowercase[character_index]
          dictionary[curr_character] = character_index
      
      print(dictionary['e'])
      
    3. my_list = []
      curr_list = []
      for i in range(9):
          curr_list.append(i)
      
          if len(curr_list) == 3:
              my_list.append(curr_list)
              curr_list = []
      
      print(my_list)
              
    4. spam = [[-1, 0, 1], [0, 1, 2], [1, 2, 3]]
      eggs = 0
      for i in range(3):
          eggs += spam[i][i]
      print(eggs)
              
    5. in_file.txt:
        One Ring to rule them all,
        One Ring to find them,
        One Ring to bring them all,
        and in the darkness bind them.
      
      lotr_file = open("in_file.txt")
      for line in lotr_file:
          print(lotr_file.readline())
      

Write a function clamp_2d_list(a_2d_list, minimum_value, maximum_value), which takes 3 parameters. The first parameter is a 2-dimensional list (not necessarily square) of integers, and the final two parameters are also integers. Your function should return a 2 dimensional list of the same dimensions of a_2d_list, where each value in the 2-d list has been clamped between the specified minimum and maximum values.

    >>> my_list = [[-2, -1], [0, 1]]
    >>> print(clamp_2d_list(my_list, -1, 0))
    [[-1, -1], [0, 0]]
  


Write a function called remove_dups(a_list), which takes a two dimensional list of items as a parameter. This function should return a new list, which contains all of the unique items from the input list.

    >>> in_list = [0, 1, 2, 1, 2, 3]
    >>> print(remove_dups(a_list))
    [0, 1, 2, 3]
  


Write a function called generate_vigenere(), which takes 0 parameters. Your function should return a dictionary that represents a Vigenère square. Your function should generate this dictionary dynamically, as opposed to hard-coding the values. For example:

    >>> square = generate_vigenere()
    >>> for key in square:
    ...     print(key, "->", square[key])

    'a' -> ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
    'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
    'y', 'z']
    'c' -> ['c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
    'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
    'a', 'b']
    'b' -> ['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
    'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
    'z', 'a']
    'e' -> ['e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
    'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b',
    'c', 'd']
    'd' -> ['d', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
    'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a',
    'b', 'c']
    .
    .
    .