\documentclass[twocolumn]{article}
% \documentclass{article}

\setlength{\oddsidemargin}{-0.5in}
\setlength{\evensidemargin}{-0.5in}
\setlength{\topmargin}{0.0in}
\setlength{\headheight}{0in}
\setlength{\headsep}{0in}
\setlength{\textwidth}{7.0in}
\setlength{\textheight}{9.5in}
\setlength{\parindent}{0in}
\setlength{\parskip}{0.05in}
\setlength{\columnseprule}{0.3pt}

\setlength\columnsep{25pt}

\usepackage{fancyvrb}
\usepackage{relsize}
\usepackage{hyperref}

\usepackage{listings}
\usepackage{amsmath}

\begin{document}

\lstset{xleftmargin=5mm,framexleftmargin=10mm,basicstyle=\footnotesize}

Name: \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

Directions: MAKE SURE TO COPY YOUR ANSWERS TO A SEPARATE SHEET FOR SENDING
ME AN ELECTRONIC COPY LATER.

{\bf 1.} (25) Consider the grading program example in Section 1.9.
Suppose an instructor perversely sets a rule that the {\it highest} few
quiz grades would be dropped.  State a \underline{single} line to be
changed in the existing code, and state what the new line would be.
For example, if you think that Line 32 should be changed to {\bf x =
88}, write your answer as

\begin{lstlisting}
32 x = 88
\end{lstlisting}

{\bf 2.} Consider each part of this problem as a separate,
independent but complete Python interactive session (minus output and
error messages, if any).  For each part, answer ``error'' or ``no error'',
respectively, depending on whether the an error message occurs from
execution of the code.

\begin{itemize}

\item [(a)] (5)
\begin{lstlisting}
>>> x = [[1,2,3],[5,12,13],[6,7]]  
\end{lstlisting}

\item [(b)] (5)
\begin{lstlisting}
>>> sqrt(9)  
\end{lstlisting}

\item [(c)] (5)
\begin{lstlisting}
>>> def sq(x):
...    return x*x
...
>>> y = sq
>>> y = y(3)
\end{lstlisting}

\item [(d)] (5) 
\begin{lstlisting}
>>> class x:
...    def __init__(self):
...       self.y = 8
... 
>>> a = x()
>>> a.z = 88
\end{lstlisting}

\item [(e)] (5) 

\begin{lstlisting}
>>> x = (1,2,3) 
>>> x.append(4)
\end{lstlisting}

\end{itemize}

{\bf 3.} (25) The function {\bf places()} below returns a Python list
of the instances of the character {\bf c} in the string {\bf s}.  For
example,

\begin{lstlisting}
>>> y
'abcdefaba'
>>> places(y,'a')
[0, 6, 8]
\end{lstlisting}

Fill in the blanks.

\begin{lstlisting}
def places(s,c):
   base = _____________________
   insts = ___________________
   while True:
      place = s[base:]_______________
      if place == -1: break
      insts.append(_________________)
      base = __________________
   return insts
\end{lstlisting}

{\bf 4.} (25) The built-in Python function {\bf map(f,sq)} (in its basic
form) calls the function {\bf f()} on each element of the sequence {\bf
sq}, returning a list consisting of the results.  

The function {\bf extractcol(j,m)} returns column {\bf j} of the matrix
{\bf m}, where we are defining a matrix to be a two-dimensional array
with the same number of elements in each row.

For instance,

\begin{lstlisting}
>>> m
[[2, 3], [7, 8], [0, 28]]
>>> extractcol(1,m)
[3, 8, 28]
\end{lstlisting}

Fill in the blanks.

\begin{lstlisting}
def extractcol(j,m):
   def getelt(r): return __________________
   return _____________________
\end{lstlisting}

{\bf Solutions:}

{\bf 1.}

\begin{lstlisting}
49  tmp = tmp[:(len(tmp) - ndrop)]
\end{lstlisting}

{\bf 2.}

Parts (b) and (e) produce error messages.

{\bf 3.}

\begin{lstlisting}
def places(s,c):
   base = 0
   insts = []
   while True:
      place = s[base:].find(c)
      if place == -1: break
      insts.append(base + place)
      base = base + place + 1
   return insts
\end{lstlisting}

{\bf 4.}

\begin{lstlisting}
def extractcol(j,m):
         def getelt(r): return r[j]
         return map(getelt,m)
\end{lstlisting}

\end{document}

