Avatar of shmibs

shmibs's solution

to Matching Brackets in the Crystal Track

Published at Dec 29 2018 · 0 comments
Instructions
Test suite
Solution

Given a string containing brackets [], braces {}, parentheses (), or any combination thereof, verify that any and all pairs are matched and nested correctly.

Setup

Follow the setup instructions for Crystal here:

http://exercism.io/languages/crystal

More help installing can be found here:

http://crystal-lang.org/docs/installation/index.html

Making the Test Suite Pass

Execute the tests with:

$ crystal spec

In each test suite all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by changing pending to it.

Source

Ginna Baker

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

matching_brackets_spec.cr

require "spec"
require "../src/*"

describe "MatchingBrackets" do
  it "paired square brackets" do
    MatchingBrackets.valid?("[]").should eq(true)
  end

  it "empty string" do
    MatchingBrackets.valid?("").should eq(true)
  end

  it "unpaired brackets" do
    MatchingBrackets.valid?("[[").should eq(false)
  end

  it "wrong ordered brackets" do
    MatchingBrackets.valid?("}{").should eq(false)
  end

  it "wrong closing bracket" do
    MatchingBrackets.valid?("{]").should eq(false)
  end

  it "paired with whitespace" do
    MatchingBrackets.valid?("{ }").should eq(true)
  end

  it "partially paired brackets" do
    MatchingBrackets.valid?("{[])").should eq(false)
  end

  it "simple nested brackets" do
    MatchingBrackets.valid?("{[]}").should eq(true)
  end

  it "several paired brackets" do
    MatchingBrackets.valid?("{}[]").should eq(true)
  end

  it "paired and nested brackets" do
    MatchingBrackets.valid?("([{}({}[])])").should eq(true)
  end

  it "unopened closing brackets" do
    MatchingBrackets.valid?("{[)][]}").should eq(false)
  end

  it "unpaired and nested brackets" do
    MatchingBrackets.valid?("([{])").should eq(false)
  end

  it "paired and wrong nested brackets" do
    MatchingBrackets.valid?("[({]})").should eq(false)
  end

  it "paired and incomplete brackets" do
    MatchingBrackets.valid?("{}[").should eq(false)
  end

  it "too many closing brackets" do
    MatchingBrackets.valid?("[]]").should eq(false)
  end

  it "math expression" do
    MatchingBrackets.valid?("(((185 + 223.85) * 15) - 543)/2").should eq(true)
  end

  it "complex latex expression" do
    MatchingBrackets.valid?("\left(\begin{array}{cc} \frac{1}{3} & x\\ \mathrm{e}^{x} &... x^2 \end{array}\right)").should eq(true)
  end
end
module Brackets

  PAIRS = {
    ']' => '[',
    ')' => '(',
    '}' => '{'
  }

  def self.are_valid?(s : String) : Bool
    a = [] of Char

    s.each_char { |c|
      pc = PAIRS[c]?

      if PAIRS.values.includes?(c)
        a.push(c)
      elsif pc
        if a.last? == nil || a.last != pc
          return false
        end
        a.pop
      end
    }

    a.empty?
  end

end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?