Quiz 4 Study Guide

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

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

Python concepts

  • lists and strings as objects

  • list methods (e.g., append, count)

  • list of objects (e.g., list of StudentRecord objects)

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

  • top-down design

  • file I/O (e.g., open, readlines)

  • Searching: linear and binary search

Practice problems

  1. For each iteration of the loop in binarySearch(x, lst), show the index values for low, high, and mid, and the value stored at location mid. What will be returned by this function call?

    x = 67
    lst = [10, 12, 14, 21, 37, 44, 45, 46, 58, 67, 99]
    index 0   1   2   3   4   5   6   7   8   9  10
    
    low | high | mid | lst[mid]
    - - - - - - - - - - - - -
        |      |     |
        |      |     |
        |      |     |
        |      |     |
  2. For each iteration of the loop in binarySearch(y, lst), show the index values for low, high, and mid, and the value stored at location mid. What will be returned by this function call?

    y = 4
    lst = [10, 12, 14, 21, 37, 44, 45, 46, 58, 67, 99]
          0   1   2   3   4   5   6   7   8   9  10
    
    low | high | mid | lst[mid]
    - - - - - - - - - - - - - -
        |      |     |
        |      |     |
        |      |     |
        |      |     |
  3. Binary search is much faster than linear search, so why don’t we use it for every search problem?

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

  5. 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"]
      print(words)
      line = "violets are blue"
      result = update(words,line)
      print(result)
      print(words)
    
    ################
    def update(lst, s):
    
      count = 0
      data = s.split()
    
      for item in data:
        if item not in lst:
          lst.append(item)
          count = count + 1
    
      # draw stack here
      return count
    
    main()
  6. 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
  7. Assume you have these two functions already written:

    • get_pick() asks the user for "r,p,s?" and returns:

      • "rock" if they enter r

      • "paper" if they enter p

      • "scissors" if they enter s

    • winner(user,comp) returns:

      • "user" if user won the game (e.g., user="rock", comp="scissors")

      • "comp" if comp won the game (e.g., user="rock", comp="paper")

      • "tie" if it’s a tie game (e.g., user="rock", comp="rock")

    Write a main() function that uses the above functions to play one round of rock-paper-scissors. Here are a few sample runs of the program:

    $ python3 rps.py
    r,p,s?: r
    Computer chose rock
    tie...
    $ python3 rps.py
    r,p,s?: r
    Computer chose paper
    Computer wins!
    $ python3 rps.py
    r,p,s?: r
    Computer chose scissors
    You win!
  8. Write the stub for the winner(user, comp) function.

  9. Write the implementation for the get_pick() function. Your get_pick() function should only accept r, p, or s from the user. If they enter anything else, print an error message and ask again.

    r,p,s?: w
    please enter r, p, or s!!!
    r,p,s?: zebra
    please enter r, p, or s!!!
    r,p,s?: r
  10. Given the following variable initialization code, what is the type and value of each expression:

    s = "CS21 is awesome"
    lst = ["May", "the Force", "be", "with you."]
    n = "".join(lst)
    
                                  VALUE            TYPE
    
        lst
    
        "a" in lst[2]
    
        "cs" in s
    
        lst[1]
    
        s.split()
    
        lst.split()
    
        n
    
        s[0]
    
        lst[0]
    
        len(s)
    
        lst.count('e')
    
        s.count('e')
    
        lst[1].count('e')
  11. Given the StudentRecord class methods:

    StudentRecord(name, age, major, gpa): creates a new StudentRecord object
                  name: the studen't name    (string)
                  age: the student's age     (int)
                  major: the student's major (string)
                  gpa: the student's gpa     (float)
    
    get_age(): returns the age value (int)
    get_gpa(): returns the gpa value (float)
    get_major(): returns the major value (string)
    get_name(): returns the name value (string)

    And given the following variable initialization code:

    students = []
    stud = StudentRecord("Jo", 22, "Pysics", 3.4)
    students.append(stud)
    stud = StudentRecord("Mo",19, "CS", 3.2)
    students.append(stud)
    stud = StudentRecord("Flo",20, "Math", 3.3)
    students.append(stud)

    What is the type of each expression? Enter "Invalid" if the expression is invalid.

                                        TYPE
    
        stud
    
        students
    
        students[0]
    
        students.get_age()
    
        students[0].get_gpa()
    
        len(students)
    
        len(stud)
    
        len(students[2].get_name())
  12. Given an input file named "numbers.txt" that consists of some number of lines, each with a single float value. For example, the first few lines of the file might look like:

    4.5
    -0.5
    32.1
    -8
    54.1
    12.3

    Complete the program below to implement the functions fix_list and find_max:

    def main():
    
      infile = open("numbers.txt", "r")
      num_list = []
      for line in infile:
          line = line.strip()
          num_list.append(line)
    
      fix_list(num_list)
    
      max = find_max(num_list)
    
      print("Max value in list of %d values is %f" % (len(num_list), max))
      infile.close()
    
    ########################
    
    # implement the fix_list function, which should take in a list of strings
    # and convert it to a list of floats
    
    
    # implement the find_max function, which should look through a list
    # and return the maximum value
    
    ########################
    main()