matching_brackets

This commit is contained in:
Danil Negrienko 2024-07-06 21:30:26 -04:00
parent 22d545194e
commit 34e04eff6a
10 changed files with 309 additions and 0 deletions

View File

@ -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"
}

View File

@ -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}

View File

@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]

24
elixir/matching-brackets/.gitignore vendored Normal file
View File

@ -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

View File

@ -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/<FILE>.exs:LINENUM` - runs only a single test, the test from `<FILE>.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.

View File

@ -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

View File

@ -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(<<opening::binary-size(1), str::binary>>, acc) when is_opening_bracket(opening), do: do_check_brackets(str, [opening | acc])
# Closing bracket
defp do_check_brackets(<<closing::binary-size(1), str::binary>>, [opening | tail]) when is_pair(closing, opening), do: do_check_brackets(str, tail)
defp do_check_brackets(<<closing::binary-size(1), _str::binary>>, _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

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,2 @@
ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)