CS21: Quiz 4 Study Guide

In addition to the concepts below, you should also know the concepts from Quiz 1, Quiz 2, and Quiz 3. Many of the earlier concepts -- especially functions -- are fundamental to what we've been studying recently.

You should be able to define the following terms:

You should understand and be able to use the following Python concepts:

Practice problems:

  1. Write a while loop that prints out all even numbers from 2 to 20.
  2. Write a program that reads in the following data from a file and computes the average value:
    98
    100
    91
    82
    88
    86
    77
    
  3. Given any specific definite (for) loop, write an indefinite (while) loop that performs the same computation.
  4. Discussion question 3 on page 261
  5. Write a function called getLetter() that asks the user for a letter and returns the given letter. Your function should check to make sure the user enters just a single letter. If the user enters anything other than a single letter, the function should ask again, until it receives valid input.
  6. Given the following:
    L = ["There are", "many", "like it,", "but this one", "is mine."]
    s = "abcdefg"
    
    what is the value and type of each expression? Try them in python to check your answers.
    (1) len(L)
    (2) len(s)
    (3) L[3].split()
    (4) "a" in L[2]
    (5) "DEF" in s
    
  7. Trace through the following code, show the output from its entire execution, and draw the stack before the return statement in function foo:
    def main():                     OUTPUT                       STACK
       print "in main"
       b = foo(5)
       print len(b)
       print b[0]
       print b[1][1]
    
    def foo(n):
      print "in foo"
      biglist = []
      for i in range (n):
        sublist = ["h", i, i+10]
        biglist.append(sublist)
      # draw stack here
      return biglist
    
    main()
    
    What is the type and value of each of the following expressions:
                          value                    type
    (1) biglist
    (2) biglist[2]
    (3) biglist[2][0]
    (4) biglist[1][2]
    (5) len(biglist)
    (6) len(biglist[1])
    
  8. Write a program that reads in a list of words from a file infile.txt into a list of strings, and then calls a function (you write) MakeAcronym that takes the list and returns a string that is the acronym of all words in the list and prints it out. For example, if the input file is:
    Thanks
    Goodness
    It's
    Friday
    
    Your program will print out the string "TGIF"