-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslides.tex
272 lines (224 loc) · 9.7 KB
/
slides.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
\documentclass[hyperref={colorlinks,citecolor=blue,linkcolor=blue,urlcolor=blue}]{beamer}
\usepackage{xcolor}
\usepackage{pgfpages}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amsmath,amssymb}
\mode<presentation>
{
\usetheme{Madrid} % or try default, Darmstadt, Warsaw, ...
\usecolortheme{beaver} % or try albatross, beaver, crane, ...
\usefonttheme{default} % or try default, structurebold, ...
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{caption}[numbered]
}
\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.95,0.95,0.92}
\usepackage{listings}
\lstdefinestyle{mystyle}{
commentstyle=\color{codegreen},
keywordstyle=\color{blue},
stringstyle=\color{codepurple},
basicstyle=\ttfamily\small,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2
}
\pgfpagesuselayout{resize to}[%
physical paper width=8in, physical paper height=6in]
\title[IbraFSG 3: List Comprehensions]{IbraFSG\texttrademark{} 3 - Week 7; List Comprehensions}
\author{Ibrahim Chehab}
\institute{UTM RGASC}
\date{\today}
\begin{document}
\begin{frame}
\titlepage
\end{frame}
\begin{frame}{Table of Contents}
\tableofcontents
\end{frame}
\section{Introduction}
\subsection{Welcome back to IbraFSGs\texttrademark{}}
\begin{frame}
\frametitle{Welcome back to IbraFSGs\texttrademark{}}
\begin{itemize}
\item Welcome back to IbraFSGs\texttrademark{}! Hello to new people and welcome back to tenured members.
\item This week we will be discussing \textit{list comprehensions}.
\item List Comprehensions are a way to create lists in Python using very little code. They are a very powerful tool that you will use in the future (A2)
\end{itemize}
Quick House Keeping Notes: Would you rather IbraFSGs\texttrademark{} continue to provide harder, "worst-case-scenario" midterm-style problems, or would you like to see more "real-world" problems?\\
Today I've tried to mix the two together, but I'd like to know your thoughts.
\end{frame}
\subsection{A Recap of the UltraSheet\texttrademark{}}
\begin{frame}{A Recap of the UltraSheet\texttrademark{}}
\begin{itemize}
\item An \textit{UltraSheet\texttrademark{}} is a "cheat sheet" that you compile for \textbf{yourself} to review course materials
\begin{itemize}
\item Sharing UltraSheets\texttrademark{} is \textbf{counter-productive} and \textbf{will not help you learn the material}
\item However, reviewing content in a group and simultaneously updating your UltraSheets\texttrademark{} is a good idea
\end{itemize}
\item It acts like your own personalized textbook chapter
\begin{itemize}
\item It allows you to \textbf{regurgitate all the course information in a contiguous, organized manner} and helps you \textbf{find gaps in your knowledge}
\end{itemize}
\item UltraSheets\texttrademark{} help with type 1 and 2 questions
\begin{itemize}
\item Can you remember what type 1 and 2 questions are?
\item Can you remember what a \textit{type 3} question is? :troll:
\end{itemize}
\end{itemize}
\end{frame}
\subsection{Midterm Debrief}
\begin{frame}
\frametitle{Midterm Debrief}
How did the midterm go! In parciular:
\begin{itemize}
\item Which course content did the midterm emphasize?
\item Which types of questions did you notice were emphasized? (Type I, II, III)
\item Which studying tactics worked? Which ones didn't?
\item What will you do differently for next time?
\end{itemize}
Fun fact: In \textit{CSC236} you prove the correctness of functions using \textit{induction.} Aka: You don't need to make test cases, you know for a fact it works no matter what.
\end{frame}
\section{List Comprehensions}
\subsection{Key Terms}
\begin{frame}{Key Terms}
\begin{itemize}
\item \textbf{List Comprehension:} A way to create lists in Python using very little code.
\item \textbf{Expression:} A piece of code that returns a value.
\item \textbf{Iterable:} An object that can be iterated over.
\end{itemize}
...Yeah that's about it for this week.
\textbf{Note:} Your UltraSheets\texttrademark{} should be filled with examples and explanations of these terms, and specifically how they relate to List Comprehensions.
\end{frame}
\subsection{A Trick to understanding list comprehensions}
\begin{frame}[fragile]
\frametitle{A Trick to Understanding List Comprehensions}
List Comprehensions may seem scary at first, but they are actually quite simple. The trick to understanding them is to relate them to MAT102:
Example:
\begin{columns}[T]
\begin{column}{0.4\textwidth}
\begin{align*}
\{x^2 : x \in \{0, 1, 2, 3, 4, 5\}\}
\end{align*}
\end{column}
\hfill
\begin{column}{0.1\textwidth}
\begin{center}
\Large $\iff$
\end{center}
\end{column}
\hfill
\begin{column}{0.5\textwidth}
\begin{lstlisting}[language=Python, style=mystyle]
[x**2 for x in range(6)]
\end{lstlisting}
\end{column}
\end{columns}
\textbf{Fun Fact:} You can include conditionals in list comprehensions, and items to be added if the condition is not met! Example: What would the following list comprehension return?
\begin{lstlisting}[language=Python, style=mystyle]
[x**2 if x % 2 == 0 else x**3 for x in range(6)]
\end{lstlisting}
\end{frame}
\subsection{Cool Application for List Comprehensions}
\begin{frame}[fragile]
\frametitle{Cool Application for List Comprehensions}
List Comprehensions are a very quick way to return \textit{counts} of something, and to filter for a certain attribute. For instance (fill in the blanks):
\begin{lstlisting}[language=Python, style=mystyle]
# Count the number of songs I've liked:
____[song for song in songs _______]
# filter for songs i've liked
[song for song in songs __________]
\end{lstlisting}
\end{frame}
\section{Practice Problems}
\subsection{Practice Problem I: Loop2LstComp}
\begin{frame}[fragile]
\frametitle{Practice Problem I: Loop2LstComp}
Nugget is working on clearing out their Spotify Playlist. They're working on making some list comprehensions to filter out some songs. Help them convert the following for-loops into list comprehensions:
\begin{lstlisting}[language=Python, style=mystyle]
# Q1:
lst = []
for i in songs: # songs is an arbitrary list of Song objects; implementation is irrelevant for this question
lst.append(i.title)
# Q2:
filter = []
for song in songs:
if song.is_liked():
filter.append(song)
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Practice Problem I: Loop2LstComp (hard mode)}
\begin{lstlisting}[language=Python, style=mystyle]
# Q3 (Blame Nugget for this, not me):
filter = []
for song in songs:
if song.genre == "Rock":
filter.append("Rock on!")
elif song.genre == "Pop":
filter.append("Pop on!")
elif song.genre == "Love":
filter.append("No Swifties Allowed")
else:
filter.append("euuuh, brother euuuh")
\end{lstlisting}
\textit{Hint:} You can't use \texttt{elif} in a List Comprehension, but you can use \texttt{if} and \texttt{else}. How can we replicate the \texttt{elif} functionality using only \texttt{if} and \texttt{else}?\\
\textit{Hint 2:} Start by eliminating \texttt{elif} in the original problem\\
\textit{Hint 3:} Think of the chain rule from MAT135
\end{frame}
\subsection{Practice Problem II: This ain't Build A ForLoop}
\begin{frame}
\frametitle{Practice Problem II: This ain't Build A ForLoop}
Bella Poarch is working on her next hit single, \textit{Build A ForLoop}. She's trying to create some list comprehensions, but since making list comprehensions isn't smiling on TikTok she's struggling. Help her fill in the following list comprehensions:
\begin{itemize}
\item \textbf{Q1:} Create a list comprehension that returns the first 10 even numbers
\item \textbf{Q2:} Create a list comprehension that returns the first 10 odd numbers
\item \textbf{Q3:} Create a list comprehension that flattens a 2D list
\end{itemize}
Example:
\texttt{[[1, 2, 3], [4, 5, 6], [7, 8, 9]]} $\rightarrow$ \texttt{[1, 2, 3, 4, 5, 6, 7, 8, 9]}\\
\textit{Hint:} An Iterable can contain an expression :thonk:
\end{frame}
\section{Conclusion}
\subsection{A Final Challenge...}
\begin{frame}
\frametitle{A final challenge...}
Recall from last week our challenge problem:
Sharon Goodwin delves into Python in her free time. She's trying to create a series of recursive functions that mutually recurse over each other to determine whether a positive integer is even or odd. Help her create these two functions.
\begin{center}
\textbf{RESTRICTIONS:}
\begin{itemize}
\item You are \textbf{NOT} allowed to use \textbf{ANY} of Python's integer operations \textbf{EXCEPT} subtraction.
\item You may \textbf{NOT} use \textbf{Modulo}.
\item Each function must have \textbf{EXACTLY} one base-case.
\item You \textbf{MUST} use mutual recursion.
\item You \textbf{MAY NOT} use any helper methods.
\end{itemize}
\end{center}
\end{frame}
\begin{frame}[fragile]
\frametitle{A final challenge\ldots}
Continue solving this problem today:
\begin{lstlisting}[language=Python, style=mystyle]
def is_even(num: int) -> bool:
"""
Method which uses mutual recursion to determine whether an integer is even or odd.
"""
# TODO: Implement this method with a recursive call to is_odd
def is_odd(num: int) -> bool:
"""
Method which uses mutual recursion to determine whether an integer is even or odd.
"""
# TODO: Implement this method with a recursive call to is_even
\end{lstlisting}
\end{frame}
\end{document}