September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,43 +1,76 @@
-- comb :: Int -> [a] -> [[a]]
on comb(n, lst)
set h to head(lst)
script headPrepended
on lambda(t)
h & t
end lambda
end script
if n < 1 then
[[]]
else if length of lst = 0 then
[]
{{}}
else
set xs to tail(lst)
if not isNull(lst) then
set {h, xs} to uncons(lst)
map(headPrepended, ¬
comb(n - 1, xs)) & comb(n, xs)
map(curry(my cons)'s |λ|(h), comb(n - 1, xs)) & comb(n, xs)
else
{}
end if
end if
end comb
-- TEST
-- spaced :: [a] -> String
on spaced(lst)
intercalate(space, lst)
end spaced
-- TEST -----------------------------------------------------------------------
on run
intercalate(linefeed, ¬
map(spaced, comb(3, range(0, 4))))
map(unwords, comb(3, enumFromTo(0, 4))))
end run
-- GENERIC FUNCTIONS ----------------------------------------------------------
-- cons :: a -> [a] -> [a]
on cons(x, xs)
{x} & xs
end cons
-- GENERIC FUNCTIONS
-- curry :: (Script|Handler) -> Script
on curry(f)
script
on |λ|(a)
script
on |λ|(b)
|λ|(a, b) of mReturn(f)
end |λ|
end script
end |λ|
end script
end curry
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if n < m then
set d to -1
else
set d to 1
end if
set lst to {}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
end enumFromTo
-- intercalate :: Text -> [Text] -> Text
on intercalate(strText, lstText)
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined to lstText as text
set my text item delimiters to dlm
return strJoined
end intercalate
-- isNull :: [a] -> Bool
on isNull(xs)
if class of xs is string then
xs = ""
else
xs = {}
end if
end isNull
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
@ -45,7 +78,7 @@ on map(f, xs)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda(item i of xs, i, xs)
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
@ -58,47 +91,27 @@ on mReturn(f)
f
else
script
property lambda : f
property |λ| : f
end script
end if
end mReturn
-- intercalate :: Text -> [Text] -> Text
on intercalate(strText, lstText)
set {dlm, my text item delimiters} to {my text item delimiters, strText}
set strJoined to lstText as text
set my text item delimiters to dlm
return strJoined
end intercalate
-- range :: Int -> Int -> [Int]
on range(m, n)
if n < m then
set d to -1
else
set d to 1
end if
set lst to {}
repeat with i from m to n by d
set end of lst to i
end repeat
return lst
end range
-- head :: [a] -> a
on head(xs)
if length of xs > 0 then
item 1 of xs
-- uncons :: [a] -> Maybe (a, [a])
on uncons(xs)
set lng to length of xs
if lng > 0 then
if class of xs is string then
set cs to text items of xs
{item 1 of cs, rest of cs}
else
{item 1 of xs, rest of xs}
end if
else
missing value
end if
end head
end uncons
-- tail :: [a] -> [a]
on tail(xs)
if length of xs > 1 then
items 2 thru -1 of xs
else
{}
end if
end tail
-- unwords :: [String] -> String
on unwords(xs)
intercalate(space, xs)
end unwords

View file

@ -1,25 +0,0 @@
combinations = (n, p) ->
return [ [] ] if p == 0
i = 0
combos = []
combo = []
while combo.length < p
if i < n
combo.push i
i += 1
else
break if combo.length == 0
i = combo.pop() + 1
if combo.length == p
combos.push clone combo
i = combo.pop() + 1
combos
clone = (arr) -> (n for n in arr)
N = 5
for i in [0..N]
console.log "------ #{N} #{i}"
for combo in combinations N, i
console.log combo

View file

@ -1,39 +0,0 @@
> coffee combo.coffee
------ 5 0
[]
------ 5 1
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
------ 5 2
[ 0, 1 ]
[ 0, 2 ]
[ 0, 3 ]
[ 0, 4 ]
[ 1, 2 ]
[ 1, 3 ]
[ 1, 4 ]
[ 2, 3 ]
[ 2, 4 ]
[ 3, 4 ]
------ 5 3
[ 0, 1, 2 ]
[ 0, 1, 3 ]
[ 0, 1, 4 ]
[ 0, 2, 3 ]
[ 0, 2, 4 ]
[ 0, 3, 4 ]
[ 1, 2, 3 ]
[ 1, 2, 4 ]
[ 1, 3, 4 ]
[ 2, 3, 4 ]
------ 5 4
[ 0, 1, 2, 3 ]
[ 0, 1, 2, 4 ]
[ 0, 1, 3, 4 ]
[ 0, 2, 3, 4 ]
[ 1, 2, 3, 4 ]
------ 5 5
[ 0, 1, 2, 3, 4 ]

