Quiz 4 Study Guide
This page contains a list of things you should know before taking Quiz 4. If you have any questions as to the correct answers to any of these questions, please let me know.
Functions
Our recent discussions have relied more upon functions as a means of defining useful pieces of a program. You should understand how functions are called, how arguments are assigned to parameters, and how values are returned. Here are some especially important details you should know:
- A returnstatement ends a function immediately and returns the evaluation of the provided expression. This is true even if thereturnappears within a loop.
- A returnstatement can only end the function in which it appears.returnhas no special meaning for loops (other than the above behavior, which may indirectly stop a loop prematurely).
- Each function has its own variables; it is not possible to refer to one variable from inside of another function.
- Arguments are passed to parameters by position and not by name.
- Functions should not assign values to their parameters.
Lists and Functions
We have also used lists in multiple labs and discussed how lists interact with functions. Consider, for instance, the following code:
def extend_list(lst, x):
  lst.append(x)
def main():
  data = [1,2]
  extend_list(data, 4)
  print data
main()
When this program is executed, it prints [1, 2, 4].  Even though the data variable is not changed within the main function, the data variable is a reference to a list (rather than the list itself).  For more information, please see the textbook.
Exercises
Write the following Python functions:
- A function longestwhich finds and returns the longest string in a list. For instance,longest(["These","are","some","words","in","a","sentence"])would return"sentence".
- A function find_vowel_stringwhich finds and returns the first string in a list containing a vowel. For instance,find_vowel_string(["xz","ac","hi","bq"])would return"ac".
- A function squish_oddwhich replaces each odd number in a list of integers with the number zero. For instance, ifxrefers to the list[3,4,5], thensquish_odd(x)would returnNoneandxwould then refer to the same list containing[0,4,0].
- A function find_oddwhich determines the index of the first odd number in a list. For instance,find_odd([24,40,12,47,4,53,80,19])would return3.
- A function add_zeroeswhich adds a number of zeroes to the end of a list. For instance, ifxrefers to the list[2,4,6], thenadd_zeroes(x, 3)would returnNoneandxwould then refer to the same list containing[2,4,6,0,0,0].
Answer the following questions:
- What is a parameter? What is an argument?
- If a function has no parameters, which variables are in scope when it first begins to run?
- Suppose the function fhas parameters(x,y,z)and it is called from functiongwith arguments(x,z,y). If functionfassigns0to the variablez, what effect does this have on the variables ing?
- If a returnappears within awhile Trueloop, what happens?
Stack Diagrams
We have discussed the syntax of stack diagrams and you have used them in the written portion of a lab. You should be familiar with how we draw stack diagrams. For a small presentation on how stack diagrams follow the execution of a program, please see this presentation (which also appears on the course schedule).
Exercises
For the following code, draw a stack diagram for the first time that the program reaches each comment.
def extend(lst1, lst2):
  for x in lst2:
    lst1.append(x)
  # Stack diagram!
def main():
  data1 = [1,2,3]
  data2 = [4,5]
  # Stack diagram!
  extend(data1,data2)
  print data1
  print data2
main()
Debugging and Execution Tracing
As always, you should be able to find mistakes in programs and understand how programs execute. You should be prepared to work out how a program will run without the use of a computer.
Exercises
Work out by hand what each of the following programs will print.
#1
def collatz(n):
  if n % 2 == 0:
    return n / 2
  else:
    return n*3 + 1
def main():
  i = 6
  while i > 1:
    print i
    i = collatz(i)
main()
#2
def num_or_zero(s):
  try:
    x = int(s)
    print x
    return x
  except ValueError:
    return 0
def sum_string(s):
  acc = 0
  for c in s:
    acc += num_or_zero(c)
  return acc
def main():
  print "#%s" % sum_string("10 hot dogs and 8 buns")
  print "$%s" % sum_string("4 and 20 blackbirds")
main()