March 2014 update

This commit is contained in:
Ingy döt Net 2014-04-02 16:56:35 +00:00
parent 09687c4926
commit a25938f123
1846 changed files with 21876 additions and 5203 deletions

View file

@ -0,0 +1,31 @@
#include <stdio.h>
struct node {
char *s;
struct node* prev;
};
void powerset(char **v, int n, struct node *up)
{
struct node me;
if (!n) {
putchar('[');
while (up) {
printf(" %s", up->s);
up = up->prev;
}
puts(" ]");
} else {
me.s = *v;
me.prev = up;
powerset(v + 1, n - 1, up);
powerset(v + 1, n - 1, &me);
}
}
int main(int argc, char **argv)
{
powerset(argv + 1, argc - 1, 0);
return 0;
}

View file

@ -1,4 +1,4 @@
(use '[clojure.contrib.combinatorics :only [subsets] ])
(use '[clojure.math.combinatorics :only [subsets] ])
(def S #{1 2 3 4})

View file

@ -0,0 +1,7 @@
(defn powerset
[coll]
(reduce (fn [a x]
(set (concat a (map #(set (concat #{x} %)) a))))
#{#{}} coll))
(powerset #{1 2 3})

View file

@ -0,0 +1 @@
#{#{} #{1} #{2} #{1 2} #{3} #{1 3} #{2 3} #{1 2 3}}

View file

@ -1,4 +1,4 @@
auto powerSet(T)(T[] xs) {
auto powerSet(T)(T[] xs) pure nothrow {
auto output = new T[xs.length];
immutable size_t len = 1U << xs.length;

View file

@ -0,0 +1,3 @@
with(combinat):
powerset({1,2,3,4});

View file

@ -7,11 +7,10 @@
NSMutableArray *subset = [[NSMutableArray alloc] init];
for (int itemIndex = 0; itemIndex < array.count; itemIndex++) {
if((subsetIndex >> itemIndex) & 0x1) {
[subset addObject:[array objectAtIndex:itemIndex]];
[subset addObject:array[itemIndex]];
}
}
[subsets addObject:subset];
[subset release];
}
return subsets;
}

View file

@ -0,0 +1,2 @@
sub powerset(Set $s) { $s.combinations.map(*.Set).Set }
say powerset set <a b c d>;

View file

@ -0,0 +1 @@
.say for <a b c d>.combinations

View file

@ -1,3 +0,0 @@
sub powerset ( *@list ) {
reduce( -> @L, $n { [ @L, @L.map({[ $_.list, $n ]}) ] }, [[]], @list );
}

View file

@ -1 +1,14 @@
sub p{@_?map{$_,[$_[0],@$_]}p(@_[1..$#_]):[]}
use Set::Object qw(set);
sub powerset {
my $p = Set::Object->new( set() );
foreach my $i (shift->elements) {
$p->insert( map { set($_->elements, $i) } $p->elements );
}
return $p;
}
my $set = set(1, 2, 3);
my $powerset = powerset($set);
print $powerset->as_string, "\n";

View file

@ -1,4 +1,6 @@
use List::Util qw(reduce);
sub powerset {
@{ (reduce { [@$a, map([@$_, $b], @$a)] } [[]], @_) }
package Set {
sub new { bless { map {$_ => undef} @_[1..$#_] }, shift; }
sub elements { sort keys %{shift()} }
sub as_string { 'Set(' . join(' ', sort keys %{shift()}) . ')' }
# ...more set methods could be defined here...
}

View file

@ -0,0 +1,11 @@
use List::Util qw(reduce);
sub powerset {
@{( reduce { [@$a, map { Set->new($_->elements, $b) } @$a ] }
[Set->new()], shift->elements )};
}
my $set = Set->new(1, 2, 3);
my @subsets = powerset($set);
print $_->as_string, "\n" for @subsets;

View file

@ -0,0 +1,3 @@
sub powerset {
@_ ? map { $_, [$_[0], @$_] } powerset(@_[1..$#_]) : [];
}

View file

@ -0,0 +1,5 @@
use List::Util qw(reduce);
sub powerset {
@{( reduce { [@$a, map([@$_, $b], @$a)] } [[]], @_ )}
}

View file

@ -0,0 +1,8 @@
my @set = (1, 2, 3);
my @powerset = powerset(@set);
sub set_to_string {
"{" . join(", ", map { ref $_ ? set_to_string(@$_) : $_ } @_) . "}"
}
print set_to_string(@powerset), "\n";

View file

@ -1,9 +1,6 @@
power_set(T, PS) :-
bagof(PS1, power_set(T, [], PS1), PS).
powerset(X,Y) :- bagof( S, subseq(S,X), Y).
power_set(T, PS, PS1) :-
select(E, T, T1), !,
append(PS, [E], PST),
( PST = PS1; power_set(T1, PS, PS1); power_set(T1, PST, PS1)).
power_set([], [], []).
subseq( [], []).
subseq( [], [_|_]).
subseq( [X|Xs], [X|Ys] ) :- subseq(Xs, Ys).
subseq( [X|Xs], [_|Ys] ) :- append(_, [X|Zs], Ys), subseq(Xs, Zs).

View file

@ -1,18 +1,5 @@
:- use_module(library(chr)).
:- chr_constraint chr_power_set/2, chr_power_set/1, clean/0.
clean @ clean \ chr_power_set(_) <=> true.
clean @ clean <=> true.
only_one @ chr_power_set(A) \ chr_power_set(A) <=> true.
creation @ chr_power_set([H | T], A) <=>
append(A, [H], B),
chr_power_set(T, A),
chr_power_set(T, B),
chr_power_set(B).
empty_element @ chr_power_set([], _) <=> chr_power_set([]).
power_set( [], [[]]).
power_set( [X|Xs], PS) :-
power_set(Xs, PS1),
maplist( append([X]), PS1, PS2 ), % i.e. prepend X to each PS1
append(PS1, PS2, PS).

View file

@ -0,0 +1,18 @@
:- use_module(library(chr)).
:- chr_constraint chr_power_set/2, chr_power_set/1, clean/0.
clean @ clean \ chr_power_set(_) <=> true.
clean @ clean <=> true.
only_one @ chr_power_set(A) \ chr_power_set(A) <=> true.
creation @ chr_power_set([H | T], A) <=>
append(A, [H], B),
chr_power_set(T, A),
chr_power_set(T, B),
chr_power_set(B).
empty_element @ chr_power_set([], _) <=> chr_power_set([]).

View file

@ -0,0 +1,26 @@
>>> from pprint import pprint as pp
>>> from itertools import chain, combinations
>>>
>>> def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
>>> pp(set(powerset({1,2,3,4})))
{(),
(1,),
(1, 2),
(1, 2, 3),
(1, 2, 3, 4),
(1, 2, 4),
(1, 3),
(1, 3, 4),
(1, 4),
(2,),
(2, 3),
(2, 3, 4),
(2, 4),
(3,),
(3, 4),
(4,)}
>>>

View file

@ -0,0 +1,4 @@
(defun power-set (s)
(reduce-right
(op append (mapcar (op cons @@1) @2) @2)
s '(())))

View file

@ -0,0 +1,10 @@
@(do (defun power-set (s)
(reduce-right
(op append (mapcar (op cons @@1) @2) @2)
s '(()))))
@(bind pset @(power-set *args*))
@(output)
@ (repeat)
{@(rep)@pset, @(last)@pset@(empty)@(end)}
@ (end)
@(end)

View file

@ -0,0 +1,7 @@
@(do (defun power-set (s)
(reduce-right
(op append (mapcar (op cons @@1) @2) @2)
s '(())))
(prinl (power-set "abc"))
(prinl (power-set ""))
(prinl (power-set #(1 2 3))))

View file

@ -1,14 +0,0 @@
@(next :args)
@(do (defun power-set (s)
(reduce-right
(lambda (item ps)
(append (mapcar (lambda (e) (cons item e)) ps) ps)) s '(()) nil)))
@(collect :vars (arg))
@arg
@(end)
@(bind pset @(power-set arg))
@(output)
@ (repeat)
{@(rep)@pset, @(last)@pset@(empty)@(end)}
@ (end)
@(end)