Return to Video

The Prison Break | Think Like A Coder, Ep 1

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

View full lesson:

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.

more » « less
Video Language:
English
Team:
closed TED
Project:
TED-Ed
Duration:
06:34

English subtitles

Revisions Compare revisions