\documentclass{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[margin=1in]{geometry}
\usepackage{fancyvrb}
\usepackage{relsize}
\usepackage{hyperref}

\usepackage{listings}
\usepackage{amsmath}

\begin{document}

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

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

\bigskip

{\bf \Huge Directions:}

\begin{quote}

Use OMSI for this quiz.  Note the following:

\begin{itemize}

\item Your OMSI window must fill your laptop screen at all times.  Use
of any Internet communication other than intended OMSI functions is not
allowed.

\item For coding problems, you will be given a test function, which you
will copy-and-paste into your Answers window, together with the
function(s) you write for the problem.  When you hit Run, OMSI will run
what is in your window.  (Do NOT insert a line {\bf if \_\_name\_\_ ...}.)

\item You must hit Save for all problems.

\item For noncoding problems, do not hit Run.

\item If you wish, you may take a picture of your screen during the last
60 seconds of the quiz, or within 60 seconds of your leaving the room if
you do so early.

\end{itemize}

\end{quote}

\vspace{1cm} 

{\bf 1.} (100) In this problem, you will write a function \texttt{countinst(startdir,flname)} that searches the directory tree rooted at \texttt{startdir}, and returns the count of all instances of files with the name \texttt{flname}. Both directories
and ordinary files with that name are included in the count.

If you wish, you may call my version of the \texttt{walk()} function in our
homework:

\begin{lstlisting}
walk <- function(currdir,f,arg) {
   # "leave trail of bread crumbs"
   savetop <- getwd()
   setwd(currdir)
   fls <- list.files()
   arg <- f(currdir,fls,arg)
   # subdirectories of this directory
   dirs <- list.dirs(recursive=FALSE)
   for (d in dirs) arg <- walk(d,f,arg)
   setwd(savetop) # go back to calling directory
   arg
}
\end{lstlisting}

If you do use it, you must copy-and-paste it into your OMSI answer
window. If you don't use it, you still may find its code helpful.

You must include this test function, and a call test() in your OMSI
answer window, so that it will run:

\begin{lstlisting}
test <- function() {
   unlink('mydir',recursive=TRUE)  # rmdir
   dir.create('mydir')
   file.create('mydir/x')
   dir.create('mydir/d1')
   dir.create('mydir/d2')
   file.create('mydir/d2/x')
   print(countinst('mydir','x'))
}
\end{lstlisting}

\newpage
\onecolumn

{\bf Solutions:}

{\bf 1.}

\begin{lstlisting}
# code to find the number of instances of files with the given name in
# the given directory tree

test <- function() {
   unlink('mydir',recursive=TRUE)
   dir.create('mydir')
   file.create('mydir/x')
   dir.create('mydir/d1')
   dir.create('mydir/d2')
   file.create('mydir/d2/x')
   print(countinst('mydir','x'))
    # print(countinst.alt('mydir','x'))
}

countinst <- function(startdir,flname) {
   walk(startdir,checkname,list(flname=flname,tot=0))
}

checkname <- function(drname,filelist,arg) {
   if (arg$flname %in% filelist) arg$tot <- arg$tot + 1
   arg
}

# descend the directory tree starting at currdir, calling the
# user-supplied f(currdir,fls,arg) at each step; walk provides f() with
# the files list fls in the given directory, and arg acts as a "running
# total" specific to the given application

walk <- function(currdir,f,arg) {
   # "leave trail of bread crumbs"
   savetop <- getwd()
   setwd(currdir)
   fls <- list.files()
   arg <- f(currdir,fls,arg)
   # subdirectories of this directory
   dirs <- list.dirs(recursive=FALSE)
   for (d in dirs) arg <- walk(d,f,arg)
   setwd(savetop) # go back to calling directory
   arg
}

countinst.alt <- function(startdir,flname,arg=0) {
   savetop <- getwd() 
   setwd(startdir)
   fls <- list.files()
   if (flname %in% fls) arg <- arg + 1
   dirs <- list.dirs(recursive=FALSE)
   if (length(dirs) > 0)
   for (d in dirs) {
      arg <- countinst.alt(d,flname,arg)
   }
   setwd(savetop)
   arg
}\end{lstlisting}

\end{document} 

