Episode 50
The Last Bracket at the Gate
Duck's package is sitting behind a toy gate. It can't be opened with the same number of brackets on both sides if the brackets cross. The gate keeps track of each opening bracket until it is erased by a matching closing one.
















Episode transcript
The gate counts the wrong thing
Duck in the monitor. Toy gate. Input ribbon. Round brackets. Square brackets. Robot out. Looking at the gate.
Diagram values: TOY GATE: ONLY ( ) [ ]; CHECK THE WHOLE RIBBON
Duck: My parcel is ready. Now, why won't this gate open?
Duck points at opening-round. opening-square. closing-round. closing-square. Count card has one of each. Gate is still closed.
Diagram values: INPUT: ([)]; ONE EACH: ( [ ) ]
Duck: I counted 1 of them each. That's gotta be enough.
Robot shows the monitor with some kind of vertical stack: opening-round at the bottom and opening-square at the top. Ribbon cursor read only 2 first symbols.
Diagram values: READ: ([; STACK BOTTOM: (; STACK TOP: [; NEXT: )
Robot: Openers are placed on top of the stack. Close the newest opener first.
Duck has the next closing tile from the closing round next to the top of the stack (opening square). The opening round underneath is still visible. We don't see the result.
Diagram values: NEXT: ); TOP: [; PREDICT MATCH OR MISMATCH
Duck: Will it fit the top?
The last opener comes first
Robot stops duck on the 3rd symbol (closing-round). Opening-square sits atop opening-round. No corresponding pop. The rest of the ribbon is unopened.
Diagram values: POSITION 3: ); TOP [ NEEDS ]; MISMATCH: REJECT
Robot: End this ribbon. Don't pop.
Ribbon. You have as many opening brackets as closing brackets.
Diagram values: ([)] HAS SAME COUNTS; COUNTS DO NOT PROVE NESTING
Duck: Totals were correct. Order is not.
Duck swaps bad ribbon with (open round, open square, close square, close round). If it's a new empty stack, it'll start a new check. Then, it pushes the first two openers in order.
Diagram values: NEW INPUT: ([]); READ: ([; STACK: ( [; TOP: [
Duck: Try this one...
Robot compares square of close (ie. closing square) to top square of open (ie. opening square) and only remove square opener. Leave round opener on stack.
Diagram values: READ: ([]; MATCH [ WITH ]; POP [; STACK: (
Robot: Just remove that opener.
Two other ways to fail
Duck matches up to the final close round with the last round opener. Stack is empty after all 4 symbols are read. Toy gate opens.
Diagram values: READ: ([]); MATCH ( WITH ); STACK EMPTY + END; ACCEPT
Duck: Empty stack. Ribbon end.
Robot sees new ribbon. Has a closing square bracket. Stack holder is empty. Refuse this closing square bracket. Don't pop anything.
Diagram values: NEW INPUT: ]; STACK EMPTY; UNMATCHED CLOSER: REJECT
Robot: No opener, so just the closer. Don't take this ribbon.
Duck checks 1st rd/1st sq/last sq. Square pair has closed but round opener is still on the stack at the end of ribbon.
Diagram values: NEW INPUT: ([]; END: STACK (; LEFTOVER OPENER: REJECT
Duck: That opener's still around.
Robot points at stack top, end of ribbon marker. Duck discards rejected examples and picks up new parcel label.
Diagram values: LAST IN, FIRST OUT; EMPTY TOO EARLY != FINISHED; CHECK THE WHOLE INPUT
Robot: Close the newest opener first.
A fresh ribbon
Read first two symbols. Square opener at the bottom of the stack. Round opener on top. Wait to compare next round closer.
Diagram values: NEW INPUT: [()]; READ: [(; BOTTOM: [; TOP: (; NEXT: )
Duck: What opener should this round closer go with?
Robot finds that third symbol = round bracket close matches the top round bracket open. Pops that round bracket open only. Square bracket open is still underneath.
Diagram values: READ: [(); MATCH ( WITH ); POP (; STACK: [
Robot: Delete it. Keep square.
Duck matches last close square and open square left. The stack is empty at the end. Parcel passes the toy gate.
Diagram values: READ: [()]; MATCH [ WITH ]; END: STACK EMPTY; ACCEPT
Duck: Square last.
The parcel's beyond open toy gate. Robot and Duck are happy next to the empty stack holder. This toy only tests round and square bracket ribbons.
Diagram values: TOY INPUT: ONLY ( ) [ ]; BRACKET CHECK != ALL SYNTAX
Robot: It didn't learn a full programming language.
The lesson
The toy works for round, square brackets. It reads from left to right. Each opener goes on top of the stack. The closer needs to have the same shape of the latest unpaired opener on top. If so - pop the opener. The closer with empty stack or wrong shape on top of it will reject the entire ribbon.
If you have in ([)]. The 3rd symbol is round closer. But the opener on top is square. So reject. Don't pop. The ribbon ([]) has the same number of brackets and nesting is fine. Accept only if you've read all ribbon without failures and now the stack is empty. Or if you have a remainder opener - reject too.
It's just this kind of bracket ribbons the toy supports. It's not supporting any entire programming language (with quotes, comments, etc.)
Try it
Diagram values: INPUT: ([])(; 5 SYMBOLS
So, with a fresh new stack, read the 5 symbol ribbon ([])(. Read each of the 5 symbols. What's left in the stack? Do you accept the entire ribbon with your toy gate?
Answer
Diagram values: AFTER (: stack bottom-to-top (; AFTER ([: stack bottom-to-top ( [; AFTER ([]: stack (; AFTER ([]): empty, but input not ended; AFTER ([])(: stack (; END; REJECT
First opener is a round opener, then push a square on top. Closing-square takes the square opener off. Closing-round takes the first round opener off. Empty stack after four symbols. But the ribbon hasn't ended yet. So now the fifth symbol is another opening-round. You push it. And the ribbon ends. But now the stack still contains that round opener. So reject it.
Sources
Runestone: Balanced Symbols, A General Case (opens in a new tab)