The Prison Break | Think Like A Coder, Ep 1

1,778,274 views ・ 2019-09-30

TED-Ed


请双击下面的英文字幕来播放视频。

翻译人员: Carol Wang 校对人员: Jiasi Hao
[ 像程序员一样思考 ]
[ 地点:监狱 ]
[ 第一集 越狱 ]
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
钥匙孔内部是一个红色拨盘,
可以转至数字 1 到 100 的 任意一位置。
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
如果艾斯克让他先前行 5 步,
01:42
turn right, then walk another 5 steps,
17
102696
3475
右转拨盘,接着再前行 5 步,
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
指令就太过模糊, 但“拨盘转 5 个位置”就可行。
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
如“上下跳跃 100 次”。
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
如“上下跳跃直到 7 点钟”。
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
尝试 1, 并检查指示灯。
03:26
If it turns green, open the door, and if not, try 2.
39
206505
5848
如果灯变绿色,打开门; 否则尝试 2 。
03:32
If that doesn’t work try 3. All the way up to 100.
40
212353
5890
如果 2 不行,尝试 3 …… 一直尝试到 100 。
03:38
But it would be tedious to lay that out in its entirety.
41
218243
3574
但若将 100 个组合 全部试一遍就太繁琐了,
03:41
Why write more than 100 lines of code,
42
221817
2436
只用 3 行代码能完成同样的事,
03:44
when you can do the same thing with just 3?
43
224253
4135
为何要编写 100 多行的代码呢?
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
锁内有 100 个 孔位,
03:56
so Ethic could say “Check the dial’s color, then spin the dial forward once,
47
236263
6051
艾斯克可以说:
“检查拨盘颜色,然后转 1 次, 重复转动 100 次。
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
称为 “for” 循环。
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
采用这种方法的话,
如果转到 1 门就开了, 他没必要继续转拨盘了。
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
上面就是一个 “until” 循环,
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
这就是 “while” 循环。
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
海吉循环转动拨盘组合, 转到 41 时,牢门开了。
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
第二道门花了更长时间, 海吉一直旋转拨盘直到 93 。
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
但是她又陷入了什么困境呢?
关于本网站

这个网站将向你介绍对学习英语有用的YouTube视频。你将看到来自世界各地的一流教师教授的英语课程。双击每个视频页面上显示的英文字幕,即可从那里播放视频。字幕会随着视频的播放而同步滚动。如果你有任何意见或要求,请使用此联系表与我们联系。

https://forms.gle/WvT1wiN1qDtmnspy7