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
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, [$[, $], ${, $}, $(, $)]).
-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?