Skip to content

Commit a87d724

Browse files
johnngugiStargator
authored andcommitted
bracket-push: Port bracket-push to the dart track
1 parent fb04f36 commit a87d724

File tree

7 files changed

+181
-0
lines changed

7 files changed

+181
-0
lines changed

config.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,17 @@
205205
"control_flow_loops",
206206
"strings"
207207
]
208+
},
209+
{
210+
"slug": "bracket-push",
211+
"uuid": "508ebbf1-205e-4992-97d7-2865f06a3b2b",
212+
"core": false,
213+
"unlocked_by": null,
214+
"difficulty": 3,
215+
"topics": [
216+
"pattern_matching",
217+
"stacks"
218+
]
208219
}
209220
]
210221
}

exercises/bracket-push/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Bracket Push
2+
3+
Given a string containing brackets `[]`, braces `{}`, parentheses `()`,
4+
or any combination thereof, verify that any and all pairs are matched
5+
and nested correctly.
6+
7+
8+
To run the tests:
9+
10+
```sh
11+
$ pub run test
12+
```
13+
14+
For more detailed info about the Dart track see the [installation](http://exercism.io/languages/dart/installation) and [testing](http://exercism.io/languages/dart/tests) pages.
15+
16+
## Source
17+
18+
Ginna Baker
19+
20+
## Submitting Incomplete Solutions
21+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
analyzer:
2+
strong-mode:
3+
implicit-casts: false
4+
implicit-dynamic: false
5+
errors:
6+
unused_element: error
7+
unused_import: error
8+
unused_local_variable: error
9+
dead_code: error
10+
11+
linter:
12+
rules:
13+
# Error Rules
14+
- avoid_relative_lib_imports
15+
- avoid_types_as_parameter_names
16+
- literal_only_boolean_expressions
17+
- no_adjacent_strings_in_list
18+
- valid_regexps
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class BracketPush {
2+
// Put your code here
3+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import 'dart:collection';
2+
3+
class BracketPush {
4+
bool isPaired(String input) {
5+
ListQueue<String> stack = new ListQueue(input.length);
6+
7+
for (String char in input.split('')) {
8+
if (char == '(' || char == '{' || char == '[') {
9+
stack.add(char);
10+
} else if (char == ')' || char == '}' || char == ']') {
11+
if (stack.isEmpty || !_areMatching(stack.last, char)) {
12+
return false;
13+
} else {
14+
stack.removeLast();
15+
}
16+
}
17+
}
18+
19+
return stack.isEmpty;
20+
}
21+
22+
bool _areMatching(String opening, String closing) {
23+
if (opening == '(' && closing == ')')
24+
return true;
25+
else if (opening == '{' && closing == '}')
26+
return true;
27+
else if (opening == '[' && closing == ']') return true;
28+
return false;
29+
}
30+
}

exercises/bracket-push/pubspec.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: 'bracket_push'
2+
environment:
3+
sdk: ">=1.24.0 <3.0.0"
4+
dev_dependencies:
5+
test: '<2.0.0'
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import 'package:test/test.dart';
2+
import 'package:bracket_push/bracket_push.dart';
3+
4+
void main() {
5+
final BracketPush bracketPush = new BracketPush();
6+
group('BracketPush', () {
7+
test("paired square brackets", () {
8+
final bool result = bracketPush.isPaired("[]");
9+
expect(result, equals(true));
10+
}, skip: false);
11+
12+
test("empty string", () {
13+
final bool result = bracketPush.isPaired("");
14+
expect(result, equals(true));
15+
}, skip: true);
16+
17+
test("unpaired brackets", () {
18+
final bool result = bracketPush.isPaired("[[");
19+
expect(result, equals(false));
20+
}, skip: true);
21+
22+
test("wrong ordered brackets", () {
23+
final bool result = bracketPush.isPaired("}{");
24+
expect(result, equals(false));
25+
}, skip: true);
26+
27+
test("wrong closing bracket", () {
28+
final bool result = bracketPush.isPaired("{]");
29+
expect(result, equals(false));
30+
}, skip: true);
31+
32+
test("paired with whitespace", () {
33+
final bool result = bracketPush.isPaired("{ }");
34+
expect(result, equals(true));
35+
}, skip: true);
36+
37+
test("partially paired brackets", () {
38+
final bool result = bracketPush.isPaired("{[])");
39+
expect(result, equals(false));
40+
}, skip: true);
41+
42+
test("simple nested brackets", () {
43+
final bool result = bracketPush.isPaired("{[]}");
44+
expect(result, equals(true));
45+
}, skip: true);
46+
47+
test("several paired brackets", () {
48+
final bool result = bracketPush.isPaired("{}[]");
49+
expect(result, equals(true));
50+
}, skip: true);
51+
52+
test("paired and nested brackets", () {
53+
final bool result = bracketPush.isPaired("([{}({}[])])");
54+
expect(result, equals(true));
55+
}, skip: true);
56+
57+
test("unopened closing brackets", () {
58+
final bool result = bracketPush.isPaired("{[)][]}");
59+
expect(result, equals(false));
60+
}, skip: true);
61+
62+
test("unpaired and nested brackets", () {
63+
final bool result = bracketPush.isPaired("([{])");
64+
expect(result, equals(false));
65+
}, skip: true);
66+
67+
test("paired and wrong nested brackets", () {
68+
final bool result = bracketPush.isPaired("[({]})");
69+
expect(result, equals(false));
70+
}, skip: true);
71+
72+
test("paired and incomplete brackets", () {
73+
final bool result = bracketPush.isPaired("{}[");
74+
expect(result, equals(false));
75+
}, skip: true);
76+
77+
test("too many closing brackets", () {
78+
final bool result = bracketPush.isPaired("[]]");
79+
expect(result, equals(false));
80+
}, skip: true);
81+
82+
test("math expression", () {
83+
final bool result = bracketPush.isPaired("(((185 + 223.85) * 15) - 543)/2");
84+
expect(result, equals(true));
85+
}, skip: true);
86+
87+
test("complex latex expression", () {
88+
final bool result = bracketPush
89+
.isPaired("\\left(\\begin{array}{cc} \\frac{1}{3} & x\\\\ \\mathrm{e}^{x} &... x^2 \\end{array}\\right)");
90+
expect(result, equals(true));
91+
}, skip: true);
92+
});
93+
}

0 commit comments

Comments
 (0)