View file

@ -1,25 +1,22 @@
#define system.
#define system'routines.
#define extensions.
#define extensions'routines.
import system'routines.
import extensions.
import extensions'routines.
#symbol(const)M = 3.
#symbol(const)N = 5.
const int M = 3.
const int N = 5.
// --- Numbers ---
#symbol numbers = (:anN)
numbers = (:anN)
[
Array new &length:(anN int) set &every: (&index:n) [ Integer new &int:n ]
^ Array new length:anN; populate(:n)( n )
].
// --- Program ---
#symbol program =
program =
[
#var aNumbers := numbers eval:N.
Combinator new:M &of:aNumbers run &each: aRow
var aNumbers := numbers eval:N.
Combinator new:M of:aNumbers; forEach(:aRow)
[
console writeLine:aRow.
console printLine:aRow
].
console readChar.
].

View file

@ -1,10 +0,0 @@
1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5

View file

@ -1,47 +1,65 @@
(function (n) {
'use strict';
(() => {
// n -> [a] -> [[a]]
let comb = (n, xs) => {
if (n < 1) return [[]];
// combinations :: Int -> [a] -> [[a]]
const combinations = (n, xs) => {
const cmb_ = (n, xs) => {
if (n < 1) return [
[]
];
if (xs.length === 0) return [];
let h = xs[0],
const h = xs[0],
tail = xs.slice(1);
return cmb_(n - 1, tail)
.map(cons(h))
.concat(cmb_(n, tail));
};
return memoized(cmb_)(n, xs);
}
return comb(n - 1, tail)
.map((t) => [h].concat(t))
.concat(comb(n, tail));
},
// GENERIC FUNCTIONS ------------------------------------------------------
// 2 or more arguments
// curry :: Function -> Function
const curry = (f, ...args) => {
const go = xs => xs.length >= f.length ? (f.apply(null, xs)) :
function () {
return go(xs.concat(Array.from(arguments)));
};
return go([].slice.call(args, 1));
};
// cons :: a -> [a] -> [a]
const cons = curry((x, xs) => [x].concat(xs));
// Derive a memoized version of a function
// Function -> Function
memoized = (f) => {
let m = {};
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = (m, n) =>
Array.from({
length: Math.floor(n - m) + 1
}, (_, i) => m + i);
return function (x) {
let args = [].slice.call(arguments),
strKey = args.join('-'),
v = m[strKey];
// Derive a memoized version of a function
// memoized :: Function -> Function
const memoized = f => {
let m = {};
return function (x) {
let args = [].slice.call(arguments),
strKey = args.join('-'),
v = m[strKey];
return (
(v === undefined) &&
(m[strKey] = v = f.apply(null, args)),
v
);
}
};
return (
(v === undefined) &&
(m[strKey] = v = f.apply(null, args)),
v
);
}
},
// show :: a -> String
const show = (...x) =>
JSON.stringify.apply(
null, x.length > 1 ? [x[0], null, x[1]] : x
);
range = (m, n) =>
Array.from({
length: (n - m) + 1
}, (_, i) => m + i);
return memoized(comb)(n, range(0, 4))
})(3);
return show(
memoized(combinations)(3, enumFromTo(0, 4))
);
})();

View file

@ -0,0 +1,68 @@
(() => {
'use strict';
// COMBINATIONS -----------------------------------------------------------
// comb :: Int -> Int -> [[Int]]
const comb = (m, n) => combinations(m, enumFromTo(0, n - 1));
// combinations :: Int -> [a] -> [[a]]
const combinations = (k, xs) =>
sort(filter(xs => k === xs.length, subsequences(xs)));
// GENERIC FUNCTIONS -----------------------------------------------------
// cons :: a -> [a] -> [a]
const cons = (x, xs) => [x].concat(xs);
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = (m, n) =>
Array.from({
length: Math.floor(n - m) + 1
}, (_, i) => m + i);
// filter :: (a -> Bool) -> [a] -> [a]
const filter = (f, xs) => xs.filter(f);
// foldr (a -> b -> b) -> b -> [a] -> b
const foldr = (f, a, xs) => xs.reduceRight(f, a);
// isNull :: [a] -> Bool
const isNull = xs => (xs instanceof Array) ? xs.length < 1 : undefined;
// show :: a -> String
const show = x => JSON.stringify(x) //, null, 2);
// sort :: Ord a => [a] -> [a]
const sort = xs => xs.sort();
// stringChars :: String -> [Char]
const stringChars = s => s.split('');
// subsequences :: [a] -> [[a]]
const subsequences = xs => {
// nonEmptySubsequences :: [a] -> [[a]]
const nonEmptySubsequences = xxs => {
if (isNull(xxs)) return [];
const [x, xs] = uncons(xxs);
const f = (r, ys) => cons(ys, cons(cons(x, ys), r));
return cons([x], foldr(f, [], nonEmptySubsequences(xs)));
};
return nonEmptySubsequences(
(typeof xs === 'string' ? stringChars(xs) : xs)
);
};
// uncons :: [a] -> Maybe (a, [a])
const uncons = xs => xs.length ? [xs[0], xs.slice(1)] : undefined;
// TEST -------------------------------------------------------------------
return show(
comb(3, 5)
);
})();

View file

@ -1,3 +1,4 @@
using Combinatorics
n = 4
m = 3
for i in combinations(0:n,m)

View file

@ -0,0 +1,25 @@
class Combinations(val m: Int, val n: Int) {
private val combination = IntArray(m)
init {
generate(0)
}
private fun generate(k: Int) {
if (k >= m) {
for (i in 0 until m) print("${combination[i]} ")
println()
}
else {
for (j in 0 until n)
if (k == 0 || j > combination[k - 1]) {
combination[k] = j
generate(k + 1)
}
}
}
}
fun main(args: Array<String>) {
Combinations(3, 5)
}

View file

@ -1,16 +1,21 @@
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)
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];;

View file

@ -0,0 +1,50 @@
<?php
//Author: I. Gavryushin aka dcc0
//set amount of elements as in $n var
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>

View file

@ -1,10 +0,0 @@
sub combinations(Int $n, Int $k) {
return [] if $k == 0;
return () if $k > $n;
gather {
take [0, (1..^$n)[@$_]] for combinations($n-1, $k-1);
take [(1..^$n)[@$_]] for combinations($n-1, $k );
}
}
.say for combinations(5, 3);

View file

@ -1,3 +0,0 @@
:- use_module(library(clpfd)).
comb_lstcomp(N, M, V) :-
V <- {L & length(L, N), L ins 1..M & all_distinct(L), chain(L, #<), label(L)}.

View file

@ -1 +1 @@
(@^5).combinations(3, {|c| say c })
combinations(5, 3, {|*c| say c })

View file

@ -0,0 +1,13 @@
program combin
tempfile cp
tempvar k
gen `k'=1
quietly save "`cp'"
rename `1' `1'1
forv i=2/`2' {
joinby `k' using "`cp'"
rename `1' `1'`i'
quietly drop if `1'`i'<=`1'`=`i'-1'
}
sort `1'*
end

View file

@ -0,0 +1,20 @@
. set obs 5
. gen a=_n
. combin a 3
. list
+--------------+
| a1 a2 a3 |
|--------------|
1. | 1 2 3 |
2. | 1 2 4 |
3. | 1 2 5 |
4. | 1 3 4 |
5. | 1 3 5 |
|--------------|
6. | 1 4 5 |
7. | 2 3 4 |
8. | 2 3 5 |
9. | 2 4 5 |
10. | 3 4 5 |
+--------------+

View file

@ -0,0 +1,9 @@
fcn comb(k,seq){ // no repeats, seq is finite
seq=seq.makeReadOnly(); // because I append to parts of seq
fcn(k,seq){
if(k<=0) return(T(T));
if(not seq) return(T);
self.fcn(k-1,seq[1,*]).pump(List,seq[0,1].extend)
.extend(self.fcn(k,seq[1,*]));
}(k,seq);
}

View file

@ -0,0 +1 @@
comb(3,"abcde".split("")).apply("concat")