Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add matching-brackets #288

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "matching-brackets",
"name": "matching brackets",
"uuid": "c3fe6f2a-cb23-4f1e-8e80-67c36b387d15",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "robot-simulator",
"name": "Robot Simulator",
Expand Down
5 changes: 5 additions & 0 deletions exercises/practice/matching-brackets/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Instructions

Given a string containing brackets `[]`, braces `{}`, parentheses `()`, or any combination thereof, verify that any and all pairs are matched and nested correctly.
Any other characters should be ignored.
For example, `"{what is (42)}?"` is balanced and `"[text}"` is not.
8 changes: 8 additions & 0 deletions exercises/practice/matching-brackets/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Introduction

You're given the opportunity to write software for the Bracketeer™, an ancient but powerful mainframe.
The software that runs on it is written in a proprietary language.
Much of its syntax is familiar, but you notice _lots_ of brackets, braces and parentheses.
Despite the Bracketeer™ being powerful, it lacks flexibility.
If the source code has any unbalanced brackets, braces or parentheses, the Bracketeer™ crashes and must be rebooted.
To avoid such a scenario, you start writing code that can verify that brackets, braces, and parentheses are balanced before attempting to run it on the Bracketeer™.
18 changes: 18 additions & 0 deletions exercises/practice/matching-brackets/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"matching_brackets.bal"
],
"test": [
"tests/matching_brackets_test.bal"
],
"example": [
".meta/reference/matching_brackets.bal"
]
},
"blurb": "Make sure the brackets and braces all match.",
"source": "Ginna Baker"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
map<string> pairs = {
"[": "]",
"{": "}",
"(": ")"
};

string[] closers = pairs.keys().map(k => pairs.get(k));

#
# Check whether the brackets in a given string are balanced.
#
# + value - the string to check
# + return - true if the brackets are balanced, false otherwise
function isPaired(string value) returns boolean {
string:Char[] stack = [];

foreach string:Char chr in value {
if pairs.hasKey(chr) {
stack.push(chr);
}
else if closers.indexOf(chr) is int {
if stack.length() == 0 {
return false;
}

string:Char last = stack.pop();

match [last, chr] {
["[", _] if chr != "]" => {
return false;
}
["{", _] if chr != "}" => {
return false;
}
["(", _] if chr != ")" => {
return false;
}
_ => {
continue;
}
}
}
}

return stack.length() == 0;
}
70 changes: 70 additions & 0 deletions exercises/practice/matching-brackets/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[81ec11da-38dd-442a-bcf9-3de7754609a5]
description = "paired square brackets"

[287f0167-ac60-4b64-8452-a0aa8f4e5238]
description = "empty string"

[6c3615a3-df01-4130-a731-8ef5f5d78dac]
description = "unpaired brackets"

[9d414171-9b98-4cac-a4e5-941039a97a77]
description = "wrong ordered brackets"

[f0f97c94-a149-4736-bc61-f2c5148ffb85]
description = "wrong closing bracket"

[754468e0-4696-4582-a30e-534d47d69756]
description = "paired with whitespace"

[ba84f6ee-8164-434a-9c3e-b02c7f8e8545]
description = "partially paired brackets"

[3c86c897-5ff3-4a2b-ad9b-47ac3a30651d]
description = "simple nested brackets"

[2d137f2c-a19e-4993-9830-83967a2d4726]
description = "several paired brackets"

[2e1f7b56-c137-4c92-9781-958638885a44]
description = "paired and nested brackets"

[84f6233b-e0f7-4077-8966-8085d295c19b]
description = "unopened closing brackets"

[9b18c67d-7595-4982-b2c5-4cb949745d49]
description = "unpaired and nested brackets"

[a0205e34-c2ac-49e6-a88a-899508d7d68e]
description = "paired and wrong nested brackets"

[1d5c093f-fc84-41fb-8c2a-e052f9581602]
description = "paired and wrong nested brackets but innermost are correct"

[ef47c21b-bcfd-4998-844c-7ad5daad90a8]
description = "paired and incomplete brackets"

[a4675a40-a8be-4fc2-bc47-2a282ce6edbe]
description = "too many closing brackets"

[a345a753-d889-4b7e-99ae-34ac85910d1a]
description = "early unexpected brackets"

[21f81d61-1608-465a-b850-baa44c5def83]
description = "early mismatched brackets"

[99255f93-261b-4435-a352-02bdecc9bdf2]
description = "math expression"

[8e357d79-f302-469a-8515-2561877256a1]
description = "complex latex expression"
5 changes: 5 additions & 0 deletions exercises/practice/matching-brackets/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
org = "ballerina_exercism"
name = "matching_brackets"
version = "0.1.0"
distribution = "2201.5.0"
38 changes: 38 additions & 0 deletions exercises/practice/matching-brackets/Dependencies.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# AUTO-GENERATED FILE. DO NOT MODIFY.

# This file is auto-generated by Ballerina for managing dependency versions.
# It should not be modified by hand.

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.5.0"

[[package]]
org = "ballerina"
name = "jballerina.java"
version = "0.0.0"
scope = "testOnly"

[[package]]
org = "ballerina"
name = "test"
version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
modules = [
{org = "ballerina", packageName = "test", moduleName = "test"}
]

[[package]]
org = "ballerina_exercism"
name = "matching_brackets"
version = "0.1.0"
dependencies = [
{org = "ballerina", name = "test"}
]
modules = [
{org = "ballerina_exercism", packageName = "matching_brackets", moduleName = "matching_brackets"}
]

3 changes: 3 additions & 0 deletions exercises/practice/matching-brackets/matching_brackets.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function isPaired(string value) returns boolean {
//TODO: Implement this function
}
159 changes: 159 additions & 0 deletions exercises/practice/matching-brackets/tests/matching_brackets_test.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import ballerina/test;

@test:Config
function testPairedSquareBrackets() {
string value = "[]";
test:assertTrue(isPaired(value));
}

@test:Config {
enable: false
}
function testEmptyString() {
string value = "";
test:assertTrue(isPaired(value));
}

@test:Config {
enable: false
}
function testUnpairedBrackets() {
string value = "[[";
test:assertFalse(isPaired(value));
}

@test:Config {
enable: false
}
function testWrongOrderedBrackets() {
string value = "}{";
test:assertFalse(isPaired(value));
}

@test:Config {
enable: false
}
function testWrongClosingBracket() {
string value = "{]";
test:assertFalse(isPaired(value));
}

@test:Config {
enable: false
}
function testPairedWithWhitespace() {
string value = "{ }";
test:assertTrue(isPaired(value));
}

@test:Config {
enable: false
}
function testPartiallyPairedBrackets() {
string value = "{[])";
test:assertFalse(isPaired(value));
}

@test:Config {
enable: false
}
function testSimpleNestedBrackets() {
string value = "{[]}";
test:assertTrue(isPaired(value));
}

@test:Config {
enable: false
}
function testSeveralPairedBrackets() {
string value = "{}[]";
test:assertTrue(isPaired(value));
}

@test:Config {
enable: false
}
function testPairedAndNestedBrackets() {
string value = "([{}({}[])])";
test:assertTrue(isPaired(value));
}

@test:Config {
enable: false
}
function testUnopenedClosingBrackets() {
string value = "{[)][]}";
test:assertFalse(isPaired(value));
}

@test:Config {
enable: false
}
function testUnpairedAndNestedBrackets() {
string value = "([{])";
test:assertFalse(isPaired(value));
}

@test:Config {
enable: false
}
function testPairedAndWrongNestedBrackets() {
string value = "[({]})";
test:assertFalse(isPaired(value));
}

@test:Config {
enable: false
}
function testPairedAndWrongNestedBracketsButInnermostAreCorrect() {
string value = "[({}])";
test:assertFalse(isPaired(value));
}

@test:Config {
enable: false
}
function testPairedAndIncompleteBrackets() {
string value = "{}[";
test:assertFalse(isPaired(value));
}

@test:Config {
enable: false
}
function testTooManyClosingBrackets() {
string value = "[]]";
test:assertFalse(isPaired(value));
}

@test:Config {
enable: false
}
function testEarlyUnexpectedBrackets() {
string value = ")()";
test:assertFalse(isPaired(value));
}

@test:Config {
enable: false
}
function testEarlyMismatchedBrackets() {
string value = "{)()";
test:assertFalse(isPaired(value));
}

@test:Config {
enable: false
}
function testMathExpression() {
string value = "(((185 + 223.85) * 15) - 543)/2";
test:assertTrue(isPaired(value));
}

@test:Config {
enable: false
}
function testComplexLatexExpression() {
string value = "\\left(\\begin{array}{cc} \\frac{1}{3} & x\\\\ \\mathrm{e}^{x} &... x^2 \\end{array}\\right)";
test:assertTrue(isPaired(value));
}