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

ikarachristos's solution

to Bracket Push in the Erlang Track

Published at Jan 31 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]).


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?