March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
5
Task/Combinations/Haskell/combinations-7.hs
Normal file
5
Task/Combinations/Haskell/combinations-7.hs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
comb :: Int -> [a] -> [[a]]
|
||||
comb m xs = combsBySize xs !! m
|
||||
where
|
||||
combsBySize = foldr f ([[]] : repeat [])
|
||||
f x next = zipWith (++) (map (map (x:)) ([]:next)) next
|
||||
16
Task/Combinations/OCaml/combinations-2.ocaml
Normal file
16
Task/Combinations/OCaml/combinations-2.ocaml
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
let comb m xs =
|
||||
let xs = Array.of_list xs in
|
||||
if m > Array.length xs then
|
||||
[]
|
||||
else begin
|
||||
let arr = Array.make (m+1) [] in
|
||||
arr.(0) <- [[]];
|
||||
for j = 0 to Array.length xs - m do
|
||||
for i = 1 to m do
|
||||
arr.(i) <- arr.(i) @ List.map (fun ys -> xs.(j+i-1)::ys) arr.(i-1)
|
||||
done
|
||||
done;
|
||||
arr.(m)
|
||||
end
|
||||
;;
|
||||
comb 3 [0;1;2;3;4];;
|
||||
|
|
@ -1 +1 @@
|
|||
.say for (^5).list.combinations(3).tree;
|
||||
.say for combinations(5,3);
|
||||
|
|
|
|||
30
Task/Combinations/Rust/combinations.rust
Normal file
30
Task/Combinations/Rust/combinations.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);
|
||||
}
|
||||
5
Task/Combinations/TXR/combinations.txr
Normal file
5
Task/Combinations/TXR/combinations.txr
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
@(do
|
||||
(defun comb-n-m (n m)
|
||||
(comb (range* 0 n) m))
|
||||
|
||||
(put-line `3 comb 5 = @(comb-n-m 5 3)`))
|
||||
Loading…
Add table
Add a link
Reference in a new issue