🔍
The Prison Break | Think Like A Coder, Ep 1 - YouTube
Channel: TED-Ed
[29]
Upon emerging from stasis, Ethic is the
unfortunate recipient of three surprises.
[37]
The first: a prison cell.
[40]
The second: complete amnesia.
[43]
And the third: a mysterious stranger
has gotten stuck
[47]
squeezing through the bars on her window.
[50]
His name is Hedge, and he has come
to help Ethic save the world.
[56]
But first they have to break out of jail.
[59]
Hedge turns his hand into a lockpick and
outlines the challenge ahead.
[65]
Each lock in the prison works
in the same unusual way.
[70]
Inside the keyhole is a red dial that can
be rotated to one of 100 positions
[77]
numbered 1 through 100.
[79]
The key for a given cell spins the dial
to the right position,
[84]
which, when stopped there, makes it turn
green and unlocks the door.
[88]
It would be out of the question to steal
keys from a guard,
[93]
but Hedge has a better idea.
[96]
Hedge can carry out Ethic‘s commands.
[99]
If Ethic tells him to walk
5 steps forward,
[102]
turn right, then walk another 5 steps,
[106]
that’s exactly what he’ll do.
[109]
Hedge needs specific instructions though.
[112]
If Ethic says “pick the lock” or
“try every combination”
[117]
that would be too vague, but “spin the
dial 5 positions forward” would work.
[123]
Once out of the cell, they will only have
a few moments to crack the lock
[127]
for the outer prison door too before
the guards catch them.
[132]
So what instructions will allow Hedge
to efficiently open any door?
[140]
Pause now to figure it out for yourself.
[145]
Before we explain the solution,
here’s a hint.
[150]
A key programming concept that can help
unlock the door is called a loop.
[157]
This can be one or more instructions
that Hedge will iterate— or repeat—
[162]
a specified number of times,
[166]
like “jump up and down 100 times.”
[170]
Or an instruction that Hedge will
repeat until a condition is met,
[175]
such as “keep jumping up and
down until it’s 7 o’clock.”
[180]
Pause now to figure it out for yourself.
[186]
The first thing that’s clear is that you
need to find a way for Hedge
[190]
to try every combination until one works.
[194]
What takes a little more effort is
how exactly you do so.
[198]
One solution would be to instruct Hedge
to try every combination in succession.
[203]
Try 1 and check the light.
[206]
If it turns green, open the door,
and if not, try 2.
[212]
If that doesn’t work try 3.
All the way up to 100.
[218]
But it would be tedious to lay
that out in its entirety.
[221]
Why write more than 100 lines of code,
[224]
when you can do the same thing
with just 3?
[228]
This is where a loop comes in.
[230]
There are a few ways to go about this.
[233]
The lock has 100 positions,
[236]
so Ethic could say “Check the dial’s
color, then spin the dial forward once,
[242]
for 100 repetitions.
[246]
Remember where the dial turns green, then
have Hedge set it back to that number.”
[253]
A loop like this, where you specify
the number of times it repeats,
[257]
is called a “for" loop.
[260]
But an even more efficient loop
[262]
would have Hedge spin the dial one
position at a time until it turns green
[267]
and as soon as that happens, have him
stop and open the door.
[271]
That way if the door unlocks on 1,
[275]
he doesn’t need to cycle through
all the rest of the numbers.
[279]
This is an “until” loop,
[282]
because it involves doing an
action until a condition is met.
[287]
A similar, alternate approach would be
to turn the dial while it’s still red,
[292]
then stop.
[294]
That’s called a “while” loop.
[298]
Back to the adventure.
[299]
Hedge loops through the combinations,
and the cell opens at 41.
[307]
Ethic and Hedge wait until the
perfect moment in the guards’ rotation
[310]
and make a break for it.
[314]
Soon, Ethic faces a choice:
hide inside a mysterious crystal,
[319]
or try to crack the outer door
and make a run for it.
[323]
Ethic chooses to run.
[328]
The second door takes Hedge longer,
requiring him to spin all the way to 93.
[334]
But he gets it open
[343]
and takes the opportunity
to explain why he’s rescued Ethic.
[348]
The world is in turmoil:
[350]
robots have taken over,
and only Ethic can set things right.
[354]
In order to do so,
[355]
they’ll need to collect three
powerful artifacts
[358]
that are being used for nefarious
purposes across the land.
[361]
Only then can Ethic return to the
world machine— that giant crystal—
[365]
to set things right.
[379]
Ethic may have escaped the prison…
[382]
but what has she gotten herself into?
Most Recent Videos:
You can go back to the homepage right here: Homepage





