\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}

\setlength\columnsep{25pt}

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

\usepackage{listings}

\begin{document}

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

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

Directions: {\bf \Large Work only on this sheet} (on both sides, if
needed).  MAKE SURE TO COPY YOUR ANSWERS TO A SEPARATE SHEET FOR SENDING
ME AN ELECTRONIC COPY LATER.

{\Large \bf IMPORTANT NOTE:}  If you believe that nothing needs to be
placed into a blank, simply give {\bf Nothing} as your answer in your
file.  If you do not answer at all, put 00 in your file.

{\bf 1.} (70) The following Hadoop code finds the product {\bf ax},
where {\bf a} is a (presumably very large) matrix, and {\bf x} is a
column vector {\bf x}. 

The input matrix (read as {\bf stdin}) consists of {\bf a} together with
a prepended column of row numbers.  The vector {\bf x} is defined in the
file {\bf x.py}, via code in that file; {\bf x} is thus defined by
executing code in a file {\bf x.py}, via a Python {\bf import}
statement.

So, if the input matrix is

\begin{lstlisting}
0 1 2 0
1 5 8 -4
2 0 0 3
\end{lstlisting} 

and the contents of {\bf x.py} are

\begin{lstlisting}
x = [5,12,13]
\end{lstlisting}

then the final output will be

\begin{lstlisting}
29
69
39
\end{lstlisting}

{\bf Note:}  There are no row/element numbers in the final output.
Also, don't worry about leading blanks in the output.

Fill in the blanks.  You may find the Python {\bf len()} function
useful; it returns the length of a Python list (array), so that for
instance {\bf len(x)} is 3 in the above example.  Also, the {\bf int()}
function is like {\bf atoi()} in C.

{\bf axmap.py:} 

\begin{lstlisting}[numbers=left]
#!/usr/bin/env python

from x import x  # x is input here from file x.py
import sys

for line in sys.stdin:
   tks = line.split() 
   rownum = tks[0]
   row = tks[1:]
   sum = 0
   for i in range(BLANKa):
      sum += BLANKb
   print BLANKc
\end{lstlisting}

{\bf axred.py:} 

\begin{lstlisting}[numbers=left]
#!/usr/bin/env python

import sys

for line in sys.stdin:
    line = line.strip()
    tks = line.split('\t') 
    print BLANKd
\end{lstlisting}

{\bf 2.} (30) Fill in the blanks in the Snow code below, which finds the
unique elements of an array in parallel.  The built-in R function {\bf
unique()} works like this:

\begin{lstlisting}[numbers=left]
> x <- sample(1:8,10,replace=T)
> x
 [1] 4 7 3 1 1 2 3 3 2 8
> unique(x)
[1] 4 7 3 1 2 8
\end{lstlisting}

Code:

\begin{lstlisting}[numbers=left]
# not claimed efficient, and 
# no guarantee of ordering in result

parunique <- function(cls,x) {
   parts <- clusterSplit(cls,1:length(x))
   xparts <- lapply(parts,function(part) x[part])
   tmp <- clusterApply(cls,xparts,BLANKa)
   tmp <- Reduce(BLANKb)
   BLANKc
}
\end{lstlisting}

\onecolumn

{\bf Solutions:}

{\bf 1.}

{\bf axmap.py:} 

\begin{lstlisting}[numbers=left]
#!/usr/bin/env python

from x import x  # input x from file x.py
import sys

for line in sys.stdin:
   tks = line.split() 
   rownum = tks[0]
   row = tks[1:]
   sum = 0
   for i in range(len(row)):
      sum += int(row[i]) * x[i]
   print '%s\t%s' % (rownum, sum)
\end{lstlisting}

{\bf axred.py:} 

\begin{lstlisting}[numbers=left]
#!/usr/bin/env python

import sys

for line in sys.stdin:
    line = line.strip()
    tks = line.split('\t') 
    print tks[1]
\end{lstlisting}

{\bf 2.}

\begin{lstlisting}[numbers=left]
# not claimed efficient, and no guarantee of ordering in result

parunique <- function(cls,x) {
   parts <- clusterSplit(cls,1:length(x))
   xparts <- lapply(parts,function(part) x[part])
   tmp <- clusterApply(cls,xparts,unique)
   tmp <- Reduce(c,tmp)
   unique(tmp)
}
\end{lstlisting}

\end{document}

