Quiz 3 Study Guide

You are responsible for all material needed for Lab 5.

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 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. Write a function called get_number(lo, hi) that takes two integer arguments, lo and hi, and returns an integer value between lo and hi (inclusive). The function should repeatedly ask the user for a number until the user enters a number in the correct range. Write a main() function uses your get_number function to get a number between 1 and 10 from the user. The program should then print the number the user entered. A sample run of the program is shown below with user input in bold:

    Enter a number between 1 and 10: -1
    This number is out of range. Please try again
    Enter a number between 1 and 10: 22
    This number is out of range. Please try again
    Enter a number between 1 and 10: 7
    You picked: 7
  3. What would you need to change in your program above to get a number between 2 and 13 instead?

  4. 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?

  5. Consider the following program:

    def mystery(a, b):
        
        print("in mystery")
        result = b
        if a < b:
            result = a  + b
            b = b - 1
    
        # draw stack here
        print("a = %d" % a)
        print("b = %d" % b)
        return result 
    
    def main():
        print("in main")
        n = 3
        m = 5
        z = mystery(n, m)
        print("n = %d" % n)
        print("m = %d" % m)
        print("z = %d" % z)
    
    main()
    1. Draw the function stack as it would look before the call to mystery() returns.

    2. Show the output of the program when it is run.

  6. 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?

  7. 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
  8. Consider the following assignment statements in a Python program:

    letters = "abacab"
    words = ["we hope", "you had", "a nice", "Fall Break"]
    text = words[0]

    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[1]

    5. words[1]

    6. letters > words[2]

    7. text[0]

  9. 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)