\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.} This problem concerns the program {\bf psax.py}, which monitors
processes, in the {\bf curses} chapter of our book.  

\begin{itemize}

\item [(a)] (5) State the number of a line in which an instance variable
is accessed.  If there is no such line, write NONE.

\item [(b)] (10) State the number of a line in which a class variable
is accessed.  If there is no such line, write NONE.

\item [(c)] (10) State the number of a line in which a class method
is accessed.  If there is no such line, write NONE.

\item [(d)] (10) Suppose the file {\bf gy} consists of a single line with
contents \lstinline{'uuddkkrr\n'}.  State in a SINGLE, BRIEF line what
will happen.

\end{itemize}

{\bf 2.} (30) Consider the binary tree example, Section 1.20.  We will add
a new method {\bf max()} to the class {\bf treenode}.  Note since it is
a method rather than a freestanding function, it will not conflict with
the built-in Python function {\bf max()}, which works as follows:

\begin{lstlisting}
>>> max(12,5,13)
13
\end{lstlisting}

If {\bf z} is an object of the class {\bf treenode}, then

\begin{lstlisting}
z.max()
\end{lstlisting}

will return the maximum value in the tree rooted at {\bf z}.  

Example:

\begin{lstlisting}
>>> x = [12,5,13,10,8,6,28]
>>> tr = bintree.tree()
>>> for n in x:  tr.insrt(n)
>>> tr.root.max()
28
\end{lstlisting}

Fill in the blanks:

\begin{lstlisting}
def max(self):  
   s =    blank (a)
   if   blank (b)  : s =   blank (c)
   return s
\end{lstlisting}

{\bf 3.} (25) Here we will deal with a class representing a vending
machine.  Each object of this class represents one machine, but all the
machines carry the same items (though the current size of the stock 
of a given item may vary from machine to machine).

The {\bf inventory} variable will be a dictionary with keys being item
names and values being the current stocks of those items, e.g.\
\lstinline{'Kit Kat':8} signifying that this machine currently holds a
stock of 8 Kit Kat bars.  

The method {\bf newstock()} adds to the stocks of the given items; e.g.\
\lstinline{m.newstock({'Kit Kat':3,'Sun Chips':2}) would record that the
stocks of Kit Kat bars and bags of Sun Chips at machine {\bf m} have
been replenished by 3 bars and 2 bags, respectively.

Fill in the blanks:

\begin{lstlisting}
class machine:
   itemnames = []
   def __init__(self):
      self.inventory =   blank (a)
      for nm in   blank (b)  :
         self.inventory[nm] = 0
   def newstock(self,newitems):
      for itm in    blank (c)  :
         blank (d)  +=   blank (e)
\end{lstlisting}

{\bf 4.} (10) This is a continuation of Problem 3.  The following test of
the above code produces an error:

\begin{lstlisting}
>>> m = machine()
>>> machine.itemnames = ['a','b']
>>> m.newstock({'b':3})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 14, in newstock
KeyError: 'b'
\end{lstlisting}

State in a SINGLE, BRIEF line how to fix this test.

{\bf Solutions:}

{\bf 1.}  Note that {\bf os} and {\bf curses} are modules, not classes,
as can be seen by the fact that they are {\bf import}ed.

{\bf 1a.}  NONE

{\bf 1b.} any line containing ``gb.''

{\bf 1c.}  NONE

{\bf 1d.}  The {\bf psax.py} program itself would be killed!

{\bf 2a.}

\begin{lstlisting}

\end{lstlisting}self.value

{\bf 2b.}

\begin{lstlisting}
self.right != None
\end{lstlisting}

{\bf 2c.}

\begin{lstlisting}
self.right.max()
\end{lstlisting}

{\bf 3a.}

\begin{lstlisting}
{}
\end{lstlisting}

{\bf 3b.}

\begin{lstlisting}
machine.itemnames
\end{lstlisting}

{\bf 3c.}

\begin{lstlisting}
newitems.keys()
\end{lstlisting}

{\bf 3d.}

\begin{lstlisting}
self.inventory[itm]
\end{lstlisting}

{\bf 3e.}

\begin{lstlisting}
newitems[itm]
\end{lstlisting}

{\bf 4.} Swap the first two lines.

\end{document}

def max(self):  # no name conflict
   s = self.value
   if self.right != None: s = max(s,self.right.max())
   return s

class machine:
   itemnames = []
   def __init__(self):
      # in (itemname, stock) form
      self.inventory = {}
      for nm in machine.itemnames:
         self.inventory[nm] = 0
   # adds the new stock to inventory; items is in dictionary form,
   # (itemname, newstock form)
   def newstock(self,newitems):
      for itm in newitems.keys():
         self.inventory[itm] += newitems[itm]

# wrong
m = machine()
machine.itemnames = ['a','b']
m.newstock({'b':3})

# right
machine.itemnames = ['a','b']
m = machine()
m.newstock({'b':3})

