-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcs102-week-nine.tex
671 lines (611 loc) · 14.8 KB
/
cs102-week-nine.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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
\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 \\ Sets 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*{.5in}
%
\begin{minipage}{4.3in}
%
\vspace*{.1in}
%
\begin{center}
%
{\large How do I use the mathematical concepts of sets and Boolean logic
to design Python programs that are easier to implement and understand?}
%
\end{center}
%
\end{minipage}
%
\vspace{2ex}
%
\begin{center}
%
\small Let's explore how to use sets implement efficient and effective
programs that are easy to understand! Let's better understand how the choice
of a discrete structure influences the readability of a Python program!
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}{Using Mathematical Sets in Python Programs}
%
\begin{itemize}
%
\item Set theory is important to many areas of mathematics
%
\vspace*{-.15in}
%
\item Concentrate on the set theory useful in programming
%
\begin{itemize}
%
\item Sets are ``containers'' for other elements
%
\item Sets do not contain duplicate values
%
\item Set elements are not stored in a specific order
%
\item The universal set is the set of all elements
%
\item Sets can contain other objects like sets and tuples
%
\item A subset of a set contains a portion of the set
%
\end{itemize}
%
\vspace*{-.2in}
%
\item How do these properties of sets make it easier to implement Python
programs? How are they different than lists or tuples or generator
expressions?
%
\end{itemize}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Using Python to Create a Set from a List}
\normalsize
\begin{minipage}{6in}
\vspace*{.25in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{python}
x = set(['pencil', 'paper', 'pen',
'pencil', 'wallet', 'pen'])
print("Set defined with a list:")
print(x)
\end{minted}
\end{minipage}
\vspace*{.25in}
\begin{center}
%
\normalsize \noindent What is the output of this program? \\
\normalsize \noindent What are the properties of a set in Python? \\
\normalsize \noindent How do we define the order of the set's elements? \\
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Using Python to Create a Set from a Tuple}
\normalsize
\begin{minipage}{6in}
\vspace*{.25in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{python}
x = set(('pencil', 'paper', 'pen',
'pencil', 'wallet', 'pen'))
print("Set defined with a tuple:")
print(x)
\end{minted}
\end{minipage}
\vspace*{.25in}
\begin{center}
%
\normalsize \noindent What is the output of this program? \\
\normalsize \noindent Does the use of a set or a tuple influence? \\
\normalsize \noindent What types of elements can we store in a set? \\
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Output of a Program that Creates Sets}
\normalsize
\begin{minipage}{6in}
\vspace*{.25in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{text}
Set defined with a list:
{'paper', 'pencil', 'pen', 'wallet'}
Set defined with a tuple:
{'paper', 'pencil', 'pen', 'wallet'}
\end{minted}
\end{minipage}
\vspace*{.25in}
\begin{center}
%
\normalsize \noindent The sets do not store duplicate values \\
\normalsize \noindent Creation from either a set or a tuple is the same \\
\normalsize \noindent The contents of a set are displayed in arbitrary order \\
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Python's Sets Store Immutable Elements}
\normalsize
\begin{minipage}{6in}
\vspace*{.25in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{python}
x = {53, 'pencil',
(1, 1, 2, 3, 5), 3.14159}
print("Set with multiple types:")
print(x)
\end{minted}
\end{minipage}
\vspace*{.05in}
\begin{center}
%
\normalsize \noindent Note the different style for creation \\
\normalsize \noindent The data types in this set are different \\
\normalsize \noindent All of the variables in this set are immutable \\
\normalsize \noindent How will this program display the elements of the set? \\
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Python's Sets Cannot Store Mutable Containers}
\normalsize
\begin{minipage}{6in}
\vspace*{.25in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{python}
list = [53, 'pencil',
(1, 1, 2, 3, 5), 3.14159]
x = {list}
\end{minted}
%
\vspace*{.15in}
%
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{text}
Traceback (most recent call last):
File "set-element-types.py", line 11,
in <module>
x = {list}
TypeError: unhashable type: 'list'
\end{minted}
\end{minipage}
\vspace*{.05in}
\begin{center}
%
\normalsize \noindent Note the different style for creation \\
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}{Creating Sets with Mathematical Notation}
%
\begin{itemize}
%
\item Explicit definition of a set: $S = \{1, 2, 3\}$
%
\vspace*{-.15in}
%
\item Definition of a set with a property:\\ $\{ n \; | \; 0 < n < 100
\;\mbox{and}\; n \;\mbox{is odd} \}$
%
\vspace*{-.15in}
%
\item $\mathbb{N}$ is the set of natural numbers
%
\vspace*{-.15in}
%
\item $\mathbb{Z}$ is the set of integer numbers
%
\vspace*{-.15in}
%
\item $\mathbb{R}$ is the set of real numbers
%
\vspace*{-.15in}
%
\item $\mathbb{C}$ is the set of complex numbers
%
\vspace*{-.15in}
%
\item You can also define sets by using different set {\bf operators}!
%
\end{itemize}
%
\end{frame}
% Slide
%
\begin{frame}{Mathematical Operations with Sets}
%
\begin{itemize}
%
\item Set membership: $S = \{1, 2, 3\}$ such that $1 \in S$ and $5 \notin S$
%
\vspace*{-.15in}
%
\item Proper subset: $S = \{1, 2, 3\}$ and thus $\{1\} \subset S$
%
\vspace*{-.15in}
%
\item Subset: $S = \{1, 2, 3\}$ and thus $\{1\} \subseteq S$ and $\{1, 2, 3\} \subseteq S$
%
\vspace*{-.15in}
%
\item Set union: $S_1 \cup S_2$ contains elements in either $S_1$ or $S_2$
%
\vspace*{-.15in}
%
\item Set intersection: $S_1 \cap S_2$ is the elements in both $S_1$ and $S_2$
%
\vspace*{-.15in}
%
\item Set difference: $S_1 - S_2$ is the elements in $S_1$ but not in $S_2$
%
\vspace*{-.15in}
%
\item Union and intersection are {\bf associative} and {\bf commutative}!
%
\end{itemize}
%
\end{frame}
% Slide
%
\begin{frame}{Differences Between Math and Programming}
%
\begin{itemize}
%
\item Programmers cannot use sets like mathematicians do!
%
\vspace*{-.15in}
%
\item Python programs cannot store an infinite set
%
\vspace*{-.15in}
%
\item Finite sets must fit into a computer's finite memory
%
\vspace*{-.15in}
%
\item Programs need a procedure for constructing the set
%
\vspace*{-.15in}
%
\item Different programming languages and packages have other restrictions.
For instance, recall that Python programs cannot create sets that contain
mutable elements like lists!
%
\vspace*{-.15in}
%
\item So, what are the benefits of using sets in Python programs?
%
\end{itemize}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Using the Set Union Operator in Python}
\normalsize
\begin{minipage}{6in}
\vspace*{.25in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{python}
a = {1, 2, 3, 4}
b = {2, 3, 4, 5}
c = {3, 4, 5, 6}
d = {4, 5, 6, 7}
print(a.union(b, c, d))
print(a | b | c | d)
\end{minted}
\end{minipage}
\vspace*{.05in}
\begin{center}
%
\normalsize \noindent Sets in Python provide useful operations like {\tt union}! \\
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Using the Set Intersection Operator in Python}
\normalsize
\begin{minipage}{6in}
\vspace*{.25in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{python}
a = {1, 2, 3, 4}
b = {2, 3, 4, 5}
c = {3, 4, 5, 6}
d = {4, 5, 6, 7}
print(a.intersection(b, c, d))
print(a & b & c & d)
\end{minted}
\end{minipage}
\vspace*{.05in}
\begin{center}
%
\normalsize \noindent Sets in Python provide useful operations like {\tt
intersection}! \\
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Using the Set Difference Operator in Python}
\normalsize
\begin{minipage}{6in}
\vspace*{.25in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{python}
a = {1, 2, 3, 4}
b = {2, 3, 4, 5}
c = {3, 4, 5, 6}
d = {4, 5, 6, 7}
print(a.difference(b, c))
print(a - b - c)
\end{minted}
\end{minipage}
\vspace*{.05in}
\begin{center}
%
\normalsize \noindent Sets in Python provide useful operations like {\tt
difference}! \\
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Output of the Set Operators in Python}
\normalsize
\begin{minipage}{6in}
\vspace*{.25in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\normalsize]{text}
Union of the sets:
{1, 2, 3, 4, 5, 6, 7}
{1, 2, 3, 4, 5, 6, 7}
Intersection of the sets:
{4}
{4}
Difference of the sets:
{1}
{1}
\end{minted}
\end{minipage}
\vspace*{.05in}
\begin{center}
%
\normalsize \noindent Sets in Python provide useful operations like {\tt
difference}! \\
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Set Comprehensions in Python: Odd Positives}
\normalsize
\begin{minipage}{6in}
\vspace*{.25in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{python}
odd_positives = {n for n in range(100)
if n % 2 == 1}
for odd_positive in odd_positives:
print(odd_positive)
print(list(odd_positives))
\end{minted}
\end{minipage}
\vspace*{.05in}
\begin{center}
%
\normalsize \noindent Modular arithmetic helps us determine when a number is
odd\\
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Set Comprehensions in Python: Even Positives}
\normalsize
\begin{minipage}{6in}
\vspace*{.25in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{python}
even_positives = {n for n in range(100)
if n % 2 == 0}
for even_positive in even_positives:
print(even_positive)
print(list(even_positives))
\end{minted}
\end{minipage}
\vspace*{.05in}
\begin{center}
%
\normalsize \noindent Modular arithmetic helps us determine when a number is
even\\
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Boolean Logic and Sets: Using ``Logical Or''}
\normalsize
\begin{minipage}{6in}
\vspace*{.25in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{python}
odd_positives_two =
{n for n in range(20)
if n % 2 == 1 or n == 2}
for value in odd_positives_two:
print(value)
print(list(odd_positives_two))
\end{minted}
%
\end{minipage}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Output of the Program Using ``Logical Or''}
\normalsize
\begin{minipage}{6in}
\vspace*{.15in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{text}
1
2
3
[...]
19
[1, 2, 3, 5, 7, 9, 11, 13, 15, 17, 19]
\end{minted}
%
\end{minipage}
\vspace*{.05in}
\begin{center}
%
\normalsize \noindent Either of the conditions must be true for logical
{\tt or} operator!\\
%
\end{center}
%
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Boolean Logic and Sets: Using ``Logical And''}
\normalsize
\begin{minipage}{6in}
\vspace*{.25in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{python}
even_positives_by_four =
{n for n in range(20)
if n % 2 == 0 and n % 4 == 0}
for value in even_positives_by_four:
print(value)
print(list(even_positives_by_four))
\end{minted}
%
\end{minipage}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Output of the Program Using ``Logical And''}
\normalsize
\begin{minipage}{6in}
\vspace*{.15in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{text}
0
4
8
12
16
[0, 4, 8, 12, 16]
\end{minted}
%
\end{minipage}
\vspace*{.05in}
\begin{center}
%
\normalsize \noindent Both of the conditions must be true for logical
{\tt and} operator!\\
%
\end{center}
%
%
\end{frame}
% Slide
%
\begin{frame}{Using Sets in Python Programs}
%
\begin{itemize}
%
\item Sets are a discrete structure with many practical benefits!
%
\vspace*{-.2in}
%
\item Sets have built-in operations that make programming easy
%
\vspace*{-.2in}
%
\item Using sets and Boolean logic in Python programs:
%
\begin{itemize}
%
\item {\bf Q1}: What are the characteristics of a set?
%
\item {\bf Q2}: What are the built-in operations provided by a set?
%
\item {\bf Q3}: How can you connect sets in math and programming?
%
\item {\bf Q4}: How does Boolean logic help us describe a set?
%
\item {\bf Q5}: How does the {\tt sympy} package support set programming?
%
\end{itemize}
%
\vspace*{-.2in}
%
\item Refer to \url{https://realpython.com/python-sets/} for more details
about how to program in Python with sets!
%
\end{itemize}
%
\end{frame}
\end{document}