[48in24 Exercise] [06-18] Matching Brackets

This issue is a discussion for contributors to collaborate in getting ready to be featured in 48in24. Please refer to this forum topic for more info.


We will be featuring Matching Brackets from Jun 18 onwards.

Staff jobs

These are things for Erik/Jeremy to do:

  • ☐ Check/update exercise in Problem Specifications
  • ☐ Create + schedule video

Community jobs

For each track:

  • Implement Matching Brackets
  • Add approaches (and an approaches introduction!) for each idiomatic or interesting/educational approach.
  • Add video walkthroughs (record yourself solving and digging deeper into the exercise).
  • Highlight up to 16 different featured exercises (coming soon)

Existing Approaches

You can use these as the basis for approaches on your own tracks. Feel free to copy/paste/reuse/rewrite/etc as you see fit! Maybe ask ChatGPT to translate to your programming language.

Track Statuses

You can see an overview of which tracks have implemented the exercise at the #48in24 implementation status page.

I’ve currently gathered the following solutions to feature:

  1. Kotlin: keep on string replacing until no changes were made
    Keep on string replacing open/close pairs until no changes were made

  2. Python: keep on string replacing until no changes were made using regex
    Keep on string replacing open/close pairs using a regex until no changes were made

  3. Ruby: recursive regex
    Use a recursive regular expression

  4. C++: stack and peek
    Use a stack, and peek to check if last opened value matches

  5. Pharo: stack and initial symbol to never have empty stack
    Use a stack, start out with symbol to prevent empty stack checks

  6. Rust: stack and pattern matching
    Use a stack, combined with pattern matching

  7. Nim: push closing char on stack
    Instead of pushing the open char on the stack, push the closing char

  8. Prolog: declare grammar for balanced strings and validate
    Declare a grammar for balanced strings and validate

If anyone has more suggestions, do let us know!

1 Like

A raku solution which uses a grammar ( a declarative programming approach with a collection of regexes ) :

grammar Brackets {

    token TOP { <brackets> * }

    token brackets {
        | <parentheses>
        | <braces>
        | <squares>
        | <un-bracket>
    }

    token parentheses { '(' ~ ')' <brackets> * }
    token braces      { '{' ~ '}' <brackets> * }
    token squares     { '[' ~ ']' <brackets> * }
    token un-bracket  { <-[ ( ) { } \[ \] ]> + }

}

The tilde ~ syntax is built-in support for nested structures which neatly simplifies the code.

3 Likes

We also had just filmed this one. Really cool solution though

PR for Web Assembly:

1 Like

Sync tests for Clojure

1 Like
2 Likes

Was this film published anywhere? It’s a fantastic exercise and I would love to hear your commentary for the Pharo solution :slight_smile:

I’ll add for Pyret and Red next.