Skip to content
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
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"grade-school",
"tournament",
"robot-simulator",
"bracket-push",
"queen-attack",
"sublist",
"space-age",
Expand Down
4 changes: 4 additions & 0 deletions exercises/bracket-push/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions exercises/bracket-push/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "bracket-push"
version = "0.0.0"
6 changes: 6 additions & 0 deletions exercises/bracket-push/HINTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Bracket Push in Rust

Reading about these Rust topics may help you implement a solution.

- Lifetimes and Structs: https://doc.rust-lang.org/book/lifetimes.html#impl-blocks
- From trait: https://doc.rust-lang.org/std/convert/trait.From.html
79 changes: 79 additions & 0 deletions exercises/bracket-push/example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use std::collections::HashMap;

pub struct Brackets {
raw_brackets: Vec<char>,
pairs: MatchingBrackets,
}

impl<'a> From<&'a str> for Brackets {
fn from(i: &str) -> Self {
Brackets::new(String::from(i), None)
}
}

impl Brackets {
pub fn new(s: String, pairs: Option<Vec<(char, char)>>) -> Self {
let p = match pairs {
Some(x) => MatchingBrackets::from(x),
None => MatchingBrackets::from(vec![('[', ']'), ('{', '}'), ('(', ')')]),
};

Brackets {
raw_brackets: s.chars().filter(|c| p.contains(&c)).collect::<Vec<char>>(),
pairs: p,
}
}

pub fn are_balanced(&self) -> bool {
let mut unclosed: Vec<char> = Vec::new();

for &bracket in self.raw_brackets.iter() {
if let Some(last_unclosed) = unclosed.pop() {
unclosed.extend(self.pairs.unmatched(last_unclosed, bracket));
} else {
unclosed.push(bracket);
}
}

unclosed.is_empty()
}
}

pub struct MatchingBrackets {
collection: HashMap<char, char>,
}

impl From<Vec<(char, char)>> for MatchingBrackets {
fn from(v: Vec<(char, char)>) -> Self {
MatchingBrackets { collection: v.into_iter().collect::<HashMap<char, char>>() }
}
}

impl MatchingBrackets {
fn contains(&self, other: &char) -> bool {
let known = self.collection.keys().chain(self.collection.values()).collect::<Vec<_>>();
known.contains(&other)
}

fn closer_for(&self, k: &char) -> Option<&char> {
self.collection.get(k)
}

fn closed_by(&self, l: char, r: char) -> bool {
match self.closer_for(&l) {
Some(&x) => r == x,
None => false,
}
}

fn unmatched(&self, open: char, potential_close: char) -> Vec<char> {
let mut ret: Vec<char> = Vec::new();

if !self.closed_by(open, potential_close) {
ret.push(open);
ret.push(potential_close);
}

ret
}
}
82 changes: 82 additions & 0 deletions exercises/bracket-push/tests/bracket-push.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
extern crate bracket_push;

use bracket_push::*;

#[test]
fn paired_square_brackets() {
assert!(Brackets::from("[]").are_balanced());
}

#[test]
#[ignore]
fn empty_string() {
assert!(Brackets::from("").are_balanced());
}

#[test]
#[ignore]
fn unpaired_brackets() {
assert!(!Brackets::from("[[").are_balanced());
}

#[test]
#[ignore]
fn wrong_ordered_brackets() {
assert!(!Brackets::from("}{").are_balanced());
}

#[test]
#[ignore]
fn paired_with_whitespace() {
assert!(Brackets::from("{ }").are_balanced());
}

#[test]
#[ignore]
fn simple_nested_brackets() {
assert!(Brackets::from("{[]}").are_balanced());
}

#[test]
#[ignore]
fn several_paired_brackets() {
assert!(Brackets::from("{}[]").are_balanced());
}

#[test]
#[ignore]
fn paired_and_nested_brackets() {
assert!(Brackets::from("([{}({}[])])").are_balanced());
}

#[test]
#[ignore]
fn unopened_closing_brackets() {
assert!(!Brackets::from("{[)][]}").are_balanced());
}

#[test]
#[ignore]
fn unpaired_and_nested_brackets() {
assert!(!Brackets::from("([{])").are_balanced());
}

#[test]
#[ignore]
fn paired_and_wrong_nested_brackets() {
assert!(!Brackets::from("[({]})").are_balanced());
}

#[test]
#[ignore]
fn math_expression() {
assert!(Brackets::from("(((185 + 223.85) * 15) - 543)/2").are_balanced());
}

#[test]
#[ignore]
fn complex_latex_expression() {
let input = "\\left(\\begin{array}{cc} \\frac{1}{3} & x\\\\ \\mathrm{e}^{x} &... x^2 \
\\end{array}\\right)";
assert!(Brackets::from(input).are_balanced());
}
1 change: 1 addition & 0 deletions problems.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ hexadecimal | Option, zip/fold/chars, map
grade-school | struct, entry api, Vec, Option
tournament | enum, sorting, hashmap, structs
robot-simulator | Immutability, enum
bracket-push | From trait, stack or recursion
queen-attack | struct, trait (optional), Result
sublist | enum, generic over type
space-age | Custom Trait, From Trait, Default Trait implementation
Expand Down