From 34e04eff6a222247bb84f65230484c7bd0f6cadb Mon Sep 17 00:00:00 2001 From: Danylo Negrienko Date: Sat, 6 Jul 2024 21:30:26 -0400 Subject: [PATCH] matching_brackets --- .../matching-brackets/.exercism/config.json | 30 +++++++ .../matching-brackets/.exercism/metadata.json | 1 + elixir/matching-brackets/.formatter.exs | 4 + elixir/matching-brackets/.gitignore | 24 ++++++ elixir/matching-brackets/HELP.md | 75 ++++++++++++++++ elixir/matching-brackets/README.md | 32 +++++++ .../lib/matching_brackets.ex | 28 ++++++ elixir/matching-brackets/mix.exs | 28 ++++++ .../test/matching_brackets_test.exs | 85 +++++++++++++++++++ elixir/matching-brackets/test/test_helper.exs | 2 + 10 files changed, 309 insertions(+) create mode 100644 elixir/matching-brackets/.exercism/config.json create mode 100644 elixir/matching-brackets/.exercism/metadata.json create mode 100644 elixir/matching-brackets/.formatter.exs create mode 100644 elixir/matching-brackets/.gitignore create mode 100644 elixir/matching-brackets/HELP.md create mode 100644 elixir/matching-brackets/README.md create mode 100644 elixir/matching-brackets/lib/matching_brackets.ex create mode 100644 elixir/matching-brackets/mix.exs create mode 100644 elixir/matching-brackets/test/matching_brackets_test.exs create mode 100644 elixir/matching-brackets/test/test_helper.exs diff --git a/elixir/matching-brackets/.exercism/config.json b/elixir/matching-brackets/.exercism/config.json new file mode 100644 index 0000000..e90bcb6 --- /dev/null +++ b/elixir/matching-brackets/.exercism/config.json @@ -0,0 +1,30 @@ +{ + "authors": [ + "jimmbraddock" + ], + "contributors": [ + "angelikatyborska", + "Cohen-Carlisle", + "devonestes", + "lex57ukr", + "lpil", + "neenjaw", + "parkerl", + "sotojuan", + "waiting-for-dev", + "workingjubilee" + ], + "files": { + "solution": [ + "lib/matching_brackets.ex" + ], + "test": [ + "test/matching_brackets_test.exs" + ], + "example": [ + ".meta/example.ex" + ] + }, + "blurb": "Make sure the brackets and braces all match.", + "source": "Ginna Baker" +} diff --git a/elixir/matching-brackets/.exercism/metadata.json b/elixir/matching-brackets/.exercism/metadata.json new file mode 100644 index 0000000..d326834 --- /dev/null +++ b/elixir/matching-brackets/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"elixir","exercise":"matching-brackets","id":"7934a9822b254a919f4aa9454f9c9455","url":"https://exercism.org/tracks/elixir/exercises/matching-brackets","handle":"negrienko","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/elixir/matching-brackets/.formatter.exs b/elixir/matching-brackets/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/elixir/matching-brackets/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/elixir/matching-brackets/.gitignore b/elixir/matching-brackets/.gitignore new file mode 100644 index 0000000..671ea18 --- /dev/null +++ b/elixir/matching-brackets/.gitignore @@ -0,0 +1,24 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +bracket_push-*.tar + diff --git a/elixir/matching-brackets/HELP.md b/elixir/matching-brackets/HELP.md new file mode 100644 index 0000000..a4caeb6 --- /dev/null +++ b/elixir/matching-brackets/HELP.md @@ -0,0 +1,75 @@ +# Help + +## Running the tests + +From the terminal, change to the base directory of the exercise then execute the tests with: + +```bash +$ mix test +``` + +This will execute the test file found in the `test` subfolder -- a file ending in `_test.exs` + +Documentation: + +* [`mix test` - Elixir's test execution tool](https://hexdocs.pm/mix/Mix.Tasks.Test.html) +* [`ExUnit` - Elixir's unit test library](https://hexdocs.pm/ex_unit/ExUnit.html) + +## Pending tests + +In test suites of practice exercises, all but the first test have been tagged to be skipped. + +Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol. + +For example: + +```elixir +# @tag :pending +test "shouting" do + assert Bob.hey("WATCH OUT!") == "Whoa, chill out!" +end +``` + +If you wish to run all tests at once, you can include all skipped test by using the `--include` flag on the `mix test` command: + +```bash +$ mix test --include pending +``` + +Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the file `test/test_helper.exs`. + +```elixir +# ExUnit.configure(exclude: :pending, trace: true) +``` + +## Useful `mix test` options + +* `test/.exs:LINENUM` - runs only a single test, the test from `.exs` whose definition is on line `LINENUM` +* `--failed` - runs only tests that failed the last time they ran +* `--max-failures` - the suite stops evaluating tests when this number of test failures +is reached +* `--seed 0` - disables randomization so the tests in a single file will always be ran +in the same order they were defined in + +## Submitting your solution + +You can submit your solution using the `exercism submit lib/matching_brackets.ex` command. +This command will upload your solution to the Exercism website and print the solution page's URL. + +It's possible to submit an incomplete solution which allows you to: + +- See how others have completed the exercise +- Request help from a mentor + +## Need to get help? + +If you'd like help solving the exercise, check the following pages: + +- The [Elixir track's documentation](https://exercism.org/docs/tracks/elixir) +- The [Elixir track's programming category on the forum](https://forum.exercism.org/c/programming/elixir) +- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +If you're stuck on something, it may help to look at some of the [available resources](https://exercism.org/docs/tracks/elixir/resources) out there where answers might be found. \ No newline at end of file diff --git a/elixir/matching-brackets/README.md b/elixir/matching-brackets/README.md new file mode 100644 index 0000000..00e1d92 --- /dev/null +++ b/elixir/matching-brackets/README.md @@ -0,0 +1,32 @@ +# Matching Brackets + +Welcome to Matching Brackets on Exercism's Elixir Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Given a string containing brackets `[]`, braces `{}`, parentheses `()`, or any combination thereof, verify that any and all pairs are matched and nested correctly. +The string may also contain other characters, which for the purposes of this exercise should be ignored. + +## Source + +### Created by + +- @jimmbraddock + +### Contributed to by + +- @angelikatyborska +- @Cohen-Carlisle +- @devonestes +- @lex57ukr +- @lpil +- @neenjaw +- @parkerl +- @sotojuan +- @waiting-for-dev +- @workingjubilee + +### Based on + +Ginna Baker \ No newline at end of file diff --git a/elixir/matching-brackets/lib/matching_brackets.ex b/elixir/matching-brackets/lib/matching_brackets.ex new file mode 100644 index 0000000..4e68b6d --- /dev/null +++ b/elixir/matching-brackets/lib/matching_brackets.ex @@ -0,0 +1,28 @@ +defmodule MatchingBrackets do + require Logger + + @opening_brackets ~w"[ { (" + @closing_brackets ~w"] } )" + @bracket_pairs Enum.zip(@closing_brackets, @opening_brackets) + + @doc """ + Checks that all the brackets and braces in the string are matched correctly, and nested correctly + """ + @spec check_brackets(String.t()) :: boolean + def check_brackets(str), do: do_check_brackets(str, []) + + defguardp is_opening_bracket(char) when char in @opening_brackets + defguardp is_closing_bracket(char) when char in @closing_brackets + defguardp is_pair(closing, opening) when {closing, opening} in @bracket_pairs + + # Base cases + defp do_check_brackets("", []), do: true + defp do_check_brackets("", _any), do: false + # Opening bracket + defp do_check_brackets(<>, acc) when is_opening_bracket(opening), do: do_check_brackets(str, [opening | acc]) + # Closing bracket + defp do_check_brackets(<>, [opening | tail]) when is_pair(closing, opening), do: do_check_brackets(str, tail) + defp do_check_brackets(<>, _acc) when is_closing_bracket(closing), do: false + # Not a bracket + defp do_check_brackets(<<_char::binary-size(1), str::binary>>, acc), do: do_check_brackets(str, acc) +end diff --git a/elixir/matching-brackets/mix.exs b/elixir/matching-brackets/mix.exs new file mode 100644 index 0000000..b9538a3 --- /dev/null +++ b/elixir/matching-brackets/mix.exs @@ -0,0 +1,28 @@ +defmodule MatchingBrackets.MixProject do + use Mix.Project + + def project do + [ + app: :matching_brackets, + version: "0.1.0", + # elixir: "~> 1.8", + start_permanent: Mix.env() == :prod, + deps: deps() + ] + end + + # Run "mix help compile.app" to learn about applications. + def application do + [ + extra_applications: [:logger] + ] + end + + # Run "mix help deps" to learn about dependencies. + defp deps do + [ + # {:dep_from_hexpm, "~> 0.3.0"}, + # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} + ] + end +end diff --git a/elixir/matching-brackets/test/matching_brackets_test.exs b/elixir/matching-brackets/test/matching_brackets_test.exs new file mode 100644 index 0000000..62419e5 --- /dev/null +++ b/elixir/matching-brackets/test/matching_brackets_test.exs @@ -0,0 +1,85 @@ +defmodule MatchingBracketsTest do + use ExUnit.Case + + test "paired square brackets" do + assert MatchingBrackets.check_brackets("[]") + end + + test "empty string" do + assert MatchingBrackets.check_brackets("") + end + + test "unpaired brackets" do + refute MatchingBrackets.check_brackets("[[") + end + + test "wrong ordered brackets" do + refute MatchingBrackets.check_brackets("}{") + end + + test "wrong closing bracket" do + refute MatchingBrackets.check_brackets("{]") + end + + test "paired with whitespace" do + assert MatchingBrackets.check_brackets("{ }") + end + + test "partially paired brackets" do + refute MatchingBrackets.check_brackets("{[])") + end + + test "simple nested brackets" do + assert MatchingBrackets.check_brackets("{[]}") + end + + test "several paired brackets" do + assert MatchingBrackets.check_brackets("{}[]") + end + + test "paired and nested brackets" do + assert MatchingBrackets.check_brackets("([{}({}[])])") + end + + test "unopened closing brackets" do + refute MatchingBrackets.check_brackets("{[)][]}") + end + + test "unpaired and nested brackets" do + refute MatchingBrackets.check_brackets("([{])") + end + + test "paired and wrong nested brackets" do + refute MatchingBrackets.check_brackets("[({]})") + end + + test "paired and wrong nested brackets but innermost are correct" do + refute MatchingBrackets.check_brackets("[({}])") + end + + test "paired and incomplete brackets" do + refute MatchingBrackets.check_brackets("{}[") + end + + test "too many closing brackets" do + refute MatchingBrackets.check_brackets("[]]") + end + + test "early unexpected brackets" do + refute MatchingBrackets.check_brackets(")()") + end + + test "early mismatched brackets" do + refute MatchingBrackets.check_brackets("{)()") + end + + test "math expression" do + assert MatchingBrackets.check_brackets("(((185 + 223.85) * 15) - 543)/2") + end + + test "complex latex expression" do + assert MatchingBrackets.check_brackets( + "\\left(\\begin{array}{cc} \\frac{1}{3} & x\\\\ \\mathrm{e}^{x} &... x^2 \\end{array}\\right)" + ) + end +end diff --git a/elixir/matching-brackets/test/test_helper.exs b/elixir/matching-brackets/test/test_helper.exs new file mode 100644 index 0000000..35fc5bf --- /dev/null +++ b/elixir/matching-brackets/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true)