Last active
May 4, 2020 08:31
-
-
Save cameronp98/931e05c7c4f8e700baa3ebacbe63138b to your computer and use it in GitHub Desktop.
not permutations but combinations, cba to change
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn perms<T: Clone>(values: &[T], perm_len: usize) -> Vec<Vec<T>> { | |
let mut out = Vec::new(); | |
perms_inner(&mut out, &values, Vec::with_capacity(perm_len), perm_len); | |
return out; | |
} | |
fn perms_inner<T: Clone>(out: &mut Vec<Vec<T>>, values: &[T], perm: Vec<T>, perm_len: usize) { | |
if perm.len() == perm_len { | |
out.push(perm); | |
return; | |
} | |
for v in values { | |
let mut new_perm = perm.clone(); | |
new_perm.push(v.clone()); | |
perms_inner(out, values, new_perm, perm_len); | |
} | |
} | |
fn main() { | |
for p in perms(&['a', 'b', 'c', 'd'], 2) { | |
println!("{:?}", p); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment