Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
30
Task/Combinations/Rust/combinations-1.rust
Normal file
30
Task/Combinations/Rust/combinations-1.rust
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
fn comb<T: std::fmt::Default>(arr: &[T], n: uint) {
|
||||
let mut incl_arr: ~[bool] = std::vec::from_elem(arr.len(), false);
|
||||
comb_intern(arr, n, incl_arr, 0);
|
||||
}
|
||||
|
||||
fn comb_intern<T: std::fmt::Default>(arr: &[T], n: uint, incl_arr: &mut [bool], index: uint) {
|
||||
if (arr.len() < n + index) { return; }
|
||||
if (n == 0) {
|
||||
let mut it = arr.iter().zip(incl_arr.iter()).filter_map(|(val, incl)|
|
||||
if (*incl) { Some(val) } else { None }
|
||||
);
|
||||
for val in it { print!("{} ", *val); }
|
||||
print("\n");
|
||||
return;
|
||||
}
|
||||
|
||||
incl_arr[index] = true;
|
||||
comb_intern(arr, n-1, incl_arr, index+1);
|
||||
incl_arr[index] = false;
|
||||
|
||||
comb_intern(arr, n, incl_arr, index+1);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let arr1 = ~[1, 2, 3, 4, 5];
|
||||
comb(arr1, 3);
|
||||
|
||||
let arr2 = ~["A", "B", "C", "D", "E"];
|
||||
comb(arr2, 3);
|
||||
}
|
||||
62
Task/Combinations/Rust/combinations-2.rust
Normal file
62
Task/Combinations/Rust/combinations-2.rust
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
struct Combo<T> {
|
||||
data_len: usize,
|
||||
chunk_len: usize,
|
||||
min: usize,
|
||||
mask: usize,
|
||||
data: Vec<T>,
|
||||
}
|
||||
|
||||
impl<T: Clone> Combo<T> {
|
||||
fn new(chunk_len: i32, data: Vec<T>) -> Self {
|
||||
let d_len = data.len();
|
||||
let min = 2usize.pow(chunk_len as u32) - 1;
|
||||
let max = 2usize.pow(d_len as u32) - 2usize.pow((d_len - chunk_len as usize) as u32);
|
||||
|
||||
Combo {
|
||||
data_len: d_len,
|
||||
chunk_len: chunk_len as usize,
|
||||
min: min,
|
||||
mask: max,
|
||||
data: data,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_chunk(&self) -> Vec<T> {
|
||||
let b = format!("{:01$b}", self.mask, self.data_len);
|
||||
b
|
||||
.chars()
|
||||
.enumerate()
|
||||
.filter(|&(_, e)| e == '1')
|
||||
.map(|(i, _)| self.data[i].clone())
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Clone> Iterator for Combo<T> {
|
||||
type Item = Vec<T>;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
while self.mask >= self.min {
|
||||
if self.mask.count_ones() == self.chunk_len as u32 {
|
||||
let res = self.get_chunk();
|
||||
self.mask -= 1;
|
||||
return Some(res);
|
||||
}
|
||||
self.mask -= 1;
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let v1 = vec![1, 2, 3, 4, 5];
|
||||
let combo = Combo::new(3, v1);
|
||||
for c in combo.into_iter() {
|
||||
println!("{:?}", c);
|
||||
}
|
||||
|
||||
let v2 = vec!("A", "B", "C", "D", "E");
|
||||
let combo = Combo::new(3, v2);
|
||||
for c in combo.into_iter() {
|
||||
println!("{:?}", c);
|
||||
}
|
||||
}
|
||||
24
Task/Combinations/Rust/combinations-3.rust
Normal file
24
Task/Combinations/Rust/combinations-3.rust
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
fn comb<T>(slice: &[T], k: usize) -> Vec<Vec<T>>
|
||||
where
|
||||
T: Copy,
|
||||
{
|
||||
// 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue