Avatar of thekeele

thekeele's solution

to Matching Brackets in the Elixir Track

Published at Nov 02 2019 · 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.

Running tests

Execute the tests with:

$ mix test

Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant @tag :pending with a # symbol.

For example:

# @tag :pending
test "shouting" do
  assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end

Or, you can enable all the tests by commenting out the ExUnit.configure line in the test suite.

# ExUnit.configure exclude: :pending, trace: true

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

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_test.exs

defmodule MatchingBracketsTest do
  use ExUnit.Case

  # @tag :pending
  test "paired square brackets" do
    assert MatchingBrackets.check_brackets("[]")
  end

  @tag :pending
  test "empty string" do
    assert MatchingBrackets.check_brackets("")
  end

  @tag :pending
  test "unpaired brackets" do
    refute MatchingBrackets.check_brackets("[[")
  end

  @tag :pending
  test "wrong ordered brackets" do
    refute MatchingBrackets.check_brackets("}{")
  end

  @tag :pending
  test "wrong closing bracket" do
    refute MatchingBrackets.check_brackets("{]")
  end

  @tag :pending
  test "paired with whitespace" do
    assert MatchingBrackets.check_brackets("{ }")
  end

  @tag :pending
  test "simple nested brackets" do
    assert MatchingBrackets.check_brackets("{[]}")
  end

  @tag :pending
  test "several paired brackets" do
    assert MatchingBrackets.check_brackets("{}[]")
  end

  @tag :pending
  test "paired and nested brackets" do
    assert MatchingBrackets.check_brackets("([{}({}[])])")
  end

  @tag :pending
  test "unopened closing brackets" do
    refute MatchingBrackets.check_brackets("{[)][]}")
  end

  @tag :pending
  test "unpaired and nested brackets" do
    refute MatchingBrackets.check_brackets("([{])")
  end

  @tag :pending
  test "paired and wrong nested brackets" do
    refute MatchingBrackets.check_brackets("[({]})")
  end

  @tag :pending
  test "math expression" do
    assert MatchingBrackets.check_brackets("(((185 + 223.85) * 15) - 543)/2")
  end

  @tag :pending
  test "complex latex expression" do
    assert MatchingBrackets.check_brackets(
             "\\left(\\begin{array}{cc} \\frac{1}{3} & x\\\\ \\mathrm{e}^{x} &... x^2 \\end{array}\\right)"
           )
  end
end

test_helper.exs

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)
defmodule MatchingBrackets do
  @matches %{"]" => "[", "}" => "{", ")" => "("}

  @spec check_brackets(String.t()) :: boolean
  def check_brackets(str) do
    str
    |> String.codepoints()
    |> Enum.reduce([], &match/2)
    |> Enum.empty?()
  end

  defp match(bracket, brackets) when bracket in ["[", "{", "("] do
    brackets ++ [bracket]
  end

  defp match(bracket, brackets) when bracket in ["]", "}", ")"] do
    if @matches[bracket] in brackets do
      List.delete_at(brackets, -1)
    else
      brackets ++ [bracket]
    end
  end

  defp match(_, brackets) do
    brackets
  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?