Skip to content

Instantly share code, notes, and snippets.

@cameronp98
Last active May 4, 2020 08:31
Show Gist options
  • Save cameronp98/931e05c7c4f8e700baa3ebacbe63138b to your computer and use it in GitHub Desktop.
Save cameronp98/931e05c7c4f8e700baa3ebacbe63138b to your computer and use it in GitHub Desktop.
not permutations but combinations, cba to change
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