\pdfminorversion=4
\documentclass{beamer}
\usepackage{graphicx}
\usepackage{amsfonts}
\usepackage{amsmath,amscd}
\usepackage{chngpage}
\usepackage{tikz}
\usepackage{ifthen}
\usepackage{relsize}
\usepackage{multirow}
\usepackage{cleveref}
\usepackage{url}
\usepackage{pdfpages}
\usepackage{ dsfont }
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\usepackage{array}% http://ctan.org/pkg/array
\usepackage{multicol}
\usepackage{textcomp}

\usetikzlibrary{positioning}
\usetikzlibrary{matrix}
\usetikzlibrary{calc}
\usetikzlibrary{shapes}
\usetikzlibrary{arrows}
\usetikzlibrary{fit}
\usetikzlibrary{decorations}
\usetikzlibrary{snakes}
\usetikzlibrary{shadows}
\pgfdeclarelayer{background}
\pgfdeclarelayer{backbackground}
\pgfdeclarelayer{foreground}
\pgfsetlayers{backbackground,background,main,foreground}
\usepackage{fancyvrb}

%%%% define python syntax display %%%%
% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{10} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{10}  % for normal


% Custom colors
\usepackage{color}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}
\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mymauve}{rgb}{0.58,0,0.82}

\usepackage{listings}

% Python style for highlighting
\newcommand\pythonstyle{\lstset{
language=Python,
basicstyle=\ttm,
otherkeywords={self},             % Add keywords here
keywordstyle=\ttb\color{deepblue},
emph={MyClass,__init__},          % Custom highlighting
emphstyle=\ttb\color{deepred},    % Custom highlighting style
%stringstyle=\color{purple},
stringstyle=\color{mymauve},
frame=tb,                         % Any extra options here
showstringspaces=false,            %
commentstyle=\color{mygreen}
}}

