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