ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# LeaveNhA's solution

## to Bracket Push in the PureScript Track

Published at Mar 14 2021 · 0 comments
Instructions
Test suite
Solution

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

Ginna Baker

## Submitting Incomplete Solutions

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

### Main.purs

module Test.Main where

import Prelude

import Effect (Effect)
import Test.Unit.Assert as Assert
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Main (runTest)
import BracketPush (isPaired)

main :: Effect Unit
main = runTest suites

suites :: TestSuite
suites = do
suite "BracketPush.isPaired" do

test "paired square brackets" $Assert.equal true (isPaired "[]") test "empty string"$
Assert.equal true
(isPaired "")

test "unpaired brackets" $Assert.equal false (isPaired "[[") test "wrong ordered brackets"$
Assert.equal false
(isPaired "}{")

test "paired with whitespace" $Assert.equal true (isPaired "{ }") test "simple nested brackets"$
Assert.equal true
(isPaired "{[]}")

test "several paired brackets" $Assert.equal true (isPaired "{}[]") test "paired and nested brackets"$
Assert.equal true
(isPaired "([{}({}[])])")

test "unopened closing brackets" $Assert.equal false (isPaired "{[)][]}") test "unpaired and nested brackets"$
Assert.equal false
(isPaired "([{])")

test "paired and wrong nested brackets" $Assert.equal false (isPaired "[({]})") test "math expression"$
Assert.equal true
(isPaired "(((185 + 223.85) * 15) - 543)/2")

test "complex latex expression" $Assert.equal true (isPaired "\\left(\\begin{array}{cc} \\frac{1}{3} & x\\\\ \\mathrm{e}^{x} &... x^2 \\end{array}\\right)") module BracketPush where import Data.Array (foldl, snoc, null, last, tail, singleton, fromFoldable, take, length, init) import Data.Maybe (Maybe(..), fromJust) import Data.String.CodeUnits (toCharArray) import Data.Tuple (Tuple(..)) import Prelude (($), (==), (||), (/), (+), (-))

bracketToInt :: Char -> Maybe Int
bracketToInt '(' = Just 1
bracketToInt '[' = Just 2
bracketToInt '{' = Just 3
bracketToInt ')' = Just 4
bracketToInt ']' = Just 5
bracketToInt '}' = Just 6
bracketToInt _ = Nothing

stackBrackets :: Maybe (Array Int) -> Char -> Maybe (Array Int)
stackBrackets Nothing _ = Nothing
stackBrackets (Just brackets) c
| c == '[' || c == '(' || c == '{' = case (bracketToInt c) of
Just bracketInt -> Just $snoc brackets bracketInt Nothing -> Nothing stackBrackets (Just brackets) c = case { l: last brackets, bi: (bracketToInt c) } of {l: _, bi: Nothing} -> Just brackets {l: Just lastBracket, bi: Just bracketInteger} -> if (lastBracket + 3) == bracketInteger then init brackets else Nothing _ -> Nothing isPaired :: String -> Boolean isPaired pairs = case foldl stackBrackets (Just ([] :: Array Int))$ toCharArray pairs of
Just brackets -> null brackets
Nothing -> false