\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}
\usepackage{fancyvrb}
\usepackage{relsize}

\begin{document}

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

\textbf{Directions: Work only on this sheet (on both sides, if needed); do not
turn in any supplementary sheets of paper. There is actually plenty of room
for your answers, as long as you organize yourself BEFORE starting writing.
In order to get full credit, SHOW YOUR WORK.}

{\fbox {\parbox{6.5in}{{\bf 
Several problems will ask you to present a ``Pythonic'' way of
accomplishing a certain task.  The term, which is common in Python
circles, simply means taking advantage of Python's features.  Often
it will mean that the task can be accomplished in a single line.  It
does not mean that you are expected to always use the most esoteric
Python feature (and certainly not one which is outside the scope of our
course materials), but it does mean that you should make a reasonable
attempt to make good use of the language.
}
}}}

{\bf 1.}  (15) Write a Python code fragment which will create 5 files,
named {\bf a1} through {\bf a5}, i.e. open them for writing.

{\bf 2.}  (20) The code below implements the ideas discussed in class, by
which an integer could be converted to a 4-byte ``string'' to transmit
or store the integer compactly.  Fill in the missing blanks:

\begin{verbatim}
def inttostring(n):
   m = n
   out = ''
   for i in range(4):
      c = m % 256
      out += _____________
      m /= 256
   return out

def stringtoint(s):
   m = 0
   for i in range(4):
      m = ______________
   return m
\end{verbatim}

{\bf 3.}  (15) Look at the handout on conversion to/from Roman numerals.
Suppose {\bf romanNumeralMap} had been implemented as a dictionary
rather than as a tuple of tuples, so that for instance one element would
be {\tt 'M':1000}.  Show how the {\bf for} loop in {\bf toRoman()} would
be rewritten.

{\bf 4.}  (15) Suppose we have a list {\bf x} which we want to rotate,
i.e.  the new {\bf x[0]} will be the old {\bf x[1]}, the new {\bf x[1]}
will be the old {\bf x[2]}, ..., and the new {\bf x[len(x)-1]} will be the
old {\bf x[0]}.  Write Pythonic code to do this (it must be
single-statement code for full credit).

{\bf 5.}  (15) Suppose our source file {\bf x.py} defines functions {\bf
f}, {\bf g} and {\bf h}.  Elsewhere in the code, a call is to be made to
one of these functions (with no arguments), depending on what the user
puts on the command line.  For example, if the user types

\begin{verbatim}
python x.py g
\end{verbatim}

then the program will execute {\bf g}.  Write Pythonic code which makes
the call (must be single-line code for full credit).

{\bf 6.}  (20) TCP treats all bytes sent by a network node as collectively
comprising one long message.  In some applications, though, we wish to
send ``records'' of a given length, say 80 characters.  A function which
is supposed to read the next record would start with whatever bytes had
been ``leftover'' from before, and then read from the network until it
had obtained 80 bytes.  For such applications, we might write a subclass
of {\bf socket}.  It would have an instance variable named {\bf
incoming}, and an instance function named {\bf getrec()} with a single
argument, {\bf reclength}.  The function {\bf getrec()} would return a
string of length {\bf reclength} (or an empty string, if we are at the
end of the entire message), which it would obtain by first removing
bytes from {\bf incoming} and calling {\bf recv()} as necessary.  

Write the function {\bf getrec()}, in a {\it short, Pythonic} manner.

{\bf Solutions:}

{\bf 1.}

\begin{verbatim}
f = []
for i in range(1,5):
   f.append(open('a'+str(i),'w'))
\end{verbatim}

Note the need for the list here.  You could not, for example, keep
assigning the result of {\bf open()} to the same variable, which would
mean you could not perform any further operations on the files.

{\bf 2.}  

\begin{verbatim}
chr(c)
...
256*m+ord(s[3-i])
\end{verbatim}

{\bf 3.}  Change the {\tt for} to 

\begin{verbatim}
for numeral,integer in romanNumeral(Map.items())
\end{verbatim}

Note that this presumes that dictionary itmes are stored in the order in
which they are shown in the code which uses them.  This works on the
CSIF platform, but a more careful version would sort first.

{\bf 4.}  For example,

\begin{verbatim}
x = x[1:] + [x[0]]
\end{verbatim}

{\bf 5.}  The easiest and most straightforward way (given our course
materials) is

\begin{verbatim}
exec sys.argv[1]+'()'
\end{verbatim}

{\bf 6.}

\begin{verbatim}
def getrec(self,reclength):
   self.incoming = []  # not destructive, as NO characters will be left over 
                       # from the last call to this function, since we
                       # read until we get exactly reclength characters
   ncharsread = 0
   while 1:
      chunk = self.recv(reclength-ncharsread)
      if chunk == []: return []
      self.incoming += chunk
      ncharsread += len(chunk)
      if ncharsread == reclength:
         return self.incoming        
\end{verbatim}

\end{document}



