Quiz 3 Study Guide

Quiz 3 will be in class on Friday, October 27 at the start of class.

Python concepts

You are responsible for all material needed for Labs 1 through 5, which includes for loops, if statements, Boolean operators, relational operators, logic operators, while loops, functions, 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.

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

  • list data type

  • list manipulation (accumulators, indexing, etc)

  • mutable and immutable types

  • functions with returns and functions with side effects

  • while loops

Practice problems

  1. Show the output from the following code fragment:

    x = 0
    while x < 6:
          print("%d  %d" % (x, x*x))
          x = x + 1
    print("done!")
  2. Show the output from the following code fragment:

    text = "strings!"
    i = 0
    found = False
    while not found:
        if text[i] in 'aeiou':
            found = True
        else:
            print(text[i])
            i = i + 1
    print("found at position %d" % (i) )
  3. Consider the following program. Note that x % y returns the integer remainder when x is divided by y, and x // y does whole division, dropping any fractional component.

    x = 3
    i = 0
    print("%d  %d" % (0, x))
    while x != 1:
        if (x % 2) == 0:
            x = x // 2
        else:
            x = ( 3*x + 1 ) // 2
        print("%d  %d" % (i, x))
        i = i + 1
    1. Trace the program and show its output.

    2. Create a table having columns for i, and x and show how the values change as the loop executes.

  4. Given the program below:

    1  def find_min(a, b):
    2      min_value = a
    3      if b < a:
    4          min_value = b
    5      return min_value
    6
    7  def main():
    8      number_one = int(input("Enter the first number: "))
    9      number_two = int(input("Enter the second number: "))
    10     smaller = find_min(number_one, number_two)
    11     print("The smaller value is %d" % (smaller))
    12
    13 main()
    1. What are the function definitions?

    2. What are the function calls for functions defined within this program?

    3. Which functions are called that aren’t defined within this program?

    4. Give an example of a parameter and give an example of an argument.

    5. What would this program print if the user entered the following two numbers:

      • 4 and 5

      • 5 and 5

      • 9 and 2

  5. 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
  6. Write a program that reads in student grades from the user, until the user enters a -1. After reading in the -1, the program should compute and display the average grade.

    Please enter your grades below.  Enter a -1 when you are all done
    
    grade  1: 98
    grade  2: 87
    grade  3: 65
    grade  4: 95
    grade  5: 80
    grade  6: -1
    
    The average of those 5 grades is 85.000
  7. 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?

  8. 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]):
             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. [answer]

    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?

  9. Write a function called get_int that has two parameters, lo and hi. The function should prompt the user to enter an integer between lo and hi (inclusive), and then return the value entered. Your function should not return until the user enters a valid integer in the range lo to hi. You may assume both lo and hi are positive integers.

    Now add a main() function that calls get_int to get an integer from 1-10. Your main() should then print out "Boo!" that many times.

    Here’s an example run of the full program:

    Please enter an integer from 1-10: 99
    Please enter an integer from 1-10: -40
    Please enter an integer from 1-10: 4
    Boo!
    Boo!
    Boo!
    Boo!
  10. The following get_numbers(n) function doesn’t work. 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