Skip to content

Commit

Permalink
First working example. Not a good example, but the tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
whit0694 committed Aug 24, 2016
1 parent babb763 commit b44363b
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions exercises/bracket-push/example.rs
Original file line number Diff line number Diff line change
@@ -1 +1,49 @@
// The code below is a stub. Just enough to satisfy the compiler.
// In order to pass the tests you can add-to or change any of this code.

pub struct Brackets {
brackets: Vec<char>,
}

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

impl Brackets {
pub fn new(s: String) -> Self {
Brackets {
brackets: s.chars().filter(|c| Brackets::brackets().contains(c)).collect::<Vec<_>>(),
}
}

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

for b in self.brackets.clone() {
if matches.is_empty() {
matches.push(b);
} else {
let t = (matches.pop().unwrap(), b);
let m = match t {
('[', ']') => true,
('{', '}') => true,
('(', ')') => true,
_ => false,
};

if !m {
matches.push(t.0);
matches.push(t.1);
}
}
}

matches.is_empty()
}

fn brackets() -> [char; 6] {
['{', '[', '(', ')', ']', '}']
}
}

0 comments on commit b44363b

Please sign in to comment.