Quiz 3 Study Guide

You are responsible for all material needed for Lab 6, including graphics and lists.

Study guides are not graded. You may work with other students on the questions, and ask questions about the guide during evening ninja help sessions, or with meetings with faculty and staff. We do not provide full solutions to the study guides.

The study guide is not indicative of length, difficulty, or format of the quiz.

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

  • list data type

  • list manipulation (accumulators, indexing, etc)

  • functions with returns and functions with side effects, e.g. those that modify a list that is passed as an argument

  • accumulating lists using + and append

  • lists and strings as objects

  • passing lists to functions, and stack diagrams for functions that take lists as parameters

Sample practice problems

  1. Write a program which asks the user for 4 words and then prints them in reverse order. Hint: store them in a list using the append() method.

    Here’s an example run of the program:

    word 1: hello
    word 2: zebra
    word 3: unicorn
    word 4: corgi
    corgi
    unicorn
    zebra
    hello
  2. Analyze the following code:

    m = ["t","i","m","e"]
    s = "time"
    m[1] = "a"
    print(m)
    s[1] = "a"
    print(s)

    What happens when this code is executed? What, if any, output is shown?

  3. Consider the following program:

    def is_odd(number):
       # Q1: draw stack here (the first
       #     time the function is called)
       if (number % 2 == 0):
          return False
       else:
          return True
    
    def enigma(ls):
       total = 0
       for i in range(len(ls)):
          if is_odd(ls[i]) == True:
             ls[i] = ls[i]*2
          total = total + ls[i]
       return total
    
    def main():
       values = [5,3,4,8]
       print(values)
       result = enigma(values)
       print("result: %d" % result)
       print(values)
    
    main()
    1. Draw the function stack as it would look the first time is_odd() is called.

    2. What variables are in scope at this point in the code?

    3. What is the program’s output (i.e. what does it print when the program is run)?

    4. What does the enigma() function do?

  4. The following get_numbers(n) function doesn’t work correctly. Find and fix the bug in the code below.

    def get_numbers(n):
        """
        Purpose: Read n numbers from the user and return them as a list
        Parameters: n -- the number of values to read from the user (integer)
        Return: a list of the numbers entered by the user
        """
        for i in range(n):
            value_list = []
            value = int(input("Enter a number: "))
            value_list.append(value)
        return value_list
  5. Consider the following assignment statements in a Python program:

    letters = "abacab"
    words = ["we hope", "you had", "a nice", "Spring Break"]
    pt = Point(100, 200)
    circ = Circle(pt, 5)

    For each of the following, identify the type and value of the expression:

    1. len(letters)

    2. len(words)

    3. len(words[2])

    4. letters

    5. letters[1]

    6. words[1]

    7. pt.getX()

    8. circ.getRadius()

    9. circ.getCenter()

  6. Write a function draw_circles(points, win) that takes a list of Point objects as its parameter and a GraphWin object win and then, for each Point, draws a Circle with that Point as its center in the window win. Each Circle should be blue and have a radius of 10.

  7. Write a function upper(words) that takes a list of strings as its parameter and then converts each string in the list to uppercase.

    The function should not have any return value, but rather should modify the list of strings that is passed to it.

    For instance, the following code should print ['BEAR', 'CAT', 'DOG']:

    animals = ['BeaR', 'cat', 'DOg']
    upper(animals)
    print(animals)