-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
matching-brackets
exercise (#331)
* Add matching brackets * Add `matching-brackets` exercise * Simnplify
- Loading branch information
1 parent
3f5399b
commit 6a1282e
Showing
7 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"authors": [ | ||
"erikschierboom" | ||
], | ||
"files": { | ||
"solution": [ | ||
"matching_brackets.pl" | ||
], | ||
"test": [ | ||
"matching_brackets_tests.plt" | ||
], | ||
"example": [ | ||
".meta/matching_brackets.example.pl" | ||
] | ||
}, | ||
"blurb": "Make sure the brackets and braces all match.", | ||
"source": "Ginna Baker" | ||
} |
17 changes: 17 additions & 0 deletions
17
exercises/practice/matching-brackets/.meta/matching_brackets.example.pl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
:- use_module(library(dcg/basics)). | ||
:- set_prolog_flag(double_quotes, chars). | ||
|
||
parentheses --> "(", balanced, ")". | ||
brackets --> "[", balanced, "]". | ||
braces --> "{", balanced, "}". | ||
|
||
balanced --> (parentheses | brackets | braces), balanced. | ||
balanced --> non_bracket, balanced. | ||
balanced --> []. | ||
|
||
non_bracket --> [C], { \+ member(C, "()[]{}") }. | ||
|
||
paired(String) :- | ||
string_chars(String, Chars), | ||
phrase(balanced, Chars), | ||
!. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
paired(String). |
69 changes: 69 additions & 0 deletions
69
exercises/practice/matching-brackets/matching_brackets_tests.plt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
pending :- | ||
current_prolog_flag(argv, ['--all'|_]). | ||
pending :- | ||
write('\nA TEST IS PENDING!\n'), | ||
fail. | ||
|
||
:- begin_tests(matching_brackets). | ||
|
||
test(paired_square_brackets, condition(true)) :- | ||
paired("[]"). | ||
|
||
test(empty_string, condition(pending)) :- | ||
paired(""). | ||
|
||
test(unpaired_brackets, [fail, condition(pending)]) :- | ||
paired("[["). | ||
|
||
test(wrong_ordered_brackets, [fail, condition(pending)]) :- | ||
paired("}{"). | ||
|
||
test(wrong_closing_bracket, [fail, condition(pending)]) :- | ||
paired("{]"). | ||
|
||
test(paired_with_whitespace, condition(pending)) :- | ||
paired("{ }"). | ||
|
||
test(partially_paired_brackets, [fail, condition(pending)]) :- | ||
paired("{[])"). | ||
|
||
test(simple_nested_brackets, condition(pending)) :- | ||
paired("{[]}"). | ||
|
||
test(several_paired_brackets, condition(pending)) :- | ||
paired("{}[]"). | ||
|
||
test(paired_and_nested_brackets, condition(pending)) :- | ||
paired("([{}({}[])])"). | ||
|
||
test(unopened_closing_brackets, [fail, condition(pending)]) :- | ||
paired("{[)][]}"). | ||
|
||
test(unpaired_and_nested_brackets, [fail, condition(pending)]) :- | ||
paired("([{])"). | ||
|
||
test(paired_and_wrong_nested_brackets, [fail, condition(pending)]) :- | ||
paired("[({]})"). | ||
|
||
test(paired_and_wrong_nested_brackets_but_innermost_are_correct, [fail, condition(pending)]) :- | ||
paired("[({}])"). | ||
|
||
test(paired_and_incomplete_brackets, [fail, condition(pending)]) :- | ||
paired("{}["). | ||
|
||
test(too_many_closing_brackets, [fail, condition(pending)]) :- | ||
paired("[]]"). | ||
|
||
test(early_unexpected_brackets, [fail, condition(pending)]) :- | ||
paired(")()"). | ||
|
||
test(early_mismatched_brackets, [fail, condition(pending)]) :- | ||
paired("{)()"). | ||
|
||
test(math_expression, condition(pending)) :- | ||
paired("(((185 + 223.85) * 15) - 543)/2"). | ||
|
||
test(complex_latex_expression, condition(pending)) :- | ||
paired("\\left(\\begin{array}{cc} \\frac{1}{3} & x\\\\ \\mathrm{e}^{x} &... x^2 \\end{array}\\right)"). | ||
|
||
:- end_tests(matching_brackets). |