Bitpath

Lesson 2 · Unit 01

Matching brackets

Push openers, pop on closers, end empty

12 exercisesabout 3 min

What you practise

  • The stack algorithm
  • Leftovers and popping from empty

Try a question from this lesson

Pick one

Which of these has correctly matched brackets?

The idea

Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.

Concept

The most recent opener is the top of a stack

"The most recent opener still waiting" is exactly what the top of a stack gives you.

Read the text left to right. Push every opener. On a closer, pop, and check that the popped opener is the partner of that closer. A lookup table of partners keeps the check to one line.

pairs = {")": "(", "]": "[", "}": "{"}

print(pairs["]"])   # its opener: [
const pairs = {
  ")": "(",
  "]": "[",
  "}": "{",
};

console.log(pairs["]"]); // its opener: [
const pairs: Record<string, string> = {
  ")": "(",
  "]": "[",
  "}": "{",
};

console.log(pairs["]"]); // its opener: [

TypeScript noteRecord<string, string> is an object type whose keys and values are strings. It lets you index pairs with any string, such as a character read from the input.

10 more exercises in the app

  • 4× type the answer
  • 1× fill the blanks
  • 3× multiple choice
  • 1× true or false
  • 1× order the lines

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy