
\documentclass[twocolumn]{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}
\usepackage{hyperref}

\usepackage{listings}

\begin{document}

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

Directions: {\bf 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.

{\bf 1.}  The function {\bf wrmat()} inputs a matrix and a file name,
and outputs the matrix to a text file.  For example, the call
{\bf wrmat([ [1,2,3], [5,12,13] ],'outmat.txt')} would produce the text
file {\bf outmat.txt} with contents

\begin{lstlisting}
1 2 3
5 12 13
\end{lstlisting}

Fill in the blanks.

\begin{lstlisting}
def wrmat(mat,tf):
   f = open(tf,'w')
   for row in mat:
      # insert 1 or more lines 
      f.write(outrow+'\n')
   f.close
\end{lstlisting}

{\bf 2.}  The function {\bf primefact()} below finds the prime
factorization of number, relative to the given primes.  For example, the
call {\bf primefact([2,3,5,7,11],24)} would return [ 2,3], [3,1] ],
meaning that $24 = 2^3 3^1$.  (It is assumed that the prime
factorization of {\bf n} does indeed exist for the numbers in {\bf
primes}.)  Fill in the blanks.

\begin{lstlisting}
# find the maximal power of p 
# that evenly divides m
def dividetomax(p,m):
   k = 0
   while True:
      if m % p != 0: # blank
      k += 1
      m /= p

def primefact(primes,n):
   tmp = map( # blank
   tmp = filter( # blank
   return tmp
\end{lstlisting}

{\bf Solutions:} 

{\bf 1.}

\begin{lstlisting}
def wrmat(mat,tf): 
   f = open(tf,'w')
   for row in mat:
      outrow = ''
      for elt in row:
         outrow += str(elt) + ' '
      f.write(outrow+'\n')
   f.close
\end{lstlisting}

{\bf 2.}

\begin{lstlisting}
# find the maximal power of p 
# that evenly divides m
def dividetomax(p,m):
   k = 0
   while True:
      if m % p != 0: return (p,k)
      k += 1
      m /= p

def primefact(primes,n):
   tmp = map(dividetomax,primes,len(primes)*[n])
   tmp = filter(lambda u: u[1] > 0,tmp)
   return tmp
\end{lstlisting}

\end{document}

