< Return to Video

The Chasm | Think Like A Coder, Ep 6

  • 0:22 - 0:27
    Ethic, Hedge, and Octavia
    stand on the edge of a bottomless ravine.
  • 0:27 - 0:29
    It’s the only thing between
    them and the tower
  • 0:29 - 0:33
    that houses the second
    of three powerful artifacts.
  • 0:33 - 0:38
    They’ve got a brief window of time
    to get across before the guards return.
  • 0:38 - 0:43
    With Hedge’s fuel gauge on empty
    he won’t be able to fly Ethic across,
  • 0:43 - 0:46
    so the only option is to make a bridge.
  • 0:46 - 0:51
    Fortunately, the floating stacks of stones
    nearby are bridge components—
  • 0:51 - 0:55
    invented by Octavia herself—
    called hover-blocks.
  • 0:55 - 0:57
    Activate a pile with a burst of energy,
  • 0:57 - 1:02
    and they’ll self-assemble
    to span the ravine as Ethic walks across.
  • 1:02 - 1:06
    But there is, of course, a catch.
  • 1:06 - 1:10
    The hover-blocks are only stable
    when they’re perfectly palindromic.
  • 1:10 - 1:13
    Meaning they have to form
    a sequence
  • 1:13 - 1:17
    that’s the same when viewed
    forwards and backwards.
  • 1:17 - 1:19
    The stacks start in random orders,
  • 1:19 - 1:23
    but will always put themselves
    into a palindromic configuration
  • 1:23 - 1:24
    if they can.
  • 1:24 - 1:27
    If they get to a point
    where a palindrome isn’t possible,
  • 1:27 - 1:28
    the bridge will collapse,
  • 1:28 - 1:32
    and whoever’s on it
    will fall into the ravine.
  • 1:32 - 1:33
    Let’s look at an example.
  • 1:33 - 1:36
    This stack would make itself stable.
  • 1:36 - 1:39
    First the A blocks
    hold themselves in place.
  • 1:39 - 1:40
    Then the B’s.
  • 1:40 - 1:44
    And finally the C
    would nestle right between the B’s.
  • 1:44 - 1:47
    However, suppose there was one more A.
  • 1:47 - 1:50
    First two A blocks form up, then two B’s,
  • 1:50 - 1:54
    but now the remaining C and A
    have nowhere to go,
  • 1:54 - 1:56
    so the whole thing falls apart.
  • 1:56 - 2:01
    The Node of Power enables Hedge
    to energize a single stack of blocks.
  • 2:01 - 2:05
    What instructions can Ethic give Hedge
    to allow him to efficiently find
  • 2:05 - 2:08
    and power a stable palindromic stack?
  • 2:08 - 2:18
    Pause now to figure it out for yourself.
  • 2:18 - 2:24
    Examples of palindromes include ANNA,
    RACECAR, and MADAM IM ADAM.
  • 2:24 - 2:27
    Counting the number of times
    a given letter appears in a palindrome
  • 2:27 - 2:30
    will reveal a helpful pattern.
  • 2:30 - 2:35
    Pause now to figure it out for yourself.
  • 2:35 - 2:38
    Let’s first look at a naïve solution
    to this problem.
  • 2:38 - 2:43
    A naïve solution is a simple,
    brute-force approach that isn’t optimized—
  • 2:43 - 2:45
    but will get the job done.
  • 2:45 - 2:48
    Naïve solutions are helpful ways
    to analyze problems,
  • 2:48 - 2:52
    and work as stepping stones
    to better solutions.
  • 2:52 - 2:56
    In this case, a naïve solution
    is to approach a pile of blocks,
  • 2:56 - 2:57
    try all the arrangements,
  • 2:57 - 3:02
    and see if one is a palindrome
    by reading it forward and then backwards.
  • 3:02 - 3:03
    The problem with this approach
  • 3:03 - 3:06
    is that it would take
    a tremendous amount of time.
  • 3:06 - 3:09
    If Hedge tried one combination
    every second,
  • 3:09 - 3:14
    a stack of just 10 different blocks
    would take him 42 days to exhaust.
  • 3:14 - 3:18
    That’s because the total time
    is a function of the factorial
  • 3:18 - 3:20
    of the number of blocks there are.
  • 3:20 - 3:23
    10 blocks
    have over 3 million combinations.
  • 3:23 - 3:28
    What this naïve solution shows
    is that we need a much faster way
  • 3:28 - 3:31
    to tell whether a pile of blocks
    can form a palindrome.
  • 3:31 - 3:36
    To start, it may be intuitively clear
    that a pile of all different blocks
  • 3:36 - 3:37
    will never form one.
  • 3:37 - 3:38
    Why?
  • 3:38 - 3:43
    The first and last blocks
    can’t be the same if there are no repeats.
  • 3:43 - 3:48
    So when can a given sequence
    become a palindrome?
  • 3:48 - 3:53
    One way to figure that out
    is to analyze a few existing palindromes.
  • 3:53 - 3:56
    In ANNA,
    there are 2 A’s and 2 N’s.
  • 3:56 - 4:01
    RACECAR
    has 2 R’s, 2 A’s, 2 C’s, and 1 E.
  • 4:01 - 4:08
    And MADAM IM ADAM
    has 4 M’s, 4 A’s, 2 D’s, and 1 I.
  • 4:08 - 4:11
    The pattern here
    is that most of the letters occur
  • 4:11 - 4:13
    an even number of times,
  • 4:13 - 4:16
    and there’s at most 1
    that occurs just once.
  • 4:16 - 4:17
    Is that it?
  • 4:17 - 4:20
    What if RACECAR had 3 E’s instead of 1?
  • 4:20 - 4:24
    We could tack the new E’s onto the ends
    and still get a palindrome,
  • 4:24 - 4:26
    so 3 is ok.
  • 4:26 - 4:32
    But make that 3 E’s and 3 C’s,
    and there’s nowhere for the last C to go.
  • 4:32 - 4:35
    So the most generalized insight is that
  • 4:35 - 4:39
    at most one letter can appear
    an odd number of times,
  • 4:39 - 4:42
    but the rest have to be even.
  • 4:42 - 4:46
    Hedge can count the letters in each stack
    and organize them into a dictionary,
  • 4:46 - 4:49
    which is a tidy way
    of storing information.
  • 4:49 - 4:53
    A loop could then go through and count
    how many times odd numbers appear.
  • 4:53 - 4:59
    If there are less than 2 odd characters,
    the stack can be made into a palindrome.
  • 4:59 - 5:03
    This approach is much, much faster
    than the naïve solution.
  • 5:03 - 5:06
    Instead of factorial time,
    it takes linear time.
  • 5:06 - 5:08
    That’s where the time increases
  • 5:08 - 5:10
    in proportion to
    the number of blocks there are.
  • 5:10 - 5:14
    Now write a loop for Hedge
    to approach the piles individually,
  • 5:14 - 5:19
    and stop when he finds a good one,
    and you’ll be ready to go.
  • 5:19 - 5:20
    Here’s what happens:
  • 5:20 - 5:24
    Hedge is fast, but there are so many piles
    it takes a long time.
  • 5:24 - 5:25
    Too long.
  • 6:18 - 6:20
    Ethic and Hedge are safe.
  • 6:20 - 6:22
    But Octavia is not so lucky.
Title:
The Chasm | Think Like A Coder, Ep 6
Speaker:
Alex Rosenthal
Description:

View full lesson: https://ed.ted.com/lessons/the-chasm-think-like-a-coder-ep-6

This is episode 6 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:24

English subtitles

Revisions Compare revisions