Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
40
Task/Balanced-brackets/Rust/balanced-brackets.rust
Normal file
40
Task/Balanced-brackets/Rust/balanced-brackets.rust
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
extern crate rand;
|
||||
|
||||
trait Balanced {
|
||||
/// Returns true if the brackets are balanced
|
||||
fn is_balanced(&self) -> bool;
|
||||
}
|
||||
|
||||
impl<'a> Balanced for str {
|
||||
fn is_balanced(&self) -> bool {
|
||||
let mut count = 0;
|
||||
|
||||
for bracket in self.chars() {
|
||||
let change = match bracket {
|
||||
'[' => 1,
|
||||
']' => -1,
|
||||
_ => panic!("Strings should only contain brackets")
|
||||
};
|
||||
|
||||
count += change;
|
||||
if count < 0 { return false; }
|
||||
}
|
||||
|
||||
count == 0
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates random brackets
|
||||
fn generate_brackets(num: usize) -> String {
|
||||
use rand::random;
|
||||
|
||||
(0..num).map(|_| if random() { '[' } else { ']' }).collect()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
for i in (0..10) {
|
||||
let brackets = generate_brackets(i);
|
||||
|
||||
println!("{} {}", brackets, brackets.is_balanced())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue