The Prison Break | Think Like A Coder, Ep 1

1,770,244 views ・ 2019-09-30

TED-Ed


Please double-click on the English subtitles below to play the video.

00:29
Upon emerging from stasis, Ethic is the unfortunate recipient of three surprises.
0
29937
7440
00:37
The first: a prison cell.
1
37377
2962
00:40
The second: complete amnesia.
2
40339
3165
00:43
And the third: a mysterious stranger has gotten stuck
3
43504
3708
00:47
squeezing through the bars on her window.
4
47212
3655
00:50
His name is Hedge, and he has come to help Ethic save the world.
5
50867
5558
00:56
But first they have to break out of jail.
6
56425
3531
00:59
Hedge turns his hand into a lockpick and outlines the challenge ahead.
7
59956
6017
01:05
Each lock in the prison works in the same unusual way.
8
65973
4650
01:10
Inside the keyhole is a red dial that can be rotated to one of 100 positions
9
70623
6628
01:17
numbered 1 through 100.
10
77251
2448
01:19
The key for a given cell spins the dial to the right position,
11
79699
4344
01:24
which, when stopped there, makes it turn green and unlocks the door.
12
84043
4870
01:28
It would be out of the question to steal keys from a guard,
13
88913
4715
01:33
but Hedge has a better idea.
14
93628
3137
01:36
Hedge can carry out Ethic‘s commands.
15
96765
2731
01:39
If Ethic tells him to walk 5 steps forward,
16
99496
3200
01:42
turn right, then walk another 5 steps,
17
102696
3475
01:46
that’s exactly what he’ll do.
18
106171
3552
01:49
Hedge needs specific instructions though.
19
109723
3121
01:52
If Ethic says “pick the lock” or “try every combination”
20
112844
4156
01:57
that would be too vague, but “spin the dial 5 positions forward” would work.
21
117000
6685
02:03
Once out of the cell, they will only have a few moments to crack the lock
22
123685
4022
02:07
for the outer prison door too before the guards catch them.
23
127707
4518
02:12
So what instructions will allow Hedge to efficiently open any door?
24
132225
5480
02:20
Pause now to figure it out for yourself.
25
140325
4070
02:25
Before we explain the solution, here’s a hint.
26
145775
4977
02:30
A key programming concept that can help unlock the door is called a loop.
27
150752
6428
02:37
This can be one or more instructions that Hedge will iterate— or repeat—
28
157180
5330
02:42
a specified number of times,
29
162510
4144
02:46
like “jump up and down 100 times.”
30
166654
3411
02:50
Or an instruction that Hedge will repeat until a condition is met,
31
170065
5180
02:55
such as “keep jumping up and down until it’s 7 o’clock.”
32
175245
4986
03:00
Pause now to figure it out for yourself.
33
180231
6017
03:06
The first thing that’s clear is that you need to find a way for Hedge
34
186248
3888
03:10
to try every combination until one works.
35
190136
4094
03:14
What takes a little more effort is how exactly you do so.
36
194230
4290
03:18
One solution would be to instruct Hedge to try every combination in succession.
37
198520
5280
03:23
Try 1 and check the light.
38
203800
2705
03:26
If it turns green, open the door, and if not, try 2.
39
206505
5848
03:32
If that doesn’t work try 3. All the way up to 100.
40
212353
5890
03:38
But it would be tedious to lay that out in its entirety.
41
218243
3574
03:41
Why write more than 100 lines of code,
42
221817
2436
03:44
when you can do the same thing with just 3?
43
224253
4135
03:48
This is where a loop comes in.
44
228388
2295
03:50
There are a few ways to go about this.
45
230683
3225
03:53
The lock has 100 positions,
46
233908
2355
03:56
so Ethic could say “Check the dial’s color, then spin the dial forward once,
47
236263
6051
04:02
for 100 repetitions.
48
242314
3882
04:06
Remember where the dial turns green, then have Hedge set it back to that number.”
49
246196
6816
04:13
A loop like this, where you specify the number of times it repeats,
50
253012
4167
04:17
is called a “for" loop.
51
257179
2986
04:20
But an even more efficient loop
52
260165
1942
04:22
would have Hedge spin the dial one position at a time until it turns green
53
262107
5250
04:27
and as soon as that happens, have him stop and open the door.
54
267357
4487
04:31
That way if the door unlocks on 1,
55
271844
3409
04:35
he doesn’t need to cycle through all the rest of the numbers.
56
275253
3995
04:39
This is an “until” loop,
57
279248
3414
04:42
because it involves doing an action until a condition is met.
58
282662
4780
04:47
A similar, alternate approach would be to turn the dial while it’s still red,
59
287442
4769
04:52
then stop.
60
292211
2099
04:54
That’s called a “while” loop.
61
294310
3732
04:58
Back to the adventure.
62
298042
1888
04:59
Hedge loops through the combinations, and the cell opens at 41.
63
299930
7100
05:07
Ethic and Hedge wait until the perfect moment in the guards’ rotation
64
307030
3921
05:10
and make a break for it.
65
310951
3529
05:14
Soon, Ethic faces a choice: hide inside a mysterious crystal,
66
314480
5407
05:19
or try to crack the outer door and make a run for it.
67
319887
3964
05:23
Ethic chooses to run.
68
323851
2266
05:28
The second door takes Hedge longer, requiring him to spin all the way to 93.
69
328227
6416
05:34
But he gets it open
70
334643
2550
05:43
and takes the opportunity to explain why he’s rescued Ethic.
71
343871
4870
05:48
The world is in turmoil:
72
348741
1728
05:50
robots have taken over, and only Ethic can set things right.
73
350469
4073
05:54
In order to do so,
74
354542
1153
05:55
they’ll need to collect three powerful artifacts
75
355695
2466
05:58
that are being used for nefarious purposes across the land.
76
358161
3462
06:01
Only then can Ethic return to the world machine— that giant crystal—
77
361623
4348
06:05
to set things right.
78
365971
1500
06:19
Ethic may have escaped the prison…
79
379685
2840
06:22
but what has she gotten herself into?
80
382525
2930
About this website

This site will introduce you to YouTube videos that are useful for learning English. You will see English lessons taught by top-notch teachers from around the world. Double-click on the English subtitles displayed on each video page to play the video from there. The subtitles scroll in sync with the video playback. If you have any comments or requests, please contact us using this contact form.

https://forms.gle/WvT1wiN1qDtmnspy7