\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.} Consider the Python list {\bf x}, pp.13-14.

\begin{itemize}

\item [(a)] (10) Write a Python expression for the element in row 1,
column 0.  Remember, row and column indices start at 0.

\item [(b)] (10) Write a Python expression for the entire row 1.

\item [(c)] (10) Is storage for a two-dimensional array (list, actually)
row-major, column-major or neither?  Assume we are working with tuples
if you wish.

\end{itemize}

{\bf 2.} (10) What function in the C library is the inspiration for the
{\bf \%} operator in line 4 of the middle code snippet on p.52?

{\bf 3.} (10) Consider the {\bf seek()} function in the top code snippet,
p.52.  One of the operations in a certain physical object inspired this
name.  What object is that?

{\bf 4.} (25) The {\bf handin} command on CSIF deposits student
submissions to a directory named {\bf handin/} in my account. Within
that directory are subdirectories, e.g.\ {\bf 132quiz3} and {\bf
145quiz8}, and some miscellaneous nondirectory files.  It is required
that I place a file {\bf users.deny} in each of those subdirectories.  I
had such a file (empty, actually) in my {\bf handin} directory, and
wanted to copy it to each subdirectory.  I did so by writing the short
script below and running it from within the {\bf handin/} directory.
Fill in the blanks.  Your code should work for the case of any
instructor using {\bf handin}, not just me.

\begin{lstlisting}
import os
flst =  blank (a)
for fl in flst:
   if  blank (b)  :
      blank (c) (fl)
      blank (d) ('cp ../users.deny .')
      blank (e)
\end{lstlisting}

% {\bf x.} () The code below concatenates all the (non-directory) files in
% a given directory tree.  They are assumed to be text files.  Fill in the
% blanks.

{\bf 5.} (25) The code below does a {\it flatten} operation, as mentioned
in class. Nested lists are ``flattened'' to non-nested ones, so that
for instance [[1,2],8,[4,6],[3,5]] becomes 
[1,2,8,4,6,3,5].  Fill in the blanks.  {\it Your code must work for any
number of levels of nesting.}

\begin{lstlisting}
def flatten(x):
   build = []
   for elt in x:
      if blank (a) :
         blank (b)
      else:
         blank (c)
   return build
\end{lstlisting}

Examples:

\begin{lstlisting}
>>> x
[[1, 2], 8, [4, 6, [3, 5]], 6, 10]
>>> flatten(x)
[1, 2, 8, 4, 6, 3, 5, 6, 10]
>>> z = [1,2,3]
>>> flatten(z)
[1, 2, 3]
>>> flatten([[1,2],'a','b'])
[1, 2, 'a', 'b']
\end{lstlisting}

You'll need the {\bf type()} function, which works as follows:

\begin{lstlisting}
>>> y = [5,12,13]
>>> type(y)
<type 'list'>
>>> type(y) is list
True
>>> type(y) is tuple
False
>>> type(3)
<type 'int'>
\end{lstlisting}

\onecolumn

\newpage

{\bf Solutions:}

{\bf 1.a} {\bf x[1][0]}

{\bf 1.b} {\bf x[1]}

{\bf 1.c} row-major

{\bf 2.} {\bf sprintf()}

{\bf 3.} disk drive

{\bf 4.}

\begin{lstlisting}
import os
flst = os.listdir('.')
for fl in flst:
   if os.path.isdir(fl):
      os.chdir(fl)
      os.system('cp ../users.deny .')
      os.chdir('..')
\end{lstlisting}

{\bf 5.}

\begin{lstlisting}
def flatten(x):
   build = []
   for elt in x:
      if not type(elt) is list:
         build.append(elt)
      else:
         build.extend(flatten(elt))
   return build
\end{lstlisting}

\end{document}

import os
flst = os.listdir('.')
for fl in flst:
   if os.path.isdir(fl):
      os.chdir(fl)
      os.system('cp ../users.deny .')
      os.chdir('..')

def flatten(x):
   build = []
   for elt in x:
      if not type(elt) is list:
         build.append(elt)
      else:
         build.extend(flatten(elt))
   return build

