Skip to content

Instantly share code, notes, and snippets.

@Splinter1984
Forked from Gadiguibou/lib.rs
Last active February 7, 2022 06:50
Show Gist options
  • Save Splinter1984/927a71594a16ef17d1019767fc1be328 to your computer and use it in GitHub Desktop.
Save Splinter1984/927a71594a16ef17d1019767fc1be328 to your computer and use it in GitHub Desktop.
Combinations in Rust
fn comb<T>(slice: &[T], k: usize) -> Vec<Vec<T>>
where
T: Copy,
{
//If k == 0, return a empty new vector
if k == 0 {
return vec![vec![]];
}
// If arr.is_empty(), return a empty new vector
if arr.is_empty() {
return Vec::<Vec<T>>::new();
}
// If k == 1, return a vector containing a vector for each element of the slice.
if k == 1 {
return slice.iter().map(|x| vec![*x]).collect::<Vec<Vec<T>>>();
}
// If k is exactly the slice length, return the slice inside a vector.
if k == slice.len() {
return vec![slice.to_vec()];
}
// Make a vector from the first element + all combinations of k - 1 elements of the rest of the slice.
let mut result = comb(&slice[1..], k - 1)
.into_iter()
.map(|x| [&slice[..1], x.as_slice()].concat())
.collect::<Vec<Vec<T>>>();
// Extend this last vector with the all the combinations of k elements after from index 1 onward.
result.extend(comb(&slice[1..], k));
// Return final vector.
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment