\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.} (30) The table below contains analog pairs between Python and R.
Fill in the blanks.

\begin{tabular}{|r|r|}
\hline
Python & R \\ \hline 
$>>>$ & $>$ \\ \hline 
list & vector \\ \hline 
lambda function & blank (a) \\ \hline 
dictionary & blank (b) \\ \hline 
map & blank (c) \\ \hline 
\end{tabular}

{\bf 2.} (15) Fill in the blank:

\begin{lstlisting}
> f <- function(x) x^2
> f
function(x) x^2
> print(f)
function(x) x^2
> p   blank       (f)
function(x) x^2
\end{lstlisting}

{\bf 3.} (55) A graph {\it adjacency matrix} consists of 0s and 1s, with
a 1 at element (i,j) meaning there is a link from i to j.  The function
{\bf haslinks(adj,target)} determines which vertices in the graph have
links to a given set of target vertices.  Here are examples:

\begin{lstlisting}
> m
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    0    1    0    1
[2,]    1    1    0    0    1
[3,]    1    0    0    1    1
[4,]    0    1    1    1    0
[5,]    1    1    0    1    1
> haslinks(m,c(1,4))
[1] 3 5
> haslinks(m,4)
[1] 3 4 5
> haslinks(m,1:2)
[1] 2 5
> haslinks(m,c(1,3,5))
[1] 1
\end{lstlisting}

In the first call, for instance, we ask which vertices have links to
both vertex 1 and vertex 4, and the function reports that vertices 3 and
5 (rows 3 and 5 in the matrix) have that property.

Fill in the blanks:

\begin{lstlisting}
haslinks <- function(adj,target) {
   canreachtarget <-
      function(outlinks) {
         which1s <- which( blank (a) )
         tmp <-  blank (b) (target,which1s)
         as.integer( blank (c) (tmp,target))
      }                         
   tmp1 <- apply( blank (d) )
   which(tmp1 == 1)             
}                               
\end{lstlisting}

\onecolumn
\newpage

{\bf Solutions:}

{\bf 1.a} anonymous function

{\bf 1.b} list

{\bf 1.c} apply

{\bf 2.} rint.function

{\bf 3.

\begin{lstlisting}
haslinks <- function(adj,target) {
   canreachtarget <-
      function(outlinks) {
         which1s <- which(outlinks == 1)
         tmp <- intersect(target,which1s)
         as.integer(setequal(tmp,target))
      }                         
   tmp1 <- apply(adj,1,canreachtarget)
   which(tmp1 == 1)             
}                               
\end{lstlisting}}

\end{document}

