Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of Samyual

Samyual's solution

to Bracket Push in the Erlang Track

Published at Feb 23 2021 · 0 comments
Test suite

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


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.


Ginna Baker

Submitting Incomplete Solutions

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


%% 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.



'1_paired_square_brackets_test'() ->

'2_empty_string_test'() ->

'3_unpaired_brackets_test'() ->

'4_wrong_ordered_brackets_test'() ->

'5_wrong_closing_bracket_test'() ->

'6_paired_with_whitespace_test'() ->
    ?assert(bracket_push:is_paired("{ }")).

'7_partially_paired_brackets_test'() ->

'8_simple_nested_brackets_test'() ->

'9_several_paired_brackets_test'() ->

'10_paired_and_nested_brackets_test'() ->

'11_unopened_closing_brackets_test'() ->

'12_unpaired_and_nested_brackets_test'() ->

'13_paired_and_wrong_nested_brackets_test'() ->

'14_paired_and_incomplete_brackets_test'() ->

'15_too_many_closing_brackets_test'() ->

'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}\\"


-define(Brackets, [$[, $], ${, $}, $(, $)]).
-define(Paired, #{$[ => $], ${ => $}, $( => $)}).

is_paired(String) -> is_paired(String, []).

%% Edge case: brackets are balanced
is_paired([], []) -> true;

% Edge case: bracket are not balanced
is_paired([], _) -> false;

%% opening bracket: push closing bracket in stack and scan others
is_paired([Bracket | Others], Stack) when Bracket == ${ orelse Bracket == $[ orelse Bracket == $( ->
  is_paired(Others, [map_get(Bracket, ?Paired) | Stack]);

%% closing bracket is matched with stack's head: scan tails of input and stack
is_paired([Bracket | Others], [Bracket | Stack]) when Bracket == $} orelse Bracket == $] orelse Bracket == $) ->
  is_paired(Others, Stack);

%% opening bracket and empty stack: brackets are not balanced
is_paired([Bracket | _], _) when Bracket == $} orelse Bracket == $] orelse Bracket == $) -> false;

%% character is not bracket: scan others
is_paired([_ | Others], Stack) -> is_paired(Others, Stack).

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?