The Prison Break | Think Like A Coder, Ep 1

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

TED-Ed


請雙擊下方英文字幕播放視頻。

譯者: Lilian Chiu 審譯者: Harper Chang
《用程式設計師的方式思考》
[ 位置:監獄 ]
[ 第一集:逃獄 ]
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
嘗試 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
如果不行,再嘗試 3,
一直嘗試到 100。
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
稱為「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