Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
10
Task/Power-set/Ada/power-set-1.ada
Normal file
10
Task/Power-set/Ada/power-set-1.ada
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
package Power_Set is
|
||||
|
||||
type Set is array (Positive range <>) of Positive;
|
||||
Empty_Set: Set(1 .. 0);
|
||||
|
||||
generic
|
||||
with procedure Visit(S: Set);
|
||||
procedure All_Subsets(S: Set); -- calles Visit once for each subset of S
|
||||
|
||||
end Power_Set;
|
||||
20
Task/Power-set/Ada/power-set-2.ada
Normal file
20
Task/Power-set/Ada/power-set-2.ada
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
package body Power_Set is
|
||||
|
||||
procedure All_Subsets(S: Set) is
|
||||
|
||||
procedure Visit_Sets(Unmarked: Set; Marked: Set) is
|
||||
Tail: Set := Unmarked(Unmarked'First+1 .. Unmarked'Last);
|
||||
begin
|
||||
if Unmarked = Empty_Set then
|
||||
Visit(Marked);
|
||||
else
|
||||
Visit_Sets(Tail, Marked & Unmarked(Unmarked'First));
|
||||
Visit_Sets(Tail, Marked);
|
||||
end if;
|
||||
end Visit_Sets;
|
||||
|
||||
begin
|
||||
Visit_Sets(S, Empty_Set);
|
||||
end All_Subsets;
|
||||
|
||||
end Power_Set;
|
||||
28
Task/Power-set/Ada/power-set-3.ada
Normal file
28
Task/Power-set/Ada/power-set-3.ada
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
with Ada.Text_IO, Ada.Command_Line, Power_Set;
|
||||
|
||||
procedure Print_Power_Set is
|
||||
|
||||
procedure Print_Set(Items: Power_Set.Set) is
|
||||
First: Boolean := True;
|
||||
begin
|
||||
Ada.Text_IO.Put("{ ");
|
||||
for Item of Items loop
|
||||
if First then
|
||||
First := False; -- no comma needed
|
||||
else
|
||||
Ada.Text_IO.Put(", "); -- comma, to separate the items
|
||||
end if;
|
||||
Ada.Text_IO.Put(Ada.Command_Line.Argument(Item));
|
||||
end loop;
|
||||
Ada.Text_IO.Put_Line(" }");
|
||||
end Print_Set;
|
||||
|
||||
procedure Print_All_Subsets is new Power_Set.All_Subsets(Print_Set);
|
||||
|
||||
Set: Power_Set.Set(1 .. Ada.Command_Line.Argument_Count);
|
||||
begin
|
||||
for I in Set'Range loop -- initialize set
|
||||
Set(I) := I;
|
||||
end loop;
|
||||
Print_All_Subsets(Set); -- do the work
|
||||
end;
|
||||
|
|
@ -1,42 +0,0 @@
|
|||
with Ada.Text_IO, Ada.Command_Line;
|
||||
|
||||
procedure Power_Set is
|
||||
|
||||
type List is array (Positive range <>) of Positive;
|
||||
Empty: List(1 .. 0);
|
||||
|
||||
procedure Print_All_Subsets(Set: List; Printable: List:= Empty) is
|
||||
|
||||
procedure Print_Set(Items: List) is
|
||||
First: Boolean := True;
|
||||
begin
|
||||
Ada.Text_IO.Put("{ ");
|
||||
for Item of Items loop
|
||||
if First then
|
||||
First := False; -- no comma needed
|
||||
else
|
||||
Ada.Text_IO.Put(", "); -- comma, to separate the items
|
||||
end if;
|
||||
Ada.Text_IO.Put(Ada.Command_Line.Argument(Item));
|
||||
end loop;
|
||||
Ada.Text_IO.Put_Line(" }");
|
||||
end Print_Set;
|
||||
|
||||
Tail: List := Set(Set'First+1 .. Set'Last);
|
||||
|
||||
begin
|
||||
if Set = Empty then
|
||||
Print_Set(Printable);
|
||||
else
|
||||
Print_All_Subsets(Tail, Printable & Set(Set'First));
|
||||
Print_All_Subsets(Tail, Printable);
|
||||
end if;
|
||||
end Print_All_Subsets;
|
||||
|
||||
Set: List(1 .. Ada.Command_Line.Argument_Count);
|
||||
begin
|
||||
for I in Set'Range loop -- initialize set
|
||||
Set(I) := I;
|
||||
end loop;
|
||||
Print_All_Subsets(Set); -- do the work
|
||||
end Power_Set;
|
||||
|
|
@ -1,25 +1,34 @@
|
|||
#include <iostream>
|
||||
#include <set>
|
||||
#include <iostream>
|
||||
|
||||
template<typename Set> std::set<Set> powerset(const Set& s, size_t n)
|
||||
template <class S>
|
||||
auto powerset(const S& s)
|
||||
{
|
||||
typedef typename Set::const_iterator SetCIt;
|
||||
typedef typename std::set<Set>::const_iterator PowerSetCIt;
|
||||
std::set<Set> res;
|
||||
if(n > 0) {
|
||||
std::set<Set> ps = powerset(s, n-1);
|
||||
for(PowerSetCIt ss = ps.begin(); ss != ps.end(); ss++)
|
||||
for(SetCIt el = s.begin(); el != s.end(); el++) {
|
||||
Set subset(*ss);
|
||||
subset.insert(*el);
|
||||
res.insert(subset);
|
||||
}
|
||||
res.insert(ps.begin(), ps.end());
|
||||
} else
|
||||
res.insert(Set());
|
||||
return res;
|
||||
std::set<S> ret;
|
||||
ret.emplace();
|
||||
for (auto&& e: s) {
|
||||
std::set<S> rs;
|
||||
for (auto x: ret) {
|
||||
x.insert(e);
|
||||
rs.insert(x);
|
||||
}
|
||||
ret.insert(begin(rs), end(rs));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template<typename Set> std::set<Set> powerset(const Set& s)
|
||||
|
||||
int main()
|
||||
{
|
||||
return powerset(s, s.size());
|
||||
std::set<int> s = {2, 3, 5, 7};
|
||||
auto pset = powerset(s);
|
||||
|
||||
for (auto&& subset: pset) {
|
||||
std::cout << "{ ";
|
||||
char const* prefix = "";
|
||||
for (auto&& e: subset) {
|
||||
std::cout << prefix << e;
|
||||
prefix = ", ";
|
||||
}
|
||||
std::cout << " }\n";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
25
Task/Power-set/C++/power-set-3.cpp
Normal file
25
Task/Power-set/C++/power-set-3.cpp
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#include <iostream>
|
||||
#include <set>
|
||||
|
||||
template<typename Set> std::set<Set> powerset(const Set& s, size_t n)
|
||||
{
|
||||
typedef typename Set::const_iterator SetCIt;
|
||||
typedef typename std::set<Set>::const_iterator PowerSetCIt;
|
||||
std::set<Set> res;
|
||||
if(n > 0) {
|
||||
std::set<Set> ps = powerset(s, n-1);
|
||||
for(PowerSetCIt ss = ps.begin(); ss != ps.end(); ss++)
|
||||
for(SetCIt el = s.begin(); el != s.end(); el++) {
|
||||
Set subset(*ss);
|
||||
subset.insert(*el);
|
||||
res.insert(subset);
|
||||
}
|
||||
res.insert(ps.begin(), ps.end());
|
||||
} else
|
||||
res.insert(Set());
|
||||
return res;
|
||||
}
|
||||
template<typename Set> std::set<Set> powerset(const Set& s)
|
||||
{
|
||||
return powerset(s, s.size());
|
||||
}
|
||||
|
|
@ -1,8 +1,4 @@
|
|||
(defun power-set (s)
|
||||
(reduce #'(lambda (item ps)
|
||||
(append (mapcar #'(lambda (e) (cons item e))
|
||||
ps)
|
||||
ps))
|
||||
s
|
||||
:from-end t
|
||||
:initial-value '(())))
|
||||
(defun powerset (s)
|
||||
(if s (mapcan (lambda (x) (list (cons (car s) x) x))
|
||||
(powerset (cdr s)))
|
||||
'(())))
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
(defun powerset (l)
|
||||
(if (null l)
|
||||
(list nil)
|
||||
(let ((prev (powerset (cdr l))))
|
||||
(append (mapcar #'(lambda (elt) (cons (car l) elt)) prev)
|
||||
prev))))
|
||||
(defun power-set (s)
|
||||
(reduce #'(lambda (item ps)
|
||||
(append (mapcar #'(lambda (e) (cons item e))
|
||||
ps)
|
||||
ps))
|
||||
s
|
||||
:from-end t
|
||||
:initial-value '(())))
|
||||
|
|
|
|||
|
|
@ -1,3 +1,6 @@
|
|||
(defun powerset (xs)
|
||||
(loop for i below (expt 2 (length xs)) collect
|
||||
(loop for j below i for x in xs if (logbitp j i) collect x)))
|
||||
(defun powerset (l)
|
||||
(if (null l)
|
||||
(list nil)
|
||||
(let ((prev (powerset (cdr l))))
|
||||
(append (mapcar #'(lambda (elt) (cons (car l) elt)) prev)
|
||||
prev))))
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
(defun power-set (list)
|
||||
(let ((pow-set (list nil)))
|
||||
(dolist (element (reverse list) pow-set)
|
||||
(dolist (set pow-set)
|
||||
(push (cons element set) pow-set)))))
|
||||
(defun powerset (xs)
|
||||
(loop for i below (expt 2 (length xs)) collect
|
||||
(loop for j below i for x in xs if (logbitp j i) collect x)))
|
||||
|
|
|
|||
5
Task/Power-set/Common-Lisp/power-set-5.lisp
Normal file
5
Task/Power-set/Common-Lisp/power-set-5.lisp
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(defun power-set (list)
|
||||
(let ((pow-set (list nil)))
|
||||
(dolist (element (reverse list) pow-set)
|
||||
(dolist (set pow-set)
|
||||
(push (cons element set) pow-set)))))
|
||||
|
|
@ -1,16 +1,27 @@
|
|||
T[][] powerSet(T)(in T[] s) pure nothrow @safe {
|
||||
auto r = new typeof(return)(1, 0);
|
||||
foreach (e; s) {
|
||||
typeof(return) rs;
|
||||
foreach (x; r)
|
||||
rs ~= x ~ [e];
|
||||
r ~= rs;
|
||||
}
|
||||
return r;
|
||||
import std.algorithm;
|
||||
import std.range;
|
||||
|
||||
auto powerSet(R)(R r)
|
||||
{
|
||||
return
|
||||
(1L<<r.length)
|
||||
.iota
|
||||
.map!(i =>
|
||||
r.enumerate
|
||||
.filter!(t => (1<<t[0]) & i)
|
||||
.map!(t => t[1])
|
||||
);
|
||||
}
|
||||
|
||||
void main() {
|
||||
import std.stdio;
|
||||
|
||||
[1, 2, 3].powerSet.writeln;
|
||||
unittest
|
||||
{
|
||||
int[] emptyArr;
|
||||
assert(emptyArr.powerSet.equal!equal([emptyArr]));
|
||||
assert(emptyArr.powerSet.powerSet.equal!(equal!equal)([[], [emptyArr]]));
|
||||
}
|
||||
|
||||
void main(string[] args)
|
||||
{
|
||||
import std.stdio;
|
||||
args[1..$].powerSet.each!writeln;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,37 +1,42 @@
|
|||
auto powerSet(T)(T[] xs) pure nothrow @safe {
|
||||
static struct Result {
|
||||
T[] xsLocal, output;
|
||||
size_t len;
|
||||
size_t bits;
|
||||
import std.range;
|
||||
|
||||
this(T[] xs_) pure nothrow @safe {
|
||||
this.xsLocal = xs_;
|
||||
this.output.length = xs_.length;
|
||||
this.len = 1U << xs_.length;
|
||||
}
|
||||
struct PowerSet(R)
|
||||
if (isRandomAccessRange!R)
|
||||
{
|
||||
R r;
|
||||
size_t position;
|
||||
|
||||
@property empty() const pure nothrow @safe {
|
||||
return bits == len;
|
||||
}
|
||||
struct PowerSetItem
|
||||
{
|
||||
R r;
|
||||
size_t position;
|
||||
|
||||
void popFront() pure nothrow @safe { bits++; }
|
||||
@property save() pure nothrow @safe { return this; }
|
||||
private void advance()
|
||||
{
|
||||
while (!(position & 1))
|
||||
{
|
||||
r.popFront();
|
||||
position >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
T[] front() pure nothrow @safe {
|
||||
size_t pos = 0;
|
||||
foreach (immutable size_t i; 0 .. xsLocal.length)
|
||||
if (bits & (1 << i))
|
||||
output[pos++] = xsLocal[i];
|
||||
return output[0 .. pos];
|
||||
}
|
||||
}
|
||||
@property bool empty() { return position == 0; }
|
||||
@property auto front()
|
||||
{
|
||||
advance();
|
||||
return r.front;
|
||||
}
|
||||
void popFront()
|
||||
{
|
||||
advance();
|
||||
r.popFront();
|
||||
position >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
return Result(xs);
|
||||
@property bool empty() { return position == (1 << r.length); }
|
||||
@property PowerSetItem front() { return PowerSetItem(r.save, position); }
|
||||
void popFront() { position++; }
|
||||
}
|
||||
|
||||
version (power_set2_main) {
|
||||
void main() {
|
||||
import std.stdio;
|
||||
[1, 2, 3].powerSet.writeln;
|
||||
}
|
||||
}
|
||||
auto powerSet(R)(R r) { return PowerSet!R(r); }
|
||||
|
|
|
|||
29
Task/Power-set/Elixir/power-set.elixir
Normal file
29
Task/Power-set/Elixir/power-set.elixir
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
defmodule RC do
|
||||
use Bitwise
|
||||
def powerset1(list) do
|
||||
n = length(list)
|
||||
max = round(:math.pow(2,n))
|
||||
for i <- 0..max-1, do: (for pos <- 0..n-1, band(i, bsl(1, pos)) != 0, do: Enum.at(list, pos) )
|
||||
end
|
||||
|
||||
def powerset2([]), do: [[]]
|
||||
def powerset2([h|t]) do
|
||||
pt = powerset2(t)
|
||||
(for x <- pt, do: [h|x]) ++ pt
|
||||
end
|
||||
|
||||
def powerset3([]), do: [[]]
|
||||
def powerset3([h|t]) do
|
||||
pt = powerset3(t)
|
||||
powerset3(h, pt, pt)
|
||||
end
|
||||
|
||||
defp powerset3(_, [], acc), do: acc
|
||||
defp powerset3(x, [h|t], acc), do: powerset3(x, t, [[x|h] | acc])
|
||||
end
|
||||
|
||||
IO.inspect RC.powerset1([1,2,3])
|
||||
IO.inspect RC.powerset2([1,2,3])
|
||||
IO.inspect RC.powerset3([1,2,3])
|
||||
IO.inspect RC.powerset1([])
|
||||
IO.inspect RC.powerset1(["one"])
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
function powerset (x)
|
||||
result = {{}}
|
||||
for i in x, j = 1:length(result)
|
||||
push!(result, [result[j],i])
|
||||
end
|
||||
result
|
||||
function powerset{T}(x::Vector{T})
|
||||
result = Vector{T}[[]]
|
||||
for elem in x, j in eachindex(result)
|
||||
push!(result, [result[j] ; elem])
|
||||
end
|
||||
result
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,3 +1 @@
|
|||
with(combinat):
|
||||
|
||||
powerset({1,2,3,4});
|
||||
combinat:-powerset({1,2,3,4});
|
||||
|
|
|
|||
|
|
@ -1,14 +1,8 @@
|
|||
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;
|
||||
use Algorithm::Combinatorics "subsets";
|
||||
my @S = ("a","b","c");
|
||||
my @PS;
|
||||
my $iter = subsets(\@S);
|
||||
while (my $p = $iter->next) {
|
||||
push @PS, "[@$p]"
|
||||
}
|
||||
|
||||
my $set = set(1, 2, 3);
|
||||
my $powerset = powerset($set);
|
||||
|
||||
print $powerset->as_string, "\n";
|
||||
say join(" ",@PS);
|
||||
|
|
|
|||
21
Task/Power-set/Perl/power-set-10.pl
Normal file
21
Task/Power-set/Perl/power-set-10.pl
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
sub powerset(&@) {
|
||||
my $callback = shift;
|
||||
my $bitmask = '';
|
||||
my $bytes = @_/8;
|
||||
{
|
||||
my @indices = grep vec($bitmask, $_, 1), 0..$#_;
|
||||
$callback->( @_[@indices] );
|
||||
++vec($bitmask, $_, 8) and last for 0 .. $bytes;
|
||||
redo if @indices != @_;
|
||||
}
|
||||
}
|
||||
|
||||
print "powerset of empty set:\n";
|
||||
powerset { print "[@_]\n" };
|
||||
print "powerset of set {1,2,3,4}:\n";
|
||||
powerset { print "[@_]\n" } 1..4;
|
||||
my $i = 0;
|
||||
powerset { ++$i } 1..9;
|
||||
print "The powerset of a nine element set contains $i elements.\n";
|
||||
|
|
@ -1,6 +1,4 @@
|
|||
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...
|
||||
}
|
||||
use ntheory "vecextract";
|
||||
my @S=("a","b","c");
|
||||
my @PS = map { "[".join(" ",vecextract(\@S,$_))."]" } 0..2**scalar(@S)-1;
|
||||
say join(" ",@PS);
|
||||
|
|
|
|||
|
|
@ -1,11 +1,7 @@
|
|||
use List::Util qw(reduce);
|
||||
|
||||
sub powerset {
|
||||
@{( reduce { [@$a, map { Set->new($_->elements, $b) } @$a ] }
|
||||
[Set->new()], shift->elements )};
|
||||
use ntheory "forcomb";
|
||||
my @S=("a","b","c");
|
||||
for $k (0..@S) {
|
||||
# Iterate over each $#S+1,$k combination.
|
||||
forcomb { print "[@S[@_]] " } @S,$k;
|
||||
}
|
||||
|
||||
my $set = Set->new(1, 2, 3);
|
||||
my @subsets = powerset($set);
|
||||
|
||||
print $_->as_string, "\n" for @subsets;
|
||||
print "\n";
|
||||
|
|
|
|||
|
|
@ -1,3 +1,14 @@
|
|||
use Set::Object qw(set);
|
||||
|
||||
sub powerset {
|
||||
@_ ? map { $_, [$_[0], @$_] } powerset(@_[1..$#_]) : [];
|
||||
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";
|
||||
|
|
|
|||
|
|
@ -1,5 +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...
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
my @set = (1, 2, 3);
|
||||
my @powerset = powerset(@set);
|
||||
use List::Util qw(reduce);
|
||||
|
||||
sub set_to_string {
|
||||
"{" . join(", ", map { ref $_ ? set_to_string(@$_) : $_ } @_) . "}"
|
||||
sub powerset {
|
||||
@{( reduce { [@$a, map { Set->new($_->elements, $b) } @$a ] }
|
||||
[Set->new()], shift->elements )};
|
||||
}
|
||||
|
||||
print set_to_string(@powerset), "\n";
|
||||
my $set = Set->new(1, 2, 3);
|
||||
my @subsets = powerset($set);
|
||||
|
||||
print $_->as_string, "\n" for @subsets;
|
||||
|
|
|
|||
|
|
@ -1,21 +1,3 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
sub powerset(&@) {
|
||||
my $callback = shift;
|
||||
my $bitmask = '';
|
||||
my $bytes = @_/8;
|
||||
{
|
||||
my @indices = grep vec($bitmask, $_, 1), 0..$#_;
|
||||
$callback->( @_[@indices] );
|
||||
++vec($bitmask, $_, 8) and last for 0 .. $bytes;
|
||||
redo if @indices != @_;
|
||||
}
|
||||
sub powerset {
|
||||
@_ ? map { $_, [$_[0], @$_] } powerset(@_[1..$#_]) : [];
|
||||
}
|
||||
|
||||
print "powerset of empty set:\n";
|
||||
powerset { print "[@_]\n" };
|
||||
print "powerset of set {1,2,3,4}:\n";
|
||||
powerset { print "[@_]\n" } 1..4;
|
||||
my $i = 0;
|
||||
powerset { ++$i } 1..9;
|
||||
print "The powerset of a nine element set contains $i elements.\n";
|
||||
|
|
|
|||
5
Task/Power-set/Perl/power-set-8.pl
Normal file
5
Task/Power-set/Perl/power-set-8.pl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
use List::Util qw(reduce);
|
||||
|
||||
sub powerset {
|
||||
@{( reduce { [@$a, map([@$_, $b], @$a)] } [[]], @_ )}
|
||||
}
|
||||
8
Task/Power-set/Perl/power-set-9.pl
Normal file
8
Task/Power-set/Perl/power-set-9.pl
Normal 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";
|
||||
34
Task/Power-set/PowerShell/power-set.psh
Normal file
34
Task/Power-set/PowerShell/power-set.psh
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
function power-set ($array) {
|
||||
if($array) {
|
||||
$n = $array.Count
|
||||
function state($set, $i){
|
||||
if($i -gt -1) {
|
||||
state $set ($i-1)
|
||||
state ($set+@($array[$i])) ($i-1)
|
||||
} else {
|
||||
"$($set | sort)"
|
||||
}
|
||||
}
|
||||
$set = state @() ($n-1)
|
||||
$power = 0..($set.Count-1) | foreach{@(0)}
|
||||
$i = 0
|
||||
$set | sort | foreach{$power[$i++] = $_.Split()}
|
||||
$power | sort {$_.Count}
|
||||
} else {@()}
|
||||
|
||||
}
|
||||
$OFS = " "
|
||||
$setA = power-set @(1,2,3,4)
|
||||
"number of sets in setA: $($setA.Count)"
|
||||
"sets in setA:"
|
||||
$OFS = ", "
|
||||
$setA | foreach{"{"+"$_"+"}"}
|
||||
$setB = @()
|
||||
"number of sets in setB: $($setB.Count)"
|
||||
"sets in setB:"
|
||||
$setB | foreach{"{"+"$_"+"}"}
|
||||
$setC = @(@(), @(@()))
|
||||
"number of sets in setC: $($setC.Count)"
|
||||
"sets in setC:"
|
||||
$setC | foreach{"{"+"$_"+"}"}
|
||||
$OFS = " "
|
||||
|
|
@ -1,29 +1,30 @@
|
|||
/*REXX program to display a power set, items may be anything (no blanks)*/
|
||||
parse arg S /*let user specify the set. */
|
||||
if S='' then S='one two three four' /*None specified? Use default*/
|
||||
N=words(S) /*number of items in the list.*/
|
||||
ps='{}' /*start with a null power set.*/
|
||||
do chunk=1 for N /*traipse through the items. */
|
||||
ps=ps combN(N,chunk) /*N items, a CHUNK at a time. */
|
||||
/*REXX pgm displays a power set, items may be anything (but can't have blanks)*/
|
||||
parse arg S /*allow the user specify optional set. */
|
||||
if S='' then S='one two three four' /*None specified? Then use the default*/
|
||||
N=words(S) /*the number of items in the list (set)*/
|
||||
@='{}' /*start process with a null power set. */
|
||||
do chunk=1 for N /*traipse through the items in the set.*/
|
||||
@=@ combN(N,chunk) /*take N items, a CHUNK at a time. */
|
||||
end /*chunk*/
|
||||
w=words(ps)
|
||||
do k=1 for w /*show combinations, one/line.*/
|
||||
say right(k,length(w)) word(ps,k)
|
||||
w=length(2**N) /*the number of items in the power set.*/
|
||||
do k=1 for words(@) /* [↓] show combinations, one per line*/
|
||||
say right(k,w) word(@,k) /*display a single combination to term.*/
|
||||
end /*k*/
|
||||
exit /*stick a fork in it, we done.*/
|
||||
/*─────────────────────────────────────$COMBN subroutine────────────────*/
|
||||
combN: procedure expose $ S; parse arg x,y; $=
|
||||
!.=0; base=x+1; bbase=base-y; ym=y-1; do p=1 for y; !.p=p; end
|
||||
do j=1; L=
|
||||
do d=1 for y; _=!.d; L=L','word(S,_); end
|
||||
$=$ '{'strip(L,'L',",")'}'
|
||||
!.y=!.y+1; if !.y==base then if .combU(ym) then leave
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
combN: procedure expose S; parse arg x,y; base=x+1; bbase=base-y; !.=0
|
||||
do p=1 for y; !.p=p; end /*p*/
|
||||
$=
|
||||
do j=1; L=
|
||||
do d=1 for y; L=L','word(S,!.d)
|
||||
end /*d*/
|
||||
$=$ '{'strip(L,'L',",")'}'
|
||||
!.y=!.y+1; if !.y==base then if .combU(y-1) then leave
|
||||
end /*j*/
|
||||
return strip($) /*return with partial powerset*/
|
||||
|
||||
.combU: procedure expose !. y bbase; parse arg d; if d==0 then return 1
|
||||
p=!.d; do u=d to y; !.u=p+1
|
||||
if !.u==bbase+u then return .combU(u-1)
|
||||
p=!.u
|
||||
end /*u*/
|
||||
return strip($) /*return with a partial powerset chunk.*/
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
.combU: procedure expose !. y bbase; parse arg d; if d==0 then return 1; p=!.d
|
||||
do u=d to y; !.u=p+1; if !.u==bbase+u then return .combU(u-1)
|
||||
p=!.u
|
||||
end /*u*/
|
||||
return 0
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# Based on http://johncarrino.net/blog/2006/08/11/powerset-in-ruby/
|
||||
# See the link if you want a shorter version.
|
||||
This was intended to show the reader how the method works.
|
||||
# This was intended to show the reader how the method works.
|
||||
class Array
|
||||
# Adds a power_set method to every array, i.e.: [1, 2].power_set
|
||||
def power_set
|
||||
|
|
|
|||
7
Task/Power-set/SETL/power-set.setl
Normal file
7
Task/Power-set/SETL/power-set.setl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
Pfour := pow({1, 2, 3, 4});
|
||||
Pempty := pow({});
|
||||
PPempty := pow(Pempty);
|
||||
|
||||
print(Pfour);
|
||||
print(Pempty);
|
||||
print(PPempty);
|
||||
|
|
@ -1,4 +1,2 @@
|
|||
(defun power-set (s)
|
||||
(reduce-right
|
||||
(op append (mapcar (op cons @@1) @2) @2)
|
||||
s '(())))
|
||||
(mappend* (op comb s) (range 0 (length s))))
|
||||
|
|
|
|||
|
|
@ -1,7 +1,5 @@
|
|||
@(do (defun power-set (s)
|
||||
(reduce-right
|
||||
(op append (mapcar (op cons @@1) @2) @2)
|
||||
s '(()))))
|
||||
(mappend* (op comb s) (range 0 (length s)))))
|
||||
@(bind pset @(power-set *args*))
|
||||
@(output)
|
||||
@ (repeat)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
@(do (defun power-set (s)
|
||||
(reduce-right
|
||||
(op append (mapcar (op cons @@1) @2) @2)
|
||||
s '(())))
|
||||
(mappend* (op comb s) (range 0 (length s))))
|
||||
(prinl (power-set "abc"))
|
||||
(prinl (power-set "b"))
|
||||
(prinl (power-set ""))
|
||||
(prinl (power-set #(1 2 3))))
|
||||
|
|
|
|||
33
Task/Power-set/VBScript/power-set.vb
Normal file
33
Task/Power-set/VBScript/power-set.vb
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
Function Dec2Bin(n)
|
||||
q = n
|
||||
Dec2Bin = ""
|
||||
Do Until q = 0
|
||||
Dec2Bin = CStr(q Mod 2) & Dec2Bin
|
||||
q = Int(q / 2)
|
||||
Loop
|
||||
Dec2Bin = Right("00000" & Dec2Bin,6)
|
||||
End Function
|
||||
|
||||
Function PowerSet(s)
|
||||
arrS = Split(s,",")
|
||||
PowerSet = "{"
|
||||
For i = 0 To 2^(UBound(arrS)+1)-1
|
||||
If i = 0 Then
|
||||
PowerSet = PowerSet & "{},"
|
||||
Else
|
||||
binS = Dec2Bin(i)
|
||||
PowerSet = PowerSet & "{"
|
||||
c = 0
|
||||
For j = Len(binS) To 1 Step -1
|
||||
If CInt(Mid(binS,j,1)) = 1 Then
|
||||
PowerSet = PowerSet & arrS(c) & ","
|
||||
End If
|
||||
c = c + 1
|
||||
Next
|
||||
PowerSet = Mid(PowerSet,1,Len(PowerSet)-1) & "},"
|
||||
End If
|
||||
Next
|
||||
PowerSet = Mid(PowerSet,1,Len(PowerSet)-1) & "}"
|
||||
End Function
|
||||
|
||||
WScript.StdOut.Write PowerSet("1,2,3,4")
|
||||
Loading…
Add table
Add a link
Reference in a new issue