% Python environment
\lstnewenvironment{python}[1][]
{
\pythonstyle
\lstset{#1}
}
{}

% Python for external files
\newcommand\pythonexternal[2][]{{
\pythonstyle
\lstinputlisting[#1]{#2}}}

% Python for inline
\newcommand\pythoninline[1]{{\pythonstyle\lstinline!#1!}}

%%%% define python syntax display %%%%


%%%% PICTURE IF CITATION


\usepackage{animate}
\usepackage{color, colortbl}
\usepackage{xspace}
\usepackage{sidecap}
\usepackage{pdfpages}

%\usepackage{biblatex}
\usepackage[style=verbose-note]{biblatex}

\newcommand{\PE}[1]{E(#1)}
\newcommand{\EI}{\text{EI}}
\newcommand{\ES}{\text{ES}}
\newcommand{\ISO}{\text{ISO}}
\newcommand{\Z}[3]{\mathcal{Z}_{\substack{(#1)\\ [#3]}}^{#2}}
\newcommand{\Y}[3]{\mathcal{Y}_{\substack{(#1)\\ [#3]}}^{#2}}
\newcommand{\B}{\mathcal{B}}
\newcommand{\Kron}{\delta}
\newcommand{\ub}{\bullet}

\newcommand{\Ab}{{\sf{A}}}
\newcommand{\Cb}{{\sf{C}}}
\newcommand{\Gb}{{\sf{G}}}
\newcommand{\Ub}{{\sf{U}}}

\newcommand{\SpaceCheating}{\vspace{-0em}}

\newcommand{\q}{\textquotesingle}

\newcommand{\bl}{\color{blue}}
\newcommand{\re}{\color{red}}

\setbeamertemplate{frametitle}[default][center]
\setbeamertemplate{footline}[frame number]


\usepackage[utf8]{inputenc}
 
\usepackage{listings}
\usepackage{color}
 
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.98,0.98,0.96}
 
\lstdefinestyle{mystyle}{
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\scriptsize,
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                    
    keepspaces=true,                 
    numbers=left,                    
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=true,                  
    tabsize=2
}
 
\lstset{style=mystyle}

\begin{document}



\definecolor{myhighlight}{rgb}{0.82,0.82,0.82}

\frame{
	\title{COMP 204}
	\subtitle{Control flow - Loops}
	\author{Yue Li\\based on materials from Mathieu Blanchette}
	
	\date{}
	\titlepage
}

\frame{\centering\Large Quiz 6 password}

\frame{
\frametitle{Midterm time and location update}
\begin{itemize}
	\item Time: Friday, February 22 from 6:30-8:00 pm
	\item Location: LEA 219
\end{itemize}	
}


\frame{
	Assignment \#1 is posted on MyCourses on January 17\\
	\vspace{0.5em}
	\url{https://www.cs.mcgill.ca/~yueli/teaching/COMP204_Winter2019/Assignments/HW1/assignment1.pdf}\\
\vspace{1cm}
	Due date: February 1, 23:59\\
\vspace{1cm}
	Submit one Python file per question, on MyCourses.\\
	\vspace{1cm}
	Start working on it ASAP!\\
}

\begin{frame}
\frametitle{Recap from last lecture: conditionals}
\begin{itemize}
	\item Conditionals (if-else, if-elif-else, and nested) allow us to decide which blocks of code get executed under which conditions.
	\item But each line of code is still executed either zero or one time.
\end{itemize}

\lstinputlisting[language=Python, firstline=7, lastline=16]{../5/nuclear.py}
\end{frame}


\begin{frame}[fragile]
\frametitle{Control flow: Loops}
How do we execute the same operations multiple times? 

Answer: \textbf{Loops}. 

There are two types of loops: 
\begin{enumerate}
	\item \texttt{while} loop
	\item \texttt{for} loop 
\end{enumerate}

\lstinputlisting[language=Python]{while.py}

What happens when this is executed?
\begin{itemize}
\item Line 1: booleanCondition is evaluated. If true, jump to line 2. If false, exit loop and jump to line 6.
\item Line 2, 3, 4: the body of the loop is executed
\item After line 4: Jump back to line 1
\item Line 6: continue executing the rest of the program
\end{itemize}
\end{frame}

\begin{frame}[fragile]
\frametitle{The first loop example - countdown}

\lstinputlisting[language=Python]{countdown.py}

Let's execute it step by step to see what happens ...

\end{frame}

\begin{frame}[fragile]
\frametitle{Input checking}

In the examples seen so far, we did not do a very good job of check the validity of data entered by the user.

\vspace{0.8em}

Usually, if a user enters invalid data, we should them ask to enter the data again.

\vspace{0.8em}
General algorithm:
\begin{enumerate}
\item Ask user to enter some data (String)
\item Check the validity of the data
\item If the data is invalid, return to step (1), else continue with rest of program
\end{enumerate}
\end{frame}

\begin{frame}[fragile]
\frametitle{While loops - input validity}
{\scriptsize
\lstinputlisting[language=Python]{validateInput.py}
}
\end{frame}


%\begin{frame}[fragile]
%\frametitle{While loops - population growth}
%In one generation, a rabbit female (doe) gives birth to 6 pups on average: 3 males (bucks) and 3 females (does). \\
%How many rabbits after 10 generations? \\
%Assumption: Rabbits never die.\\
%{\small
%\vspace{1cm}
%Example: Starting with 5 does and 3 bucks:\\
%Generation 0: 5 does, 3 bucks\\
%Generation 1: (5 + 5*3) does, (3 + 5*3) bucks = 20 does, 18 bucks\\
%Generation 2: (20 + 20*3) does, (18 + 20*3) bucks = 80 does, 78 bucks\\
%Generation 3: (80 + 80*3) does, (78 + 80*3) bucks = 240 does, 238 bucks\\
%}
%\end{frame}


%\begin{frame}[fragile]
%\frametitle{While loops - population growth}
%
%\lstinputlisting[language=Python]{rabbits.py}
%\end{frame}
%
%\begin{frame}[fragile]
%\frametitle{While loops - password checking}
%A solid password should include at least one lowercase letter, one uppercase letter, one number, and one special character. Write a program that checks that a given password is solid.
%
%One possible algorithm:
%\begin{itemize}
%\item Ask user to type in password; save it in a string
%\item Count the number of lower, upper, number, special character (need counter variables for each)
%	\begin{itemize}
%	\item for each position in the password string,
%		\begin{itemize}
%	\item determine type of character
%	\item increase (increment) the corresponding counter variable
%		\end{itemize}
%	\end{itemize}
%\item check that all four counter variables are at least 1
%\end{itemize}
%\end{frame}


\begin{frame}[fragile]
\frametitle{For loops}
As we see, \texttt{while} loop allows us to repeat the execution of a block of code, as long as a certain condition hold.

Another type of loop is called \textbf{for loop}:

\lstinputlisting[language=Python]{forLoop.py}

Execution:
\begin{itemize}
\item Line 1: \texttt{someVariable} gets the value of the next element in \texttt{someList}. 
\item If this is the first turn of the loop, the next element is the first element in the list. If there is no next element, jump to line 4, else execute body of loop (Line 2).
\item Line 2: Body of loop
\item After 2: Jump back to line 1.
\item Line 4:  rest of the program (outside loop) 
\end{itemize}
\end{frame}

\begin{frame}[fragile]
	\frametitle{Sidetrack: the \texttt{range} function}
The \texttt{range} function is often used in combination with \texttt{for} loops.

\begin{itemize}
	\item \texttt{range(stop)}: integer list from 0 up to \texttt{stop}
	\item \texttt{range(start, stop)}: integer list from \texttt{start} up to \texttt{stop}
	\item \texttt{range(start, stop, step)}: integer list from \texttt{start} up to \texttt{stop} but with increment set by \texttt{step}
\end{itemize}
\begin{itemize}
	\item start: Starting number of the sequence
	\item stop: Generate numbers up to, but not including this number.
	\item step: Step size to increment the start until stop (default: 1).
\end{itemize}

\begin{python}
range(5) # 0, 1, 2, 3, 4
range(3,7) # 3, 4, 5, 6   (note: 7 is not included)
range(3,9,2) # 3, 5, 7  (Start at 3, up to but not including 9, in increments of 2)
range(5,0,-1) # 5, 4, 3, 2, 1  (Start at 5, down to but excluding 0, in increments of -1)
\end{python}
\end{frame}

\begin{frame}[fragile]
\frametitle{For loops - countdown example (vs while-loop version)}

\texttt{countdown\_forLoop.py}:
\lstinputlisting[language=Python]{countdown_forLoop.py}
\vfill
\texttt{countdown\_whileLoop.py}:
\lstinputlisting[language=Python]{countdown.py}
\end{frame}


\frame{
	\frametitle{\texttt{while} loops vs \texttt{for} loops}
In fact, you can always replace a for loop with a while loop, and vice-versa. But there are times where using one is much simpler than the other.

Use a {\bf while loop} when:
\begin{itemize}
\item The number of iterations is not known ahead of time, but depends on the results of some computation, or on some user input.
\end{itemize}

Use a {\bf for loop} when:
\begin{itemize}
\item We want to repeat a block of code for a {\em fixed} number of times
\item We want to perform the same operation on each element of a object
\end{itemize}
}

\begin{frame}[fragile]
\frametitle{For loops vs while loop example 2: password guessing}
Task: keep asking password until get the correct one

 \lstinputlisting[language=Python]{whileLoopPwd.py}

 \lstinputlisting[language=Python]{forLoopPwd.py}
\end{frame}

\begin{frame}
	\center\Large Resumed from the end of last lecture (01/18)
\end{frame}

\begin{frame}[fragile]
\frametitle{Sidetrack: how to access substring in a string}
\lstinputlisting[language=Python, firstline=1, lastline=19]{strings.py}
\end{frame}

\begin{frame}[fragile]
\frametitle{Sidetrack: how to access substring in a string}
\lstinputlisting[language=Python, firstline=20, lastline=37]{strings.py}
\end{frame}

\begin{frame}
\frametitle{How to iterate over a string using loops}
Task: change every occurrence of `T' to 'U' to convert a DNA sequence to an RNA sequence

\vspace{0.2cm}

\textbf{Before we see the solution code, let's step back and think about how shall we approach this problem by hand:}

\vspace{0.5cm}

\begin{itemize}
\item Here is a DNA sequence: \texttt{ACTGAGCTAGCT}
\end{itemize}

\vspace{0.5cm}

Points to think about:

\begin{enumerate}
	\item Where do we save the converted RNA sequence?
	\item How do we access each letter in the DNA sequence?	
	\item How do we go to the next letter and then next letter and so on in the DNA sequence?
	\item How do we change every T to a U but keep other letters the same?
\end{enumerate}
\end{frame}

\begin{frame}[fragile]
\frametitle{For loops vs while loop example 3}
Task: change every occurrence of `T' to 'U' to convert a DNA sequence to an RNA sequence

 \lstinputlisting[language=Python]{T2U_forLoop.py}

 \lstinputlisting[language=Python]{T2U_whileLoop.py}
\end{frame}

\begin{frame}[fragile]
\frametitle{Quiz 6: finding start codon}

Here are some RNA sequences: 
\begin{align*}
&\texttt{\underline{AUG}UGA}: & \text{start codon position at 0}\\
&\texttt{ACCU\underline{AUG}ACGUCCUAAGCAGUUUGACG}: & \text{start codon position at 4}\\
&\texttt{ACCU\underline{AUG}AUAACCUAAGCAGUUUGACG}: & \text{start codon position at 4}\\
&\texttt{\underline{AUG}AUGAUGAUGAUGUGAUGAUGAUGA}: & \text{start codon position at 0}\\
&\texttt{CGUAUUGCAGUGGUACUAC}: & \texttt{contains no start codon }
\end{align*}

Points to think about:

\begin{enumerate}
	\item We want to go letter by letter meanwhile ``look ahead" 3 letters ahead
	\item We want to quit searching as soon as we find `AUG'
	\item We do not want to go outside of the range of the string sequence in case the string do not contain `AUG'
\end{enumerate}

Let's try out the 4 choices of while loop conditionals in Python
\end{frame}

%\begin{frame}[fragile]
%\frametitle{Calculate protein length from a mRNA sequence}
% \lstinputlisting[language=Python]{mRNATranslation.py}
%\end{frame}








\end{document}