I have a function in Rust which returns a vector of vectors of the number of combinations possible from a set given a number of digits for which the combinations are needed(nCk).
If the set is [1,2,3]
and the number of digits(k) is 2, the output is:
[[1, 2], [1, 3], [2, 3]]
However, if I choose k to be a large number, it takes too much time and prints all of the output at once after it has finished processing.
Is it possible that it keeps giving me running output on the go?
In Java, I have the same thing and it gives me running output.
Here's my function:
fn comb<T>(slice: &[T], k: usize) -> Vec<Vec<T>>
where
T: Copy,
{
if k == 1 {
return slice.iter().map(|x| vec![*x]).collect::<Vec<Vec<T>>>();
}
if k == slice.len() {
return vec![slice.to_vec()];
}
let mut result = comb(&slice[1..], k - 1)
.into_iter()
.map(|x| [&slice[..1], x.as_slice()].concat())
.collect::<Vec<Vec<T>>>();
result.extend(comb(&slice[1..], k));
return result;
}