forked from digixc/7-CS-Turing
-
Notifications
You must be signed in to change notification settings - Fork 4
/
7_Lesson.html
438 lines (416 loc) · 17.4 KB
/
7_Lesson.html
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
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Touring Turing</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="generator" content="Org-mode" />
<link rel="stylesheet" type="text/css" href="css/styles.css"/>
<link href='http://fonts.googleapis.com/css?family=Ubuntu+Mono|Ubuntu' rel='stylesheet' type='text/css'>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<script src="js/navbar.js" type="text/javascript"></script>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<h1 class="title">Touring Turing</h1>
<div class="github-fork-ribbon-wrapper left">
<div class="github-fork-ribbon">
<a href="https://github.com/stsb11/7-CS-Turing">Fork me on GitHub</a>
</div>
</div>
<div id="stickyribbon">
<ul>
<li><a href="1_Lesson.html">Week1</a></li>
<li><a href="2_Lesson.html">Week2</a></li>
<li><a href="3_Lesson.html">Week3</a></li>
<li><a href="4_Lesson.html">Week4</a></li>
<li><a href="5_Lesson.html">Week5</a></li>
<li><a href="6_Lesson.html">Week6</a></li>
<li><a href="7_Lesson.html">Week7</a></li>
<li><a href="assessment.html">Assessment</a></li>
</ul>
</div>
<div id="outline-container-sec-1" class="outline-2 activity">
<h2 id="sec-1"><span class="section-number-2">1</span> Turing Complete</h2>
<div class="outline-text-2" id="text-1">
</div>
<div id="outline-container-sec-1-1" class="outline-3 learn">
<h3 id="sec-1-1">Learn It</h3>
<div class="outline-text-3" id="text-1-1">
<ul class="org-ul">
<li>By combining while loops and conditional selection we can become <i>Turing Complete</i>.
</li>
<li>That means we can write programs that can solve any problem a <i>Turing Machine</i> can solve.
</li>
<li>As <i>Turing Machines</i> can solve any computable problem, that means we'll be able to write programs that can solve any computable problem.
</li>
</ul>
</div>
</div>
<div id="outline-container-sec-1-2" class="outline-3 code">
<h3 id="sec-1-2">Code It</h3>
<div class="outline-text-3" id="text-1-2">
<ul class="org-ul">
<li>Before we get started on learning about mixing conditional selection and loops, we need to look at an important aspect of programming that you may find useful.
</li>
<li>Sometimes when we are writing scripts, we want to prevent lines from being executed, but we don't want to delete them.
</li>
<li>Other times we want to add in little explanations into our code, so that someone reading it can understand what is going on.
</li>
<li>For both of these things, we use <code>comments</code>
</li>
</ul>
</div>
</div>
<div id="outline-container-sec-1-3" class="outline-3 code">
<h3 id="sec-1-3">Code It</h3>
<div class="outline-text-3" id="text-1-3">
<ul class="org-ul">
<li>A <code>commented</code> line in Python starts with a hash - <code>#</code>
</li>
<li>Copy and paste this code into a new script.
</li>
</ul>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #268bd2;">x</span> = 10
<span style="color: #268bd2;">x</span> = 20
<span style="color: #859900; font-weight: bold;">while</span> x > 0:
<span style="color: #859900; font-weight: bold;">print</span>(x)
<span style="color: #268bd2;">x</span> = x - 1
</pre>
</div>
<ul class="org-ul">
<li>Run the code and see what happens.
</li>
</ul>
<iframe src="https://trinket.io/embed/python/36fa8c2e86" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
<ul class="org-ul">
<li>Now we're going to edit the code a little.
</li>
</ul>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #268bd2;">x</span> = 10
<span style="color: #93a1a1;">#</span><span style="color: #93a1a1;">x = 20</span>
<span style="color: #859900; font-weight: bold;">while</span> x > 0:
<span style="color: #859900; font-weight: bold;">print</span>(x)
<span style="color: #268bd2;">x</span> = x - 1
</pre>
</div>
<ul class="org-ul">
<li>Run the code again.
</li>
<li>You'll notice that the line with a <code>#</code> symbol in it did not get executed, so x was never reassigned to the value <code>20</code>
</li>
<li>We can also do this:
</li>
</ul>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #268bd2;">x</span> = 10 <span style="color: #93a1a1;">#</span><span style="color: #93a1a1;">Assign x to the value 10</span>
<span style="color: #93a1a1;">#</span><span style="color: #93a1a1;">x = 20</span>
<span style="color: #859900; font-weight: bold;">while</span> x > 0: <span style="color: #93a1a1;">#</span><span style="color: #93a1a1;">Loop until x reaches 0</span>
<span style="color: #859900; font-weight: bold;">print</span>(x)
<span style="color: #268bd2;">x</span> = x - 1 <span style="color: #93a1a1;">#</span><span style="color: #93a1a1;">Subtract 1 from x.</span>
</pre>
</div>
<ul class="org-ul">
<li>You'll notice that this code runs without any problems, but the comments provide helpful information to anyone who is reading your code.
</li>
</ul>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #93a1a1;">#</span><span style="color: #93a1a1;">x = 10 #Assign x to the value 10</span>
<span style="color: #93a1a1;">#</span><span style="color: #93a1a1;">x = 20</span>
<span style="color: #859900; font-weight: bold;">while</span> x > 0: <span style="color: #93a1a1;">#</span><span style="color: #93a1a1;">Loop until x reaches 0</span>
<span style="color: #859900; font-weight: bold;">print</span>(x)
<span style="color: #268bd2;">x</span> = x - 1 <span style="color: #93a1a1;">#</span><span style="color: #93a1a1;">Subtract 1 from x.</span>
</pre>
</div>
<ul class="org-ul">
<li>Here we've gone a little too far with our comments.
</li>
<li>As the lines that assign a value to <code>x</code> have both been commented out, it's as if they're not there at all, and so the <code>while</code> loop doesn't make any sense.
</li>
</ul>
</div>
</div>
<div id="outline-container-sec-1-4" class="outline-3 code">
<h3 id="sec-1-4">Code It</h3>
<div class="outline-text-3" id="text-1-4">
<ul class="org-ul">
<li>To get used to combining loops and conditional selection, let's make another simple guessing game.
</li>
<li>Here's the code
</li>
</ul>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #268bd2;">number</span> = 7
<span style="color: #268bd2;">guess</span> = <span style="color: #268bd2; font-weight: bold;">None</span>
<span style="color: #859900; font-weight: bold;">while</span> guess != number:
<span style="color: #268bd2;">guess</span> = <span style="color: #657b83; font-weight: bold;">int</span>(<span style="color: #657b83; font-weight: bold;">input</span>(<span style="color: #2aa198;">'What number am I thinking of? '</span>))
<span style="color: #859900; font-weight: bold;">if</span> guess > number:
<span style="color: #859900; font-weight: bold;">print</span>(<span style="color: #2aa198;">'Too high'</span>)
<span style="color: #859900; font-weight: bold;">elif</span> guess < number:
<span style="color: #859900; font-weight: bold;">print</span>(<span style="color: #2aa198;">'Too low'</span>)
<span style="color: #859900; font-weight: bold;">else</span>:
<span style="color: #859900; font-weight: bold;">print</span>(<span style="color: #2aa198;">'Well done'</span>)
</pre>
</div>
<ul class="org-ul">
<li>Create a new script and call it <code>guessAnumber.py</code>
</li>
<li>Copy and paste the code in above, or use the Trinket below:
</li>
</ul>
<iframe src="https://trinket.io/embed/python/5a13331386" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
<ul class="org-ul">
<li>Run the code and make sure you are familiar with how it works.
</li>
</ul>
</div>
</div>
<div id="outline-container-sec-1-5" class="outline-3 badge">
<h3 id="sec-1-5">Badge It - Silver</h3>
<div class="outline-text-3" id="text-1-5">
<ul class="org-ul">
<li>Add comments to the end of each line of code that explains the purpose of the line.
</li>
<li>For instance:
</li>
</ul>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #268bd2;">number</span> = 7 <span style="color: #93a1a1;">#</span><span style="color: #93a1a1;">Assign the value 7 to the variable number</span>
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-sec-2" class="outline-2 activity">
<h2 id="sec-2"><span class="section-number-2">2</span> Solving Computable problems</h2>
<div class="outline-text-2" id="text-2">
</div>
<div id="outline-container-sec-2-1" class="outline-3 learn">
<h3 id="sec-2-1">Learn It</h3>
<div class="outline-text-3" id="text-2-1">
<ul class="org-ul">
<li>A <i>Turing Machine</i> can solve any problem that is computable.
</li>
<li>Up to now, your scripts have been fairly simple, and not accomplished any real useful work.
</li>
<li>Let's try and make something useful
</li>
</ul>
</div>
</div>
<div id="outline-container-sec-2-2" class="outline-3 code">
<h3 id="sec-2-2">Code It</h3>
<div class="outline-text-3" id="text-2-2">
<ul class="org-ul">
<li>Finding square roots of a number are pretty hard.
</li>
<li>There are some special formulas you can use to calculate a square root, but we're going to harness the power and speed of a computer in our algorithm.
</li>
<li>Let's say we wanted to find the square root of <code>16129</code>
</li>
<li>What we need to know is which number, when multiplied by itself, equals <code>16129</code>
</li>
</ul>
<div class="org-src-container">
<pre class="src src-python">sqrRt * sqrRt == 16129
</pre>
</div>
<ul class="org-ul">
<li>Because computers can run calculations pretty fast, we can test every number below <code>16129</code>, multiply it by it by itself and then see if it equals <code>16129</code>
</li>
</ul>
<div class="org-src-container">
<pre class="src src-python">>>> 16128 * 16128 == 16129
<span style="color: #268bd2; font-weight: bold;">False</span>
>>> 16127 * 16127 == 16129
<span style="color: #268bd2; font-weight: bold;">False</span>
>>> 16126 * 16126 == 16129
<span style="color: #268bd2; font-weight: bold;">False</span>
</pre>
</div>
<ul class="org-ul">
<li>We can keep doing this until we get the answer True
</li>
<li>Let's see if we can code this in Python
</li>
</ul>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #268bd2;">number</span> = 16129
<span style="color: #268bd2;">test</span> = 16128
<span style="color: #268bd2;">stop</span> = <span style="color: #268bd2; font-weight: bold;">False</span>
<span style="color: #859900; font-weight: bold;">while</span> stop == <span style="color: #268bd2; font-weight: bold;">False</span>:
<span style="color: #859900; font-weight: bold;">if</span> test * test == number:
<span style="color: #859900; font-weight: bold;">print</span>(test)
<span style="color: #268bd2;">stop</span> = <span style="color: #268bd2; font-weight: bold;">True</span>
<span style="color: #859900; font-weight: bold;">else</span>:
<span style="color: #268bd2;">test</span> = test - 1
</pre>
</div>
<iframe src="https://trinket.io/embed/python/a13820cfb0" width="100%" height="350" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
<ul class="org-ul">
<li>Try it with a few other numbers.
</li>
<li>What happens when you use the number <code>152399025</code>?
</li>
</ul>
</div>
</div>
<div id="outline-container-sec-2-3" class="outline-3 badge">
<h3 id="sec-2-3">Badge It - Gold</h3>
<div class="outline-text-3" id="text-2-3">
<ul class="org-ul">
<li>Alter the script above so that instead of <code>hard coding</code> the number, it asks the user for a number (Don't forget to type cast)
</li>
<li>Make sure the variable <code>test</code> is assigned to one less than the number to be tested.
</li>
<li>When the program prints out the answer (if it finds one) it should say something like - <code>The square-root is ....</code>
</li>
</ul>
</div>
</div>
</div>
<div id="outline-container-sec-3" class="outline-2 activity">
<h2 id="sec-3"><span class="section-number-2">3</span> Solving extra tricky problems</h2>
<div class="outline-text-2" id="text-3">
</div>
<div id="outline-container-sec-3-1" class="outline-3 learn">
<h3 id="sec-3-1">Learn It</h3>
<div class="outline-text-3" id="text-3-1">
<ul class="org-ul">
<li>We all have problems, and we're all very busy.
</li>
<li>Computers can perform calculations so quickly, that they can solve problems billions of times faster than a human ever could. So even if we have problems to solve that would take us too much time, we can get a computer to do it for us.
</li>
<li>The problem has to be a <code>computable</code> one though.
</li>
<li>Too often people believe that computers can solve any problem…
</li>
</ul>
<div class="figure">
<p><img src="http://imgs.xkcd.com/comics/tasks.png" alt="tasks.png" />
</p>
</div>
</div>
</div>
<div id="outline-container-sec-3-2" class="outline-3 learn">
<h3 id="sec-3-2">Learn It</h3>
<div class="outline-text-3" id="text-3-2">
<ul class="org-ul">
<li>You've already encountered an infinite loop.
</li>
<li>Here's another one you can try if you've forgotten what an infinite loop is.
</li>
</ul>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #268bd2;">x</span> = <span style="color: #268bd2; font-weight: bold;">True</span>
<span style="color: #859900; font-weight: bold;">while</span> x == <span style="color: #268bd2; font-weight: bold;">True</span>:
<span style="color: #859900; font-weight: bold;">print</span>(<span style="color: #2aa198;">'Does this program halt?'</span>)
<span style="color: #859900; font-weight: bold;">print</span>(<span style="color: #2aa198;">'\n'</span>*3)
</pre>
</div>
<ul class="org-ul">
<li>Infinite loops are a problem in designing programs. Have you ever had an application that just freezes until you are forced to kill it?
</li>
</ul>
<div class="figure">
<p><img src="https://cdn2.scratch.mit.edu/get_image/user/4491013_60x60.png" alt="4491013_60x60.png" />
</p>
</div>
<div class="figure">
<p><img src="https://upload.wikimedia.org/wikipedia/en/3/3d/WaitCursor-300p.gif" alt="WaitCursor-300p.gif" />
</p>
</div>
<ul class="org-ul">
<li>Wouldn't it be nice if we could have some way of detecting if a program would enter an infinite loop?
</li>
</ul>
</div>
</div>
<div id="outline-container-sec-3-3" class="outline-3 research">
<h3 id="sec-3-3">Research It</h3>
<div class="outline-text-3" id="text-3-3">
<ul class="org-ul">
<li>Imagine we create a program that we'll call A.
</li>
<li>Could a program exist that can read the code of A and determine if A contains an infinite loop?
</li>
<li>This problem is known as <code>The Halting Problem</code>.
</li>
<li>Is it possible to design a program that can detect if another program will <i>halt</i> or just go into an <i>infinite loop</i>?
</li>
<li>There are lots of resources online about <code>The Halting Problem</code>.
</li>
<li>You might like to have a read of<a href="http://www.lel.ed.ac.uk/~gpullum/loopsnoop.html"> <i>Scooping The Loop Snooper</i></a>
</li>
<li>Or have a look at the video below.
</li>
</ul>
<iframe width="560" height="315" src="https://www.youtube.com/embed/92WHN-pAFCs" frameborder="0" allowfullscreen></iframe>
<ul class="org-ul">
<li>Or try an find your own explanations online.
</li>
</ul>
</div>
</div>
<div id="outline-container-sec-3-4" class="outline-3 badge">
<h3 id="sec-3-4">Badge It - Platinum</h3>
<div class="outline-text-3" id="text-3-4">
<ul class="org-ul">
<li>Try and write your own explanation for Alan Turing's proof that <i>The Halting Problem</i> is undecidable.
</li>
<li>Be sure to write this in your own words - <b>Don't copy and paste answers</b>
</li>
</ul>
</div>
</div>
</div>
</div>
<div id="postamble" class="status">
<p class="validation"></p>
</div>
</body>
</html>