Quiz 4 Study Guide

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

  • lists and strings as objects

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

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

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

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

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.

  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"]
        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()
  3. The following get_numbers(n) function doesn’t work. Find and fix the bug in the code below.

    def get_numbers(n):
        """
        Read n numbers from the user and return them as a list.
    
        Args:
            n (int):  the number of values to read from the user
    
        Returns:
            list: 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
  4. Assume you have these two functions already written:

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

      • "rock" if they enter r

      • "paper" if they enter p

      • "scissors" if they enter s

    • winner(p1, p2) returns:

      • "p1" if p1 won the game (e.g., p1=="rock", p2=="scissors")

      • "p2" if p2 won the game (e.g., p1=="rock", p2=="paper")

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

    Write a main() function that uses the above functions to play one round of rock-paper-scissors. The computer always randomly picks one of "rock", "paper", or "scissors". Here are a few sample runs of the program:

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

  6. 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, or s?: w
    please enter r, p, or s!!!
    r, p, or s?: zebra
    please enter r, p, or s!!!
    r, p, or s?: r
  7. Given the following variable initialization code, what is the type and value of each expression. Note: some expressions may be invalid. Write "Invalid" if the expression is invalid.

    s = "CS21 is awesome"
    lst = ["A", "November", "to remember"]
    n = "".join(lst)
    
                                  VALUE            TYPE
    
        lst
    
        "e" 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')
  8. Given a StudentRecord class with the following methods:

    StudentRecord(name, age, major, year): creates a new StudentRecord object
                  name: the student's name        (str)
                  age: the student's age          (int)
                  major: the student's major      (str)
                  year: the student's class year  (int)
    
    get_name(): returns the name value (str)
    get_age(): returns the age value (int)
    get_major(): returns the major value (str)
    get_year(): returns the class year value (int)

    And given the following variable initialization code:

    students = []
    student = StudentRecord("Jo", 22, "Physics", 2025)
    students.append(student)
    student = StudentRecord("Mo", 19, "CS", 2028)
    students.append(student)
    student = StudentRecord("Flo",20, "Math", 2026)
    students.append(student)

    What is the type of each expression? You do not need to provide the value. Write "Invalid" if the expression is invalid.

                                        TYPE
    
        student
    
        students
    
        students[0]
    
        students.get_age()
    
        students[0].get_major()
    
        len(students)
    
        len(student)
    
        len(students[2].get_name())
  9. 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 float_list and find_max:

    def main():
    
        infile = open("numbers.txt", "r")
        num_list = []
        for line in infile:
            line = line.strip()
            num_list.append(line)
    
        float_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 float_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()