Avatar of artemkorsakov

artemkorsakov's solution

to Matching Brackets in the C# Track

Published at May 16 2019 · 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 the tests

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit MatchingBrackets.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

Source

Ginna Baker

MatchingBracketsTest.cs

// This file was auto-generated based on version 1.5.0 of the canonical data.

using Xunit;

public class MatchingBracketsTest
{
    [Fact]
    public void Paired_square_brackets()
    {
        var value = "[]";
        Assert.True(MatchingBrackets.IsPaired(value));
    }

    [Fact(Skip = "Remove to run test")]
    public void Empty_string()
    {
        var value = "";
        Assert.True(MatchingBrackets.IsPaired(value));
    }

    [Fact(Skip = "Remove to run test")]
    public void Unpaired_brackets()
    {
        var value = "[[";
        Assert.False(MatchingBrackets.IsPaired(value));
    }

    [Fact(Skip = "Remove to run test")]
    public void Wrong_ordered_brackets()
    {
        var value = "}{";
        Assert.False(MatchingBrackets.IsPaired(value));
    }

    [Fact(Skip = "Remove to run test")]
    public void Wrong_closing_bracket()
    {
        var value = "{]";
        Assert.False(MatchingBrackets.IsPaired(value));
    }

    [Fact(Skip = "Remove to run test")]
    public void Paired_with_whitespace()
    {
        var value = "{ }";
        Assert.True(MatchingBrackets.IsPaired(value));
    }

    [Fact(Skip = "Remove to run test")]
    public void Partially_paired_brackets()
    {
        var value = "{[])";
        Assert.False(MatchingBrackets.IsPaired(value));
    }

    [Fact(Skip = "Remove to run test")]
    public void Simple_nested_brackets()
    {
        var value = "{[]}";
        Assert.True(MatchingBrackets.IsPaired(value));
    }

    [Fact(Skip = "Remove to run test")]
    public void Several_paired_brackets()
    {
        var value = "{}[]";
        Assert.True(MatchingBrackets.IsPaired(value));
    }

    [Fact(Skip = "Remove to run test")]
    public void Paired_and_nested_brackets()
    {
        var value = "([{}({}[])])";
        Assert.True(MatchingBrackets.IsPaired(value));
    }

    [Fact(Skip = "Remove to run test")]
    public void Unopened_closing_brackets()
    {
        var value = "{[)][]}";
        Assert.False(MatchingBrackets.IsPaired(value));
    }

    [Fact(Skip = "Remove to run test")]
    public void Unpaired_and_nested_brackets()
    {
        var value = "([{])";
        Assert.False(MatchingBrackets.IsPaired(value));
    }

    [Fact(Skip = "Remove to run test")]
    public void Paired_and_wrong_nested_brackets()
    {
        var value = "[({]})";
        Assert.False(MatchingBrackets.IsPaired(value));
    }

    [Fact(Skip = "Remove to run test")]
    public void Paired_and_incomplete_brackets()
    {
        var value = "{}[";
        Assert.False(MatchingBrackets.IsPaired(value));
    }

    [Fact(Skip = "Remove to run test")]
    public void Too_many_closing_brackets()
    {
        var value = "[]]";
        Assert.False(MatchingBrackets.IsPaired(value));
    }

    [Fact(Skip = "Remove to run test")]
    public void Math_expression()
    {
        var value = "(((185 + 223.85) * 15) - 543)/2";
        Assert.True(MatchingBrackets.IsPaired(value));
    }

    [Fact(Skip = "Remove to run test")]
    public void Complex_latex_expression()
    {
        var value = "\\left(\\begin{array}{cc} \\frac{1}{3} & x\\\\ \\mathrm{e}^{x} &... x^2 \\end{array}\\right)";
        Assert.True(MatchingBrackets.IsPaired(value));
    }
}
using System.Text.RegularExpressions;

public static class MatchingBrackets
{
    private static readonly Regex regexBrackets = new Regex("\\[\\]");
    private static readonly Regex regexBraces = new Regex("\\{\\}");
    private static readonly Regex regexParentheses = new Regex("\\(\\)");
    private static readonly Regex regexClear = new Regex("[^" + regexBrackets + regexBraces + regexParentheses + "]*");

    /// <summary>
    /// Given a string containing brackets [], braces {}, parentheses (),
    /// or any combination thereof,
    /// verify that any and all pairs are matched and nested correctly.
    /// </summary>
    public static bool IsPaired(string input)
    {
        input = regexClear.Replace(input, "");
        var replaces = GetReplaced(input);

        while (replaces.Length < input.Length)
        {
            input = replaces;
            replaces = GetReplaced(replaces);
        }

        return string.IsNullOrEmpty(replaces);
    }

    private static string GetReplaced(string input)
    {
        var result = input;
        result = regexBrackets.Replace(result, "");
        result = regexBraces.Replace(result, "");
        result = regexParentheses.Replace(result, "");
        return result;
    }
}

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?