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

ikarachristos's solution

to Bracket Push in the Erlang Track

Published at Jan 31 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}\\"


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

is_paired([], []) -> true;
is_paired([], _) -> false;
is_paired([$[ | T], B) -> is_paired(T, [$[ | B]);
is_paired([${ | T], B) -> is_paired(T, [${ | B]);
is_paired([$( | T], B) -> is_paired(T, [$( | B]);
is_paired([$] | T], [$[ | B]) -> is_paired(T, B);
is_paired([$} | T], [${ | B]) -> is_paired(T, B);
is_paired([$) | T], [$( | B]) -> is_paired(T, B);
is_paired([$] | _], _) -> false;
is_paired([$} | _], _) -> false;
is_paired([$) | _], _) -> false;
is_paired([_|T], B) -> is_paired(T, B).

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?