🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉

# akzj's solution

## to Bracket Push in the Erlang Track

Published at May 11 2021 · 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

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, rebar3 is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

$rebar3 eunit  ## Questions? For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises. ## Source Ginna Baker ## Submitting Incomplete Solutions It's possible to submit an incomplete solution so you can see how others have completed the exercise. ### bracket_push_tests.erl %% Based on canonical data version 1.5.0 %% https://github.com/exercism/problem-specifications/raw/master/exercises/bracket-push/canonical-data.json %% This file is automatically generated from the exercises canonical data. -module(bracket_push_tests). -include_lib("erl_exercism/include/exercism.hrl"). -include_lib("eunit/include/eunit.hrl"). '1_paired_square_brackets_test'() -> ?assert(bracket_push:is_paired("[]")). '2_empty_string_test'() -> ?assert(bracket_push:is_paired([])). '3_unpaired_brackets_test'() -> ?assertNot(bracket_push:is_paired("[[")). '4_wrong_ordered_brackets_test'() -> ?assertNot(bracket_push:is_paired("}{")). '5_wrong_closing_bracket_test'() -> ?assertNot(bracket_push:is_paired("{]")). '6_paired_with_whitespace_test'() -> ?assert(bracket_push:is_paired("{ }")). '7_partially_paired_brackets_test'() -> ?assertNot(bracket_push:is_paired("{[])")). '8_simple_nested_brackets_test'() -> ?assert(bracket_push:is_paired("{[]}")). '9_several_paired_brackets_test'() -> ?assert(bracket_push:is_paired("{}[]")). '10_paired_and_nested_brackets_test'() -> ?assert(bracket_push:is_paired("([{}({}[])])")). '11_unopened_closing_brackets_test'() -> ?assertNot(bracket_push:is_paired("{[)][]}")). '12_unpaired_and_nested_brackets_test'() -> ?assertNot(bracket_push:is_paired("([{])")). '13_paired_and_wrong_nested_brackets_test'() -> ?assertNot(bracket_push:is_paired("[({]})")). '14_paired_and_incomplete_brackets_test'() -> ?assertNot(bracket_push:is_paired("{}[")). '15_too_many_closing_brackets_test'() -> ?assertNot(bracket_push:is_paired("[]]")). '16_math_expression_test'() -> ?assert(bracket_push:is_paired("(((185 + 223.85) * 15) - 543)/2")). '17_complex_latex_expression_test'() -> ?assert(bracket_push:is_paired("\\left(\\begin{array}{cc} \\frac{1}{3} " "& x\\\\ \\mathrm{e}^{x} &... x^2 \\end{array}\\" "right)")). -module(bracket_push). -export([is_paired/1]). -define(BRACKETS ,"{[()]}"). is_paired(String) -> Brackets=[X||X<-String,lists:member(X,?BRACKETS)], is_paired(queue:new(),Brackets). is_paired(Q,[]) -> queue:is_empty(Q); is_paired(Q,[H|T]) when H ==$[ orelse H == ${ orelse H ==$( ->
is_paired(queue:in(H,Q),T);
is_paired(Q,[H|T]) ->
case queue:out_r(Q) of
{empty,_} -> false;
{{value, Item}, Q2} ->
case match(Item,H) of
true ->
is_paired(Q2,T);
_ -> false
end
end.

match(L,R) when
L ==$[ andalso R ==$]
orelse
L ==${ andalso R ==$}
orelse
L ==$( andalso R ==$)->
true;
match(_,_) ->false.