-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcs102-week-three.tex
602 lines (568 loc) · 14.6 KB
/
cs102-week-three.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
\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: \\ Computational and Mathematical Tasks 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}
%
\begin{center}
%
{\large How do I use iteration and conditional logic in a Python program to
perform computational tasks like processing the contents of a file and
mathematical tasks like using Newton's method to approximate the square root
of a number?}
%
\end{center}
%
\vspace{2ex}
%
\begin{center}
%
\small Let's learn how to use the Python programming language to implement
programs that perform useful computational and mathematical tasks!
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}{Python Programming Retrospective}
%
\begin{itemize}
%
\item Intuitively read the code segments to grasp their behavior
%
\vspace*{-.15in}
%
\item Key components of the Python programming segments
%
\begin{itemize}
%
\item Function calls
%
\item Assignment statements
%
\item Iteration constructs
%
\item Conditional logic
%
\item Variable creation
%
\item Variable computations
%
\item Variable output
%
\end{itemize}
%
\vspace*{-.2in}
%
\item Investigate the {\em syntax} and {\em semantics} of these components!
%
\end{itemize}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Python Programs are Sequences of Statements}
\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 A Python program is a sequence of statements \\
\normalsize \noindent Programs contain both {\em simple} and {\em compound} statements \\
\normalsize \noindent Why is this, technically, not a ``Python program''? \\
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Simple and Compound Statements in Python}
\hspace*{-.6in}
\begin{minipage}{6in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{python}
sum = 0
count = 0
file = open("observations")
for line in file:
n = int(line)
sum += n
count += 1
print(sum/count)
\end{minted}
\end{minipage}
\end{frame}
% Slide
%
\begin{frame}{Industry-Standard Python Programming}
%
\begin{itemize}
%
\item Please use Python 3 for all of your programs!
%
\vspace*{-.15in}
%
\item Add ``docstring'' comments to your Python programs
%
\begin{itemize}
%
\item Module
%
\item Class
%
\item Function
%
\end{itemize}
%
\vspace*{-.2in}
%
\item Add comments for important blocks of your program
%
\vspace*{-.2in}
%
\item Use descriptive variable and function names
%
\vspace*{-.2in}
%
\item The book does not always adhere to industry standards!
%
\end{itemize}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Command-Line Interfaces for Python Programs}
\normalsize
\hspace*{-.15in}
\begin{minipage}{6in}
\vspace*{.15in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\footnotesize]{python}
def main(
a: float = typer.Option(1),
b: float = typer.Option(2),
c: float = typer.Option(2)
):
"""Calculate roots of a quadratic eqn with quadratic formula."""
typer.echo(f"Calculating the roots of a quadratic equation with:")
typer.echo(f" a = {a}")
typer.echo(f" b = {b}")
typer.echo(f" c = {c}")
x_one, x_two = rootfind.calculate_quadratic_equation_roots(a, b, c)
typer.echo(f" x_one = {x_one}")
typer.echo(f" x_two = {x_two}")
if __name__ == "__main__":
typer.run(main)
\end{minted}
\end{minipage}
%
\vspace*{.05in}
%
\begin{center}
%
\normalsize \noindent This program can accept user input through the command line \\
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Calculating the Roots of a Quadratic Function}
\hspace*{-.1in}
\begin{minipage}{6in}
%
\vspace*{.2in}
%
\begin{minted}[mathescape, numbersep=5pt, fontsize=\small]{python}
def calc_quad_eqn_roots(a: float, b: float, c: float):
"""Calculate the roots of a quadratic equation."""
D = (b * b - 4 * a * c) ** 0.5
x_one = (-b + D) / (2 * a)
x_two = (-b - D) / (2 * a)
return x_one, x_two
\end{minted}
\end{minipage}
%
\vspace*{.05in}
%
\begin{center}
%
\normalsize \noindent {\bf Input}: Three floating-point inputs called {\tt a}, {\tt b}, and {\tt c}\\
\normalsize \noindent {\bf Output}: Two floating-point outputs called {\tt x\_one} and {\tt x\_two}\\
\normalsize \noindent {\bf Behavior}: Calculate the roots of a quadratic equation\\
\normalsize \noindent {\bf Question}: How does this function work? How do you test it?
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Testing the Root Finding Function}
\hspace*{-.1in}
\begin{minipage}{6in}
%
\vspace*{.2in}
%
\begin{minted}[mathescape, numbersep=5pt, fontsize=\small]{python}
def test_calculate_x_values_non_imaginary():
"""Check calculation of x values."""
a = 1
b = 2
c = 1
x_one, x_two = rootfind.calc_quad_eqn_roots(a, b, c)
assert x_one == -1.0
assert x_two == -1.0
\end{minted}
\end{minipage}
%
\vspace*{.05in}
%
\begin{center}
%
\normalsize \noindent {\bf Input}: Three floating-point inputs with values of 1, 2, and 1
\normalsize \noindent {\bf Actual Output}: Stored in the variables called {\tt x\_one} and {\tt x\_two}\\
\normalsize \noindent {\bf Assertions}: Ensure that the function calculated correctly\\
%
\end{center}
%
\end{frame}
%% Slide
%
\begin{frame}{Mathematical Equations for Root Finding}
%
\vspace*{-.5in}
%
\begin{center}
%
\fontsize{20}{30}\selectfont
%
\begin{equation*}
%
x_1=\frac{-b+\sqrt{b^2-4ac}}{2a}
%
\end{equation*}
%
\begin{equation*}
%
x_2=\frac{-b-\sqrt{b^2-4ac}}{2a}
%
\end{equation*}
%
\end{center}
%
\vspace{.05ex}
%
\begin{center}
%
\small What is the meaning of the ``root'' of a quadratic equation? \\
%
\small For a quadratic equation $f(x)= a \times x^2 + b \times x +c$, the
variables $x_1$ and $x_2$ are the points where the equation's output are
the same\\
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Conditional Logic in Python Programs}
\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 {\tt if} statement allows the program to make a decision \\
\normalsize \noindent The {\tt ==} operator compares {\tt name} to a String literal \\
\normalsize \noindent When the condition is {\tt true} the {\tt print} executes \\
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Iteration Constructs in Python Programs}
\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 {\tt for} loop allows a Python program to repeat an operation \\
\normalsize \noindent This {\tt for} loop ``iterates'' through all of the lines in the file \\
\normalsize \noindent Indentation indicates which statements are in the {\tt for} loop \\
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Using {\tt for} Loops in Python Programs}
\normalsize
\hspace*{-.65in}
\begin{minipage}{6in}
\vspace*{.25in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{python}
for i in range(20):
print("2 to the " + str(i)
+ " power is " + str(2**i))
\end{minted}
\end{minipage}
\vspace*{.25in}
\begin{center}
%
\normalsize \noindent The {\tt for} loop displays the powers of 2 from 0 to 19 \\
\normalsize \noindent The {\tt range} function returns the values from 0 to 19 \\
\normalsize \noindent The {\tt 2**i} performs the computation of $2^i$ \\
\normalsize \noindent The {\tt str} function converts an integer to a string \\
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Using {\tt while} Loops in Python Programs}
\normalsize
\hspace*{-.65in}
\begin{minipage}{6in}
\vspace*{.25in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\large]{python}
i = 0
while i < 20:
print("2 to the " + str(i)
+ " power is " + str(2**i))
i += 1
\end{minted}
\end{minipage}
\vspace*{.1in}
\begin{center}
%
\normalsize \noindent The {\tt for} loop and the {\tt while} loop are equivalent\\
\normalsize \noindent The {\tt print} statement is the same for both of the loops\\
\normalsize \noindent The purpose of {\tt i += 1} is to increment the loop counter\\
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Output of a Python Program Using Iteration}
\normalsize
\hspace*{-.35in}
\begin{minipage}{6in}
\vspace*{.25in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\small]{text}
2 to the 0 power is 1
2 to the 1 power is 2
2 to the 2 power is 4
2 to the 3 power is 8
2 to the 4 power is 16
2 to the 5 power is 32
2 to the 6 power is 64
2 to the 7 power is 128
2 to the 8 power is 256
2 to the 9 power is 512
\end{minted}
\end{minipage}
\vspace*{.05in}
\begin{center}
%
\normalsize \noindent The output displays the counter and the computation\\
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Output of a Python Program Using Iteration}
\normalsize
\hspace*{-.35in}
\begin{minipage}{6in}
\vspace*{.25in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\small]{text}
2 to the 10 power is 1024
2 to the 11 power is 2048
2 to the 12 power is 4096
2 to the 13 power is 8192
2 to the 14 power is 16384
2 to the 15 power is 32768
2 to the 16 power is 65536
2 to the 17 power is 131072
2 to the 18 power is 262144
2 to the 19 power is 524288
\end{minted}
\end{minipage}
\vspace*{.05in}
\begin{center}
%
\normalsize \noindent The {\tt for} and {\tt while} loops create the same output!\\
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}[fragile]
\frametitle{Using a Loop to Approximate Square Roots}
\normalsize
\hspace*{-.45in}
\begin{minipage}{6in}
\vspace*{.25in}
\begin{minted}[mathescape, numbersep=5pt, fontsize=\normalsize]{python}
n = 4
guess = 1.0
while abs(n - guess*guess) > 0.0001:
guess = guess - (guess*guess - n)/(2*guess)
root = guess
\end{minted}
\end{minipage}
\vspace*{.1in}
\begin{center}
%
\normalsize \noindent The {\tt while} loop uses {\tt abs} for computing an absolute value\\
\normalsize \noindent This loop computes the root as 2.0000000929222947\\
\normalsize \noindent The {\tt math.sqrt(n)} function confirms this approximation!\\
%
\end{center}
%
\end{frame}
% Slide
%
\begin{frame}{Computing with Python Programs}
%
\begin{itemize}
%
\item Python programs can express many computations
%
\vspace*{-.15in}
%
\item Key components of the Python programming segments
%
\begin{itemize}
%
\item Function calls
%
\item Assignment statements
%
\item Iteration constructs
%
\item Conditional logic
%
\item Variable creation
%
\item Variable computations
%
\item Variable output
%
\end{itemize}
%
\vspace*{-.2in}
%
\item Programs must have correct format, comments, and tests!
%
\end{itemize}
%
\end{frame}
% Slide
%
\begin{frame}{Investigations in Python and Mathematics}
%
\begin{itemize}
%
\item How do you pick between the {\tt for} and {\tt while} loops?
%
\vspace*{-.15in}
%
\item Program for the root finding of a quadratic equation
%
\begin{itemize}
%
\item {\bf Q1}: What does it mean if a number is imaginary?
%
\item {\bf Q2}: What happens if the root of the equation is imaginary?
%
\item {\bf Q3}: How do tests use assertions for floating point values?
%
\item {\bf Q4}: How can you confirm that the function works correctly?
%
\item {\bf Q5}: How do you know when you have tested enough?
%
\end{itemize}
%
\vspace*{-.2in}
%
\item Can you translate the root finding equation into a Python program?
Can you ensure its correctness? Can you follow industry standards for
comments, format, and testing?
%
\end{itemize}
%
\end{frame}
\end{document}