Quiz 4 Study Guide

Quiz Study Guides are provided as a courtesy. You may work with other students on the questions, and ask questions about the guide during evening ninja help sessions, on EdSTEM, or during meetings with faculty and staff. We do not provide full solutions to the quiz guides.

You are responsible for all material covered through the end of Week 8.

In addition to all concepts from Quiz 3, you should understand the following:

Python concepts

  • accumulating lists using + and append

  • lists and strings as objects

  • string methods (e.g., split, strip, isdigit)

  • passing lists to functions

    • stack diagrams for functions that take lists as parameters

  • using the in operator

  • file I/O (e.g., open, reading the contents of a file line-by-line)

  • top-down design and function stubs

Practice problems

  1. Write a function called is_vowel(letter) that has one parameter, letter. This function should return True if the letter is a vowel (upper or lowercase), False if not. For example, calling is_vowel("i") should return True, and is_vowel("Q") should return False. (answer)

  2. For the following program, show the full output when the program is run to completion, and draw the stack as it would look just before the computer executes the return count statement.

    def main():
      words = ['roses', 'are', 'red']
      line = 'violets are blue'
      result = update(words, line)
    def update(word_lst, phrase):
      count = 0
      data = phrase.split() # splits on spaces with no parameters
      for item in data:
        if not (item in word_lst):
          count = count + 1
      # draw stack here
      return count
  3. Write a function called get_zipcode() that ask the user to enter their zip code. Be sure that the user only enters 5-digit numbers. Any other input should force the user to enter the value again. Once the user has entered a valid zipcode, return the value as a string.

  4. You are trying to implement the game tic-tac-toe. You will store the board as a list of 9 strings. When the game starts, each element of the board will be an empty string. Once a user plays in a spot on the board, the element will change to an X or an O. For example, after four turns, the board variable might be equal to this:

    ['', 'X', '', 'O', '', 'O', 'X', '', '']

    Assume that you have these functions already written for you:

    • get_space(board) asks the user to type which space they want to play in on the board and returns an integer representing the index in the list they want to play in. The get_space function only accepts valid, open spaces on the board. For example, given the board shown above, the get_space function would only allow the user to enter the values 0, 2, 4, 7, or 8.

    • winner(board) returns 'X' if X won the game, 'O' if O won the game, 'T' if there are no spaces left and nobody won, or '' if there are spaces left and nobody won.

    • display(board) prints the board to the screen. For example, given the board above, display(board) would print this:

         | X |
       O |   | O
       X |   |
      1. Write the stub for the winner(board) function.

      2. Write the implementation for the get_space(board) function.

      3. Extra practice if you have time: Write a main() function that uses the above functions to play the game of tic-tac-toe. Assume that X always goes first.

  5. Given the assignments shown below, what is the value and type of each expression?

    letters = "abacab"
    words = ["only two", "more weeks", "until", "Thanksgiving"]
    pt = Point(100, 200)
    circ = Circle(pt, 5)
                                     VALUE      |      TYPE
        len(letters)                            |
        len(words)                              |
        len(words[2])                           |
        "a" in words                            |
        "a" in words[3]                         |
        "a" in letters                          |
        words[1][1]                             |
        words[0].split(' ')                     |
        pt.getX()                               |
        pt.getY() == 200                        |
        circ.getRadius()                        |
        circ.getCenter()                        |
  6. Given an input file named "numbers.txt" that consists of some number of lines, each with a single value. For example, the first few lines of the file might look like:


    Complete the program below which should:

    • read in the values from the file and append them to a list

    • use the convert function (that you will write) to convert a list of strings into a new list of integers and returns that new list.

    • use the find_min function (that you will write) to find the smallest number in the list of integers.

      def main():
        infile = open("numbers.txt", "r")
        str_list = []
        # a. Read all of the numbers in the file into the list str_list
        # Each number should be stored as a string in the list.
        num_list = convert(str_list)
        small = find_min(num_list)
        print("Min value in list of %d values is %d" % (len(num_list), small))
      # b. Implement the convert function, which should take in a list of strings
      # and convert it to a list of ints. For now, you can assume all of the strings
      # can be converted to integers.
      # c. Implement the find_min function, which should look through a list of
      # integers and return the minimum value
      # d. Can you rewrite the convert function so that any string that is not made
      # up of only digits is not converted to an int and then not stored in the new
      # list? in this example, the number -63 would not be in the new list returned
      # by convert since "-" is not a digit.

      Running the program above without step (d) on the list of numbers shown should produce the output:

      Min value in list of 6 values is -63

      Once you implement step (d), the output should become:

      Min value in list of 5 values is 2