-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcs102-week-eight.tex
551 lines (499 loc) · 12.1 KB
/
cs102-week-eight.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
\documentclass[14pt,aspectratio=169]{beamer}
\usepackage{pgfpages}
\usepackage{fancyvrb}
\usepackage{pgfplots}
\usepackage{minted}
\usemintedstyle{tango}
\usepackage{amsfonts}
\usepackage{moresize}
\usepackage{anyfontsize}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes}
\usetikzlibrary{arrows.meta}
\tikzstyle{process}=[rectangle, draw, thick, text width=5em, text centered, minimum height=2.5em, fill=gray!40]
\tikzstyle{entity}=[rounded rectangle, draw, thick, text width=5em, text centered, minimum height=1.5em, fill=gray!40]
\usetheme{auriga}
\usecolortheme{auriga}
\setbeamercolor{background canvas}{bg=lightgray}
% define some colors for a consistent theme across slides
\definecolor{red}{RGB}{181, 23, 0}
\definecolor{blue}{RGB}{0, 118, 186}
\definecolor{gray}{RGB}{146, 146, 146}
\title{Discrete Structures: \\ Programming with \\ Streams in Python}
\author{{\bf Gregory M. Kapfhammer}}
\institute[shortinst]{{\bf Department of Computer Science, Allegheny College}}
\begin{document}
{
\setbeamercolor{page number in head/foot}{fg=background canvas.bg}
\begin{frame}
\titlepage
\end{frame}
}
%% Slide
%
\begin{frame}{Technical Question}
%
\hspace*{.25in}
%
\begin{minipage}{4.8in}
%
\vspace*{.1in}
%
\begin{center}
%
{\large How do I use dynamically generated streams of data to implement
memory efficient and predictable Python programs?}
%
\end{center}
%
\end{minipage}
%
\vspace{2ex}
%
\begin{center}
%
\small Let's explore the difference between static sequences and dynamically
generated streams! Let's better understand how the choice of a discrete
structure influences the efficiency and behavior of Python programs!
%
\end{center}
%
\end{frame}
% slide
%
\begin{frame}{Dynamically Generated Streams}
%
\begin{itemize}
%
\item Sequences and streams are evident in Python programs!
%
\vspace*{-.15in}
%
\item Static sequences and dynamically generated streams:
%
\begin{itemize}
%
\item A list is a static sequence that exists as a complete data
structure
%
\item An file input stream appears dynamically over time when read
%
\item Let's distinguish between static sequences and dynamic streams
%
\item Example: streams are generated by iterators and range objects
%
\end{itemize}
%
\vspace*{-.2in}
%
\item What are the benefits of having a dynamic stream whose values are not
known until they are generated and returned? How does the memory
consumption of a Python program influence our choice of a discrete
structure?
%
\end{itemize}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{File Input Involves the Use of Streams}
\normalsize
\hspace*{-.65in}
\begin{minipage}{6in}
\vspace*{.25in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{python}
file = open("emails")
for line in file:
name, email = line.split(",")
if name == "John Davis":
print(email)
\end{minted}
\end{minipage}
\vspace*{.25in}
\begin{center}
%
\normalsize \noindent The file is a sequence of characters \\
\normalsize \noindent A character is a sequence of numbers \\
\normalsize \noindent Does the entire file exist in the computer's
memory? \\
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Comprehensions and Generators are Different}
\normalsize
\begin{minipage}{6in}
\vspace*{.2in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{python}
list_comprehension =
['Hello' for i in range(3)]
generator_expression =
('Hello' for i in range(3))
print(list_comprehension)
print(generator_expression)
\end{minted}
\end{minipage}
\vspace*{.05in}
\begin{center}
%
\normalsize \noindent What is the output of this Python program segment? \\
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Output from Comprehensions and Generators}
\normalsize
\begin{minipage}{6in}
\vspace*{.2in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{text}
Output from the list comprehension:
['Hello', 'Hello', 'Hello']
Output from the generator expression:
<generator object <genexpr>
at 0x7f0ebc7bc890>
\end{minted}
%
\end{minipage}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Using a Generator Expression in Python}
\normalsize
\begin{minipage}{6in}
\vspace*{.2in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{python}
even_squares =
(x * x for x in range(10)
if x % 2 == 0)
print(even_squares)
for value in even_squares:
print(value)
\end{minted}
%
\end{minipage}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Output from a Generator Expression in Python}
\normalsize
\hspace*{.1in}
\begin{minipage}{6in}
\vspace*{.2in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{text}
<generator object <genexpr>
at 0x7f8cb00db430>
0
4
16
36
64
\end{minted}
%
\end{minipage}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Exhausting a Generator Expression}
\normalsize
\begin{minipage}{6in}
\vspace*{.2in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{python}
even_squares =
(x * x for x in range(10)
if x % 2 == 0)
for value in even_squares:
print(value)
for value in even_squares:
print(value)
\end{minted}
%
\end{minipage}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Using Generator Functions in Python}
\normalsize
\begin{minipage}{6in}
\vspace*{.1in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{python}
def fibonacci_generator(n):
a = 1
b = 1
for i in range(n):
yield a
a, b = b, a + b
for value in fibonacci_generator(10):
print(fibonacci_value, end=" ")
\end{minted}
%
\end{minipage}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Output from a Generator Function in Python}
\normalsize
\begin{minipage}{6in}
\vspace*{.1in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{text}
<function fibonacci_generator
at 0x7f791d4bb1f0>
1 1 2 3 5 8 13 21 34 55
\end{minted}
%
\vspace*{.35in}
\begin{minipage}{5.2in}
\begin{center}
%
\normalsize \noindent What is the output of the program segment? \\
\normalsize \noindent Generator functions versus generator expressions? \\
\normalsize \noindent Can you find the pattern in these numbers? \\
\normalsize \noindent Benefits of generator expressions and functions? \\
%
\end{center}
%
\end{minipage}
\end{minipage}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Using Functions and Tuples in Python}
\normalsize
\begin{minipage}{6in}
\vspace*{.1in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{python}
def fibonacci_tuple(n):
result = ( )
a = 1
b = 1
for i in range(n):
result += (a,)
a, b = b, a + b
return result
\end{minted}
%
\end{minipage}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Output from a Tuple Function in Python}
\normalsize
\begin{minipage}{6in}
\vspace*{.1in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{text}
<function fibonacci_tuple
at 0x7f6e976b61f0>
1 1 2 3 5 8 13 21 34 55
\end{minted}
%
\vspace*{.35in}
\begin{minipage}{5.2in}
\begin{center}
%
\normalsize \noindent What is the output of the program segment? \\
\normalsize \noindent Generator functions versus tuple functions? \\
\normalsize \noindent Notice that this function produces the same output! \\
\normalsize \noindent Drawbacks of tuple-creation functions? \\
%
\end{center}
%
\end{minipage}
\end{minipage}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Using Functions and a List in Python}
\normalsize
\begin{minipage}{6in}
\vspace*{.1in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{python}
def fibonacci_list(n):
result = [ ]
a = 1
b = 1
for i in range(n):
result.append(a)
a, b = b, a + b
return result
\end{minted}
%
\end{minipage}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Output from a List Function in Python}
\normalsize
\begin{minipage}{6in}
\vspace*{.1in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{text}
<function fibonacci_list
at 0x7f8daf1051f0>
1 1 2 3 5 8 13 21 34 55
\end{minted}
%
\vspace*{.35in}
\begin{minipage}{5.2in}
\begin{center}
%
\normalsize \noindent What is the output of the program segment? \\
\normalsize \noindent Generator functions versus list functions? \\
\normalsize \noindent Notice that this function produces the same output! \\
\normalsize \noindent Drawbacks of list-creation functions? \\
%
\end{center}
\end{minipage}
%
\end{minipage}
%
\end{frame}
% Slide
%
\begin{frame}{Functions with Generators, Tuples, and Lists}
%
\begin{itemize}
%
\item Remember, each function produces the same final output!
%
\vspace*{-.15in}
%
\item Behavior and memory use differs for each function type
%
\vspace*{-.15in}
%
\item Understanding the properties of function types:
%
\begin{itemize}
%
\item {\bf Generator}: caller pulls values from the function
%
\item {\bf Tuple}: function pushes values to the caller
%
\item {\bf List}: function also pushes values to the caller
%
\end{itemize}
%
\vspace*{-.2in}
%
\item If a computer's memory was constrained, what would be the best
approach? The worst approach? Why?
%
\vspace*{-.2in}
%
\item Remember, the generation approach can use either an expression or a
function. How to pick one of these?
%
\end{itemize}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Endless Streams in Python Programs}
\normalsize
\begin{minipage}{6in}
\vspace*{.1in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{python}
def integers(n):
while True:
yield n
n += 1
for value in integers(10):
print(value)
\end{minted}
%
\end{minipage}
%
\vspace*{.05in}
\begin{center}
%
\normalsize \noindent Does this function ever stop running? \\
\normalsize \noindent What is the output of this program? \\
%
\end{center}
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Output of an Endless Stream Program}
\normalsize
\begin{minipage}{6in}
\vspace*{.1in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{text}
441027
441028
441029
441030
^C441031
Traceback (most recent call last):
File "endless-stream.py", line 7,
in <module> print(value)
KeyboardInterrupt
\end{minted}
%
\end{minipage}
%
\end{frame}
% Slide
%
\begin{frame}{Using Streams in Python Programs}
%
\begin{itemize}
%
\item Python programs frequently generate streams of values
%
\vspace*{-.2in}
%
\item Using streams enables the program to be memory efficient
%
\vspace*{-.2in}
%
\item Using tuples, lists, and streams in Python programs:
%
\begin{itemize}
%
\item {\bf Q1}: What is the difference between a list and a tuple?
%
\item {\bf Q2}: What is the difference between a list and stream?
%
\item {\bf Q3}: What is the difference between a tuple and a stream?
%
\item {\bf Q4}: How can you concatenate streams into a new stream?
%
\item {\bf Q5}: How do streams aid distributed programming in Python?
%
\end{itemize}
%
\vspace*{-.2in}
%
\item Dynamically generated streams support memory efficiency and
flexibility in Python programs. Also, the stream is a monoid under the
identify of the empty stream!
%
\end{itemize}
%
\end{frame}
\end{document}