\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.} (10) Fill in the blank: The examples on p.30 show that in general
Python is not a \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ language.

{\bf 2.} Consider the grading program example, pp.22-23.

\begin{itemize}

\item [(a)] (10) Suppose there is an input line equal to line 13 in the
source code, for a student John Paul Jones (without the \#).  Then in
line 66, what value will {\bf lw} have?

\item [(b)] (10) The instructor has the option of discarding some 
quiz grades. Give the line number on which this is done.

\item [(c)] (10) What is the purpose of the -1 in line 81?

\end{itemize}

{\bf 3.} (30) A common operation on file names is to extract a {\it
suffix}, e.g. extracting '.c' from 'xyz.c'.  The rest of the file name
is typically called the {\it basename}.  The code below will return a
Python list consisting of the basename and the suffix.  Fill in the
blanks.

\begin{lstlisting}
def fnamesplit(fname):
   fnamesp = blank (a)
   suff = blank (b)
   bname = blank (c)
   return [bname,suff]
\end{lstlisting}

Examples:

\begin{lstlisting}
>>> fnamesplit('abcde.f')
['abcde', '.f']
>>> fnamesplit('abcde.f.1.23')
['abcde.f.1', '.23']
\end{lstlisting}

{\bf 4.} (30) Consider the vending machine value on p.28. Every
time we add stock for new type of item, we will need to update our data.
The code in the book needs to be modified for this, as shown below.
Fill in the blanks:

\begin{lstlisting}
def newstock(self,newitems):
   for itm in newitems.keys():
      blank (a):
         self.inventory[itm] += newitems[itm]
      blank (b):
         blank (c).append(itm)
         blank (d)
\end{lstlisting}

Example:

\begin{lstlisting}
>>> m1 = machine()
>>> machine.itemnames
[]
>>> m1.newstock({'Kit Kat':5,'Sun Chips':8})
>>> machine.itemnames
['Sun Chips', 'Kit Kat']
>>> m1.inventory['Sun Chips']
8
>>> m1.newstock({'Fritos':2,'Sun Chips':8})
>>> m1.inventory
{'Sun Chips': 16, 'Fritos': 2, 'Kit Kat': 5}
>>> m2 = machine()
>>> m2.inventory
{'Sun Chips': 0, 'Fritos': 0, 'Kit Kat': 0}
\end{lstlisting}

\newpage
\onecolumn

{\bf Solutions:}

{\bf 1.} functional

{\bf 2.a} 15

{\bf 2.b} 53

{\bf 2.c} This deletes the EOL charactter, which would be make an extre
line print out.

{\bf 3.}

\begin{lstlisting}
def fnamesplit(fname):
   fnamesp = fname.split('.')
   suff = '.' + fnamesp[-1]
   bname = fname[0: (len(fname) - len(suff)) ]
   return [bname,suff]
\end{lstlisting}

{\bf 4.}

\begin{lstlisting}
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():
         # as alt approach, could do something like
         # if itm in self.inventory...
         try:
            self.inventory[itm] += newitems[itm]
         except:
            machine.itemnames.append(itm)
            self.inventory[itm] = newitems[itm]


\end{lstlisting}

\end{document} 

def fnamesplit(fname):
   fnamesp = fname.split('.')
   suff = '.' + fnamesp[-1]
   bname = fname[0: (len(fname) - len(suff)) ]
   return [bname,suff]

