\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: \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

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

{\bf 1.} (25) Fill in the blank:  The entity in standard Python that
disallows 2 computational threads from running at the same time is 
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_.

{\bf 2.} (25) Briefly (say, using at most a dozen words or so), state
what the two ways in which a thread (say under the {\bf threading}
module) can lose its turn.

{\bf 3.} (25) Write a generator version of our ``circular queue''
example, in a function declared as 

\begin{lstlisting}
def cq(q):
\end{lstlisting}

Your code is to be tested by running

\begin{lstlisting}
def testcq():
   x = [5,12,13]
   myq = cq(x)
   print myq.next()
   print myq.next()
   print myq.next()
\end{lstlisting}

whose output must be

\begin{lstlisting}
5
12
13
5
\end{lstlisting}

\end{quote}

{\bf 4.} (25) Here you will write a generator function to enable
iterating through all elements of a Python list that match a given
value.  Your function will be declared as

\begin{lstlisting}
def getnextmatch(x,m):
\end{lstlisting}

Here is the test code:

\begin{lstlisting}
def testgnm():
   y = [5,12,13,3,4,5,6,7,8,5,12]
   g = getnextmatch(y,5)
   for j in g: print j
   g = getnextmatch(y,12)
   for j in g: print j
   g = getnextmatch(y,2)
   for j in g: print j
\end{lstlisting}

The output will be

\begin{lstlisting}
0
5
9
1
10
\end{lstlisting}

\newpage
\onecolumn

{\bf Solutions:}

{\bf 1.} GIL

{\bf 2.} hardware interrupt from timer or system call

{\bf 3.}

\begin{lstlisting}
def cq(q):
   while 1:
      elt = q[0]
      q = q[1:] + [elt]
      yield elt

def testcq():
   x = [5,12,13]
   myq = cq(x)
   print myq.next()
   print myq.next()
   print myq.next()
   print myq.next()
\end{lstlisting}

{\bf 4.}

\begin{lstlisting}
def getnextmatch(x,m):
   i = 0
   lx = len(x)
   while 1:
      if x[i] == m: yield i
      i += 1
      if i == lx: raise StopIteration

def testgnm():
   y = [5,12,13,3,4,5,6,7,8,5,12]
   g = getnextmatch(y,5)
   for j in g: print j
   g = getnextmatch(y,12)
   for j in g: print j
   g = getnextmatch(y,2)
   for j in g: print j
\end{lstlisting}

\end{document} 

