-
Notifications
You must be signed in to change notification settings - Fork 0
/
permutations.rs
55 lines (49 loc) · 1.22 KB
/
permutations.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#![allow(dead_code)]
pub fn permute(nums: Vec<i32>) -> Vec<Vec<i32>> {
if nums.len() == 1 {
return vec![nums];
}
let mut result = vec![];
for i in 0..nums.len() {
let mut nums_clone = nums.clone();
let num = nums_clone.remove(i);
for mut perm in permute(nums_clone) {
perm.insert(0, num);
result.push(perm);
}
}
result
}
#[cfg(test)]
mod tests {
use super::*;
fn validate_permutations(mut result: Vec<Vec<i32>>, expected: Vec<Vec<i32>>) -> bool {
result.sort();
result == expected
}
#[test]
fn test_permutations() {
assert!(validate_permutations(
permute(vec![1, 2, 3]),
vec![
vec![1, 2, 3],
vec![1, 3, 2],
vec![2, 1, 3],
vec![2, 3, 1],
vec![3, 1, 2],
vec![3, 2, 1]
]
));
}
#[test]
fn test_permutations_2() {
assert!(validate_permutations(
permute(vec![0, 1]),
vec![vec![0, 1], vec![1, 0]]
));
}
#[test]
fn test_permutations_3() {
assert!(validate_permutations(permute(vec![1]), vec![vec![1]]));
}
}