<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Combinatorics
% Math 480 Spring 2015
%

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

%%%%%%%%%%%%%%%% start here %%%%%%%%%%%%%%%%
\begin{center}
\Large{
Discrete Mathematics in Python \\
Ethan Bolker \\
\today
}
\end{center}

\section{Permutations}

Here's a function that write out all the permutations of a list.

\lstinputlisting{combinatorics1.py}

with its outputn

\begin{verbatim}
python combinatorics1.py 
permute([1,2,3])
[[1, 2, 3], [2, 1, 3], [2, 3, 1], [1, 3, 2], [3, 1, 2], [3, 2, 1]]
print(permute([]))
[]
permute(["a"]))
[['a']]
permute([1,2,2])
[[1, 2, 2], [2, 1, 2], [2, 2, 1], [1, 2, 2], [2, 1, 2], [2, 2, 1]]
\end{verbatim}

Lessons here:
\begin{itemize}
\item Turning recursive pseudocode into a recursive program
\item Slicing a list returns a copy
\item Single and double quotes
\item This algorithm isn't smart about repetitions
\item Should find the built in function in \lstinline!itertools!
\item Should use \lstinline!yield! instead of \lstinline!return!
\end{itemize}

In class a student remembered that $0! = 1$, which suggests that the
list of permutations of the empty list should be \lstinline![[]]!, not
the \lstinline![]! in my code. I tried that, which allowed me to treat
the empty list as the base case and delete the special treatment for a
list of length 1. While I was at it I changed the code so that the
\emph{last} element of the list was the new guy. 

\lstinputlisting[firstnumber=8,firstline=8,lastline=21]{combinatorics2.py}

This worked just fine. The permutations in the output appear in a
different order, of course.

\section{Itertools}

In class I pointed out that the list of permutations of 10 elements
would be very long -- length 10! If you wanted that list in order to
loop over it you would not want to build the whole list in memory. The
\lstinline!itertools! module can do just that.

Here's the code and the testing code:

\lstinputlisting[firstnumber=34,firstline=34,lastline=44]{combinatorics2.py}

Here's the output:

\begin{verbatim}
myshell&gt; python combinatorics2.py

...

Try to print the object returned by the itertools permutations function
&lt;itertools.permutations object at 0x027244B0&gt;
Iterating through that object works
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
\end{verbatim}

Notes for next hw:

\begin{itemize}
\item Do an assignment for another course using \TeX or Python
  (preferably both).

\item Do something with itertools (question not made up yet)

\item Improve the integration functions so that they correctly handle
  functions that may be negative (area under the x axis counts
  negative) and correctly handle $t$ less than $b$. This may happen
  automatically for the trapezoid rule but will need work for monte
  carlo.

\item TBD
\end{itemize}

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

</pre></body></html>