The Prison Break | Think Like A Coder, Ep 1
-
0:30 - 0:37Upon emerging from stasis, Ethic is the
unfortunate recipient of three surprises. -
0:37 - 0:40The first: a prison cell.
-
0:40 - 0:44The second: complete amnesia.
-
0:44 - 0:47And the third: a mysterious stranger
has gotten stuck -
0:47 - 0:51squeezing through the bars on her window.
-
0:51 - 0:56His name is Hedge, and he has come
to help Ethic save the world. -
0:56 - 1:00But first they have to break out of jail.
-
1:00 - 1:06Hedge turns his hand into a lockpick and
outlines the challenge ahead. -
1:06 - 1:11Each lock in the prison works
in the same unusual way. -
1:11 - 1:17Inside the keyhole is a red dial that can
be rotated to one of 100 positions -
1:17 - 1:20numbered 1 through 100.
-
1:20 - 1:24The key for a given cell spins the dial
to the right position, -
1:24 - 1:29which, when stopped there, makes it turn
green and unlocks the door. -
1:29 - 1:34It would be out of the question to steal
keys from a guard, -
1:34 - 1:37but Hedge has a better idea.
-
1:37 - 1:39Hedge can carry out Ethic‘s commands.
-
1:39 - 1:43If Ethic tells him to walk
5 steps forward, -
1:43 - 1:46turn right, then walk another 5 steps,
-
1:46 - 1:50that’s exactly what he’ll do.
-
1:50 - 1:53Hedge needs specific instructions though.
-
1:53 - 1:57If Ethic says “pick the lock” or
“try every combination” -
1:57 - 2:04that would be too vague, but “spin the
dial 5 positions forward” would work. -
2:04 - 2:08Once out of the cell, they will only have
a few moments to crack the lock -
2:08 - 2:12for the outer prison door too before
the guards catch them. -
2:12 - 2:18So what instructions will allow Hedge
to efficiently open any door? -
2:20 - 2:24Pause now to figure it out for yourself.
-
2:26 - 2:31Before we explain the solution,
here’s a hint. -
2:31 - 2:37A key programming concept that can help
unlock the door is called a loop. -
2:37 - 2:43This can be one or more instructions
that Hedge will iterate— or repeat— -
2:43 - 2:47a specified number of times,
-
2:47 - 2:50like “jump up and down 100 times.”
-
2:50 - 2:55Or an instruction that Hedge will
repeat until a condition is met, -
2:55 - 3:00such as “keep jumping up and
down until it’s 7 o’clock.” -
3:00 - 3:06Pause now to figure it out for yourself.
-
3:06 - 3:10The first thing that’s clear is that you
need to find a way for Hedge -
3:10 - 3:14to try every combination until one works.
-
3:14 - 3:19What takes a little more effort is
how exactly you do so. -
3:19 - 3:24One solution would be to instruct Hedge
to try every combination in succession. -
3:24 - 3:27Try 1 and check the light.
-
3:27 - 3:32If it turns green, open the door,
and if not, try 2. -
3:32 - 3:38If that doesn’t work try 3.
All the way up to 100. -
3:38 - 3:42But it would be tedious to lay
that out in its entirety. -
3:42 - 3:44Why write more than 100 lines of code,
-
3:44 - 3:48when you can do the same thing
with just 3? -
3:48 - 3:51This is where a loop comes in.
-
3:51 - 3:54There are a few ways to go about this.
-
3:54 - 3:56The lock has 100 positions,
-
3:56 - 4:02so Ethic could say “Check the dial’s
color, then spin the dial forward once, -
4:02 - 4:06for 100 repetitions.
-
4:06 - 4:13Remember where the dial turns green, then
have Hedge set it back to that number.” -
4:13 - 4:17A loop like this, where you specify
the number of times it repeats, -
4:17 - 4:20is called a “for" loop.
-
4:20 - 4:22But an even more efficient loop
-
4:22 - 4:27would have Hedge spin the dial one
position at a time until it turns green -
4:27 - 4:32and as soon as that happens, have him
stop and open the door. -
4:32 - 4:35That way if the door unlocks on 1,
-
4:35 - 4:39he doesn’t need to cycle through
all the rest of the numbers. -
4:39 - 4:43This is an “until” loop,
-
4:43 - 4:47because it involves doing an
action until a condition is met. -
4:47 - 4:52A similar, alternate approach would be
to turn the dial while it’s still red, -
4:52 - 4:54then stop.
-
4:54 - 4:58That’s called a “while” loop.
-
4:58 - 5:00Back to the adventure.
-
5:00 - 5:07Hedge loops through the combinations,
and the cell opens at 41. -
5:07 - 5:11Ethic and Hedge wait until the
perfect moment in the guards’ rotation -
5:11 - 5:14and make a break for it.
-
5:14 - 5:20Soon, Ethic faces a choice:
hide inside a mysterious crystal, -
5:20 - 5:24or try to crack the outer door
and make a run for it. -
5:24 - 5:26Ethic chooses to run.
-
5:28 - 5:35The second door takes Hedge longer,
requiring him to spin all the way to 93. -
5:35 - 5:37But he gets it open
-
5:44 - 5:49and takes the opportunity
to explain why he’s rescued Ethic. -
5:49 - 5:50The world is in turmoil:
-
5:50 - 5:55robots have taken over,
and only Ethic can set things right. -
5:55 - 5:56In order to do so,
-
5:56 - 5:58they’ll need to collect three
powerful artifacts -
5:58 - 6:02that are being used for nefarious
purposes across the land. -
6:02 - 6:06Only then can Ethic return to the
world machine— that giant crystal— -
6:06 - 6:07to set things right.
-
6:20 - 6:23Ethic may have escaped the prison…
-
6:23 - 6:25but what has she gotten herself into?
- Title:
- The Prison Break | Think Like A Coder, Ep 1
- Speaker:
- Alex Rosenthal
- Description:
-
View full lesson: https://ed.ted.com/lessons/the-prison-break-think-like-a-coder-ep-1
This is episode 1 of our animated series “Think Like A Coder.” This 10-episode narrative follows a girl, Ethic, and her robot companion, Hedge, as they attempt to save the world. The two embark on a quest to collect three artifacts and must solve their way through a series of programming puzzles.
Lesson by Alex Rosenthal, directed by Kozmonot Animation Studio.
- Video Language:
- English
- Team:
- closed TED
- Project:
- TED-Ed
- Duration:
- 06:34
lauren mcalpine edited English subtitles for The Prison Break | Think Like A Coder, Ep 1 | ||
lauren mcalpine edited English subtitles for The Prison Break | Think Like A Coder, Ep 1 | ||
Elise Haadsma approved English subtitles for The Prison Break | Think Like A Coder, Ep 1 | ||
Elise Haadsma accepted English subtitles for The Prison Break | Think Like A Coder, Ep 1 | ||
lauren mcalpine edited English subtitles for The Prison Break | Think Like A Coder, Ep 1 |