%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
\documentclass[10pt]{article}
\usepackage[textheight=10in]{geometry}

\usepackage{verbatim}
\usepackage{amsmath}
\usepackage{amsfonts} % to get \mathbb letters

\usepackage[utf8]{inputenc}
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{9} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{9}  % for normal
% Defining colors
\usepackage{color}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}

\usepackage{listings}

%Python style from 
%http://tex.stackexchange.com/questions/199375/problem-with-listings-package-for-python-syntax-coloring
\newcommand\pythonstyle{\lstset{
  language=Python,
  backgroundcolor=\color{white}, %%%%%%%
  basicstyle=\ttm,
  otherkeywords={self},            
  keywordstyle=\ttb\color{deepblue},
  emph={MyClass,__init__},          
  emphstyle=\ttb\color{deepred},    
  stringstyle=\color{deepgreen},
  commentstyle=\color{red},  %%%%%%%%
  frame=tb,                         
  showstringspaces=false,
  numbers=left,numberstyle=\tiny,numbersep =5pt
}}
\usepackage{hyperref}

\begin{document}

\pythonstyle{}

\begin{center}
\Large{
Solution to Homework 2 \\
Ethan Bolker \\
Spring 2015
}
\end{center}

\section{Echo}
Here's my code for \lstinline!echo.py!. It's mostly comment, since I
included my ``diary'' there.

\lstinputlisting{echo.py}
\section{Average}

The \lstinline!open()! function opens a file and returns an object that
you can loop over with \lstinline!for!. Before I tried this I wondered
whether I would have to strip the newline from the end of each line. I
was lucky: the \lstinline!float()! function strips white
space (blanks and tabs and newlines) at the beginning and the end of
its argument.

\lstinputlisting{average.py}

Here's a test, pasted from Windows \verb!powershell!. The leading
blanks on the second line don't matter. The fourth line shows that the
\lstinline!float()! function can parse scientific notation.

\begin{verbatim}
PS C:\eb\python\hw2> type floats.txt
100
    200
300
4e2
PS C:\eb\python\hw2> python average.py floats.txt
average: 250.0
\end{verbatim}

\section{Robust Average}

Our main focus in this course is quick-and-dirty software to answer
mathematical questions we're curious about. If our programs break from
time to time, so what? But applications intended for general use must
not crash. To keep that from happening you have to anticipate all the
things that might go wrong, and guard against them. When something
does go wrong you have to warn the user and continue on, or exit the
program. 

You can detect problems either by testing in advance with an
\lstinline!if:! statement, or you can 
just \lstinline!try:! something and deal with any problem in the 
\lstinline!except:! clause.

Here are the tests for \lstinline!robust_average.py!.

First look for errors telling the program what file to use -- there
might be no files named, or more than one, or a file that isn't there.
(The file might be there but not be readable. I haven't tested for
that, but could.)

\begin{verbatim}
PS C:\eb\python\hw2> python robust_average.py
usage: robust_average.py <filename>
PS C:\eb\python\hw2> python robust_average.py nosuchfile
File nosuchfile not found
PS C:\eb\python\hw2> python robust_average.py floats.txt floats.txt
usage: robust_average.py <filename>
\end{verbatim}

If the file is really there, test for bad file contents, and warn the
user when a line can't be parsed by \lstinline!float()!.

\begin{verbatim}
PS C:\eb\python\hw2> python echo.py empty.txt
PS C:\eb\python\hw2> python robust_average.py empty.txt
Warning: no floats to average
PS C:\eb\python\hw2> python echo.py .\floaterrors.txt
100
next line is blank

200
2..0
-100
2e2
1f2
average should be 100
PS C:\eb\python\hw2> python robust_average.py  .\floaterrors.txt
Warning: float error on line next line is blank
Warning: float error on line
Warning: float error on line 2..0
Warning: float error on line 1f2
Warning: float error on line average should be 100
average: 100.0
\end{verbatim}

\lstinputlisting{robust_average.py}

\newpage
\emph{
Here is the \LaTeX{} source for this document. You can cut it from the
pdf and use it to start your answers. I used the} \verb!\jobname! 
\emph{macro for the source file name, so you can call your file by any
  name you like.}
\verbatiminput{\jobname}

\end{document}

