Avatar of silvia

silvia's solution

to Matching Brackets in the Scala Track

Published at Oct 16 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.

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command sbt test in the exercise directory.

Please see the learning and installation pages if you need any help.

Source

Ginna Baker

Submitting Incomplete Solutions

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

MatchingBracketsTest.scala

import org.scalatest.{Matchers, FunSuite}

/** @version 1.3.0 */
class MatchingBracketsTest extends FunSuite with Matchers {

  test("paired square brackets") {
    MatchingBrackets.isPaired("[]") should be(true)
  }

  test("empty string") {
    pending
    MatchingBrackets.isPaired("") should be(true)
  }

  test("unpaired brackets") {
    pending
    MatchingBrackets.isPaired("[[") should be(false)
  }

  test("wrong ordered brackets") {
    pending
    MatchingBrackets.isPaired("}{") should be(false)
  }

  test("wrong closing bracket") {
    pending
    MatchingBrackets.isPaired("{]") should be(false)
  }

  test("paired with whitespace") {
    pending
    MatchingBrackets.isPaired("{ }") should be(true)
  }

  test("partially paired brackets") {
    pending
    MatchingBrackets.isPaired("{[])") should be(false)
  }

  test("simple nested brackets") {
    pending
    MatchingBrackets.isPaired("{[]}") should be(true)
  }

  test("several paired brackets") {
    pending
    MatchingBrackets.isPaired("{}[]") should be(true)
  }

  test("paired and nested brackets") {
    pending
    MatchingBrackets.isPaired("([{}({}[])])") should be(true)
  }

  test("unopened closing brackets") {
    pending
    MatchingBrackets.isPaired("{[)][]}") should be(false)
  }

  test("unpaired and nested brackets") {
    pending
    MatchingBrackets.isPaired("([{])") should be(false)
  }

  test("paired and wrong nested brackets") {
    pending
    MatchingBrackets.isPaired("[({]})") should be(false)
  }

  test("math expression") {
    pending
    MatchingBrackets.isPaired("(((185 + 223.85) * 15) - 543)/2") should be(true)
  }

  test("complex latex expression") {
    pending
    MatchingBrackets.isPaired(
      """\left(\begin{array}{cc} \frac{1}{3} & x\ \mathrm{e}^{x} &... x^2 \end{array}\right)""") should be(
      true)
  }
}
object MatchingBrackets {
  def isPaired(expression: String) : Boolean = {
    return bracketSearch(Nil, expression.toList)
  }

  def bracketSearch(open_brackets: List[Char], str: List[Char]): Boolean = {
    str match {
      case '}'::rest => closeBracket('{', open_brackets, rest)
      case ']'::rest => closeBracket('[', open_brackets, rest)
      case ')'::rest => closeBracket('(', open_brackets, rest)
      case '('::rest => bracketSearch('('::open_brackets, rest)
      case '['::rest => bracketSearch('['::open_brackets, rest)
      case '{'::rest => bracketSearch('{'::open_brackets, rest)
      case _::rest => bracketSearch(open_brackets, rest)
      case Nil => open_brackets.isEmpty
    }
  }

  def closeBracket(bracket:Char, open_brackets: List[Char], rest: List[Char]): Boolean = {
    if (!open_brackets.isEmpty && open_brackets.head == bracket) {
      bracketSearch(open_brackets.tail, rest)
    }
    else {
      false
    }
  }
}

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?