aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--matching-brackets/.exercism/metadata.json1
-rw-r--r--matching-brackets/.gitignore8
-rw-r--r--matching-brackets/Cargo.toml4
-rw-r--r--matching-brackets/README.md85
-rw-r--r--matching-brackets/src/lib.rs12
-rw-r--r--matching-brackets/tests/matching-brackets.rs98
6 files changed, 208 insertions, 0 deletions
diff --git a/matching-brackets/.exercism/metadata.json b/matching-brackets/.exercism/metadata.json
new file mode 100644
index 0000000..7b5495d
--- /dev/null
+++ b/matching-brackets/.exercism/metadata.json
@@ -0,0 +1 @@
{"track":"rust","exercise":"matching-brackets","id":"568307886f5d4f38afb0394b1e981c76","url":"https://exercism.io/my/solutions/568307886f5d4f38afb0394b1e981c76","handle":"dyamon","is_requester":true,"auto_approve":false} \ No newline at end of file
diff --git a/matching-brackets/.gitignore b/matching-brackets/.gitignore
new file mode 100644
index 0000000..db7f315
--- /dev/null
+++ b/matching-brackets/.gitignore
@@ -0,0 +1,8 @@
1# Generated by Cargo
2# will have compiled files and executables
3/target/
4**/*.rs.bk
5
6# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
7# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
8Cargo.lock
diff --git a/matching-brackets/Cargo.toml b/matching-brackets/Cargo.toml
new file mode 100644
index 0000000..2e7dafa
--- /dev/null
+++ b/matching-brackets/Cargo.toml
@@ -0,0 +1,4 @@
1[package]
2edition = "2018"
3name = "matching-brackets"
4version = "2.0.0"
diff --git a/matching-brackets/README.md b/matching-brackets/README.md
new file mode 100644
index 0000000..049e3fa
--- /dev/null
+++ b/matching-brackets/README.md
@@ -0,0 +1,85 @@
1# Matching Brackets
2
3Given a string containing brackets `[]`, braces `{}`, parentheses `()`,
4or any combination thereof, verify that any and all pairs are matched
5and nested correctly.
6
7## Rust Installation
8
9Refer to the [exercism help page][help-page] for Rust installation and learning
10resources.
11
12## Writing the Code
13
14Execute the tests with:
15
16```bash
17$ cargo test
18```
19
20All but the first test have been ignored. After you get the first test to
21pass, open the tests source file which is located in the `tests` directory
22and remove the `#[ignore]` flag from the next test and get the tests to pass
23again. Each separate test is a function with `#[test]` flag above it.
24Continue, until you pass every test.
25
26If you wish to run all ignored tests without editing the tests source file, use:
27
28```bash
29$ cargo test -- --ignored
30```
31
32To run a specific test, for example `some_test`, you can use:
33
34```bash
35$ cargo test some_test
36```
37
38If the specific test is ignored use:
39
40```bash
41$ cargo test some_test -- --ignored
42```
43
44To learn more about Rust tests refer to the [online test documentation][rust-tests]
45
46Make sure to read the [Modules][modules] chapter if you
47haven't already, it will help you with organizing your files.
48
49## Further improvements
50
51After you have solved the exercise, please consider using the additional utilities, described in the [installation guide](https://exercism.io/tracks/rust/installation), to further refine your final solution.
52
53To format your solution, inside the solution directory use
54
55```bash
56cargo fmt
57```
58
59To see, if your solution contains some common ineffective use cases, inside the solution directory use
60
61```bash
62cargo clippy --all-targets
63```
64
65## Submitting the solution
66
67Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer.
68
69## Feedback, Issues, Pull Requests
70
71The [exercism/rust](https://github.com/exercism/rust) repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!
72
73If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md).
74
75[help-page]: https://exercism.io/tracks/rust/learning
76[modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html
77[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html
78[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
79
80## Source
81
82Ginna Baker
83
84## Submitting Incomplete Solutions
85It's possible to submit an incomplete solution so you can see how others have completed the exercise.
diff --git a/matching-brackets/src/lib.rs b/matching-brackets/src/lib.rs
new file mode 100644
index 0000000..03e39ca
--- /dev/null
+++ b/matching-brackets/src/lib.rs
@@ -0,0 +1,12 @@
1pub fn brackets_are_balanced(string: &str) -> bool {
2 let mut stack = vec![];
3 for c in string.chars() {
4 match c {
5 '(' => stack.push(((c as u8) + 1) as char),
6 '[' | '{' => stack.push(((c as u8) + 2) as char),
7 ')' | ']' | '}' if stack.pop() != Some(c) => return false,
8 _ => ()
9 }
10 }
11 stack.is_empty()
12}
diff --git a/matching-brackets/tests/matching-brackets.rs b/matching-brackets/tests/matching-brackets.rs
new file mode 100644
index 0000000..0826dad
--- /dev/null
+++ b/matching-brackets/tests/matching-brackets.rs
@@ -0,0 +1,98 @@
1use matching_brackets::brackets_are_balanced;
2
3#[test]
4fn paired_square_brackets() {
5 assert!(brackets_are_balanced("[]"));
6}
7
8#[test]
9fn empty_string() {
10 assert!(brackets_are_balanced(""));
11}
12
13#[test]
14fn unpaired_brackets() {
15 assert!(!brackets_are_balanced("[["));
16}
17
18#[test]
19fn wrong_ordered_brackets() {
20 assert!(!brackets_are_balanced("}{"));
21}
22
23#[test]
24fn wrong_closing_bracket() {
25 assert!(!brackets_are_balanced("{]"));
26}
27
28#[test]
29fn paired_with_whitespace() {
30 assert!(brackets_are_balanced("{ }"));
31}
32
33#[test]
34fn partially_paired_brackets() {
35 assert!(!brackets_are_balanced("{[])"));
36}
37
38#[test]
39fn simple_nested_brackets() {
40 assert!(brackets_are_balanced("{[]}"));
41}
42
43#[test]
44fn several_paired_brackets() {
45 assert!(brackets_are_balanced("{}[]"));
46}
47
48#[test]
49fn paired_and_nested_brackets() {
50 assert!(brackets_are_balanced("([{}({}[])])"));
51}
52
53#[test]
54fn unopened_closing_brackets() {
55 assert!(!brackets_are_balanced("{[)][]}"));
56}
57
58#[test]
59fn unpaired_and_nested_brackets() {
60 assert!(!brackets_are_balanced("([{])"));
61}
62
63#[test]
64fn paired_and_wrong_nested_brackets() {
65 assert!(!brackets_are_balanced("[({]})"));
66}
67
68#[test]
69fn paired_and_incomplete_brackets() {
70 assert!(!brackets_are_balanced("{}["));
71}
72
73#[test]
74fn too_many_closing_brackets() {
75 assert!(!brackets_are_balanced("[]]"));
76}
77
78#[test]
79fn early_incomplete_brackets() {
80 assert!(!brackets_are_balanced(")()"));
81}
82
83#[test]
84fn early_mismatched_brackets() {
85 assert!(!brackets_are_balanced("{)()"));
86}
87
88#[test]
89fn math_expression() {
90 assert!(brackets_are_balanced("(((185 + 223.85) * 15) - 543)/2"));
91}
92
93#[test]
94fn complex_latex_expression() {
95 let input = "\\left(\\begin{array}{cc} \\frac{1}{3} & x\\\\ \\mathrm{e}^{x} &... x^2 \
96 \\end{array}\\right)";
97 assert!(brackets_are_balanced(input));
98}