 # HegelPro's solution

## to Matching Brackets in the Elm Track

Published at Jul 05 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

# Bracket Push

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

## Elm Installation

Refer to the Installing Elm page for information about installing elm.

## Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

$elm-test  Automatically run tests again when you save changes: $ elm-test --watch


As you work your way through the test suite, be sure to remove the skip <| calls from each test until you get them all passing!

Ginna Baker

## Submitting Incomplete Solutions

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

### Tests.elm

module Tests exposing (tests)

import BracketPush exposing (isPaired)
import Expect
import String
import Test exposing (..)

tests : Test
tests =
describe "BracketPush"
[ test "paired square brackets" <|
\_ ->
Expect.equal True (isPaired "[]")
, skip <|
test "empty string" <|
\_ ->
Expect.equal True (isPaired "")
, skip <|
test "unpaired brackets" <|
\_ ->
Expect.equal False (isPaired "[[")
, skip <|
test "wrong ordered brackets" <|
\_ ->
Expect.equal False (isPaired "}{")
, skip <|
test "wrong closing bracket" <|
\_ ->
Expect.equal False (isPaired "{]")
, skip <|
test "paired with whitespace" <|
\_ ->
Expect.equal True (isPaired "{ }")
, skip <|
test "partially paired brackets" <|
\_ ->
Expect.equal False (isPaired "{[])")
, skip <|
test "simple nested brackets" <|
\_ ->
Expect.equal True (isPaired "{[]}")
, skip <|
test "seceral paired brackets" <|
\_ ->
Expect.equal True (isPaired "{}[]")
, skip <|
test "paired and nested brackets" <|
\_ ->
Expect.equal True (isPaired "([{}({}[])])")
, skip <|
test "unopened closing brackets" <|
\_ ->
Expect.equal False (isPaired "{[)][]}")
, skip <|
test "unpaired and nested brackets" <|
\_ ->
Expect.equal False (isPaired "([{])")
, skip <|
test "paired and wrong nested brackets" <|
\_ ->
Expect.equal False (isPaired "[({]})")
, skip <|
test "math expression" <|
\_ ->
Expect.equal True (isPaired "(((185 + 223.85) * 15) - 543)/2")
, skip <|
test "complex latex expression" <|
\_ ->
Expect.equal True (isPaired "\\left(\\begin{array}{cc} \\frac{1}{3} & x\\\\ \\mathrm{e}^{x} &... x^2 \\end{array}\\right)")
]
module BracketPush exposing (isPaired)

import Dict exposing (..)

openBracketToCloseBracket : String -> String
openBracketToCloseBracket openChar =
case openChar of
"[" -> "]"
"{" -> "}"
"(" -> ")"
_ -> ""

isBracket : String -> Bool
isBracket char
= isOpenBracket char
|| isCloseBracket char

isOpenBracket : String -> Bool
isOpenBracket char
= char == "["
|| char == "{"
|| char == "("

isCloseBracket : String -> Bool
isCloseBracket char
= char == "]"
|| char == "}"
|| char == ")"

isPaired : String -> Bool
isPaired input =
let
char = String.left 1 input
in
if String.length input == 0 then
True
else
if isBracket char then
if isOpenBracket char then
case findLastCloseBracketIndex char input of
Just closeBracketIndex ->
isPaired (String.dropLeft 1 (String.left (closeBracketIndex + 1) input))
&& isPaired (String.dropLeft (closeBracketIndex + 2) input)
Nothing -> False
else
False
else
isPaired (String.dropLeft 1 input)

findFirstIndex : String -> String -> Maybe Int
findFirstIndex char str =
str
|> String.indexes char

findSecondIndex : String -> String -> Maybe Int
findSecondIndex char str =
str
|> String.indexes char
|> List.drop 1

findLastIndex : String -> String -> Maybe Int
findLastIndex char str =
str
|> String.indexes char
|> List.reverse

findLastCloseBracketIndex : String -> String -> Maybe Int
findLastCloseBracketIndex char str =
String.foldl (\n res -> funcFo char n res) { index = 0, z = 0, result = Nothing, isDone = False } (str)
|> \n -> n.result

funcFo : String -> Char -> { index : Int, z : Int, result : Maybe Int, isDone : Bool } -> { index : Int, z : Int, result : Maybe Int, isDone : Bool }
funcFo findChar char res =
if res.isDone then
res
else
if String.fromChar (char) == findChar then
{ res | index = res.index + 1, z = res.z + 1 }
else if openBracketToCloseBracket findChar == String.fromChar char then
if res.index == 1 then
{res | isDone = True, result = Just (res.z - 1) }
else
{ res | index = res.index - 1, z = res.z + 1 }
else
{ res | z = res.z + 1 }