tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
6
Task/Power-set/0DESCRIPTION
Normal file
6
Task/Power-set/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
A [[set]] is a collection (container) of certain values, without any particular order, and no repeated values. It corresponds with a finite set in mathematics. A set can be implemented as an associative array (partial mapping) in which the value of each key-value pair is ignored.
|
||||
|
||||
Given a set S, the [[wp:Power_set|power set]] (or powerset) of S, written P(S), or 2<sup>S</sup>, is the set of all subsets of S.<br>
|
||||
'''Task : ''' By using a library or build-in set type, or defining a set type with necessary operations, write a function with a set S as input that yields a power set 2<sup>S</sup> of S.
|
||||
|
||||
For example, the power set of {1,2,3,4} is {{}, {1}, {2}, {1,2}, {3}, {1,3}, {2,3}, {1,2,3}, {4}, {1,4}, {2,4}, {1,2,4}, {3,4}, {1,3,4}, {2,3,4}, {1,2,3,4}}.
|
||||
2
Task/Power-set/1META.yaml
Normal file
2
Task/Power-set/1META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
note: Discrete math
|
||||
27
Task/Power-set/ALGOL-68/power-set.alg
Normal file
27
Task/Power-set/ALGOL-68/power-set.alg
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
MODE MEMBER = INT;
|
||||
|
||||
PROC power set = ([]MEMBER s)[][]MEMBER:(
|
||||
[2**UPB s]FLEX[1:0]MEMBER r;
|
||||
INT upb r := 0;
|
||||
r[upb r +:= 1] := []MEMBER(());
|
||||
FOR i TO UPB s DO
|
||||
MEMBER e = s[i];
|
||||
FOR j TO upb r DO
|
||||
[UPB r[j] + 1]MEMBER x;
|
||||
x[:UPB x-1] := r[j];
|
||||
x[UPB x] := e; # append to the end of x #
|
||||
r[upb r +:= 1] := x # append to end of r #
|
||||
OD
|
||||
OD;
|
||||
r[upb r] := s;
|
||||
r
|
||||
);
|
||||
# Example: #
|
||||
test:(
|
||||
[][]MEMBER set = power set((1, 2, 4));
|
||||
FOR member TO UPB set DO
|
||||
INT upb = UPB set[member];
|
||||
FORMAT repr set = $"("f( upb=0 | $$ | $n(upb-1)(d", ")d$ )");"$;
|
||||
printf(($"set["d"] = "$,member, repr set, set[member],$l$))
|
||||
OD
|
||||
)
|
||||
84
Task/Power-set/Ada/power-set.ada
Normal file
84
Task/Power-set/Ada/power-set.ada
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure Power_Set is
|
||||
type Universe is (A,B,C,D,E);
|
||||
|
||||
-- The type Set are subsets of Universe
|
||||
type Set is array (Universe) of Boolean;
|
||||
Empty : constant Set := (others => False);
|
||||
function Cardinality (X : Set) return Natural is
|
||||
N : Natural := 0;
|
||||
begin
|
||||
for I in X'Range loop
|
||||
if X (I) then
|
||||
N := N + 1;
|
||||
end if;
|
||||
end loop;
|
||||
return N;
|
||||
end Cardinality;
|
||||
function Element (X : Set; Position : Positive) return Universe is
|
||||
N : Natural := 0;
|
||||
begin
|
||||
for I in X'Range loop
|
||||
if X (I) then
|
||||
N := N + 1;
|
||||
if N = Position then
|
||||
return I;
|
||||
end if;
|
||||
end if;
|
||||
end loop;
|
||||
raise Constraint_Error;
|
||||
end Element;
|
||||
procedure Put (X : Set) is
|
||||
Empty : Boolean := True;
|
||||
begin
|
||||
for I in X'Range loop
|
||||
if X (I) then
|
||||
if Empty then
|
||||
Empty := False;
|
||||
Put (Universe'Image (I));
|
||||
else
|
||||
Put ("," & Universe'Image (I));
|
||||
end if;
|
||||
end if;
|
||||
end loop;
|
||||
if Empty then
|
||||
Put ("empty");
|
||||
end if;
|
||||
end Put;
|
||||
|
||||
-- Set_Of_Set are sets of subsets of Universe
|
||||
type Set_Of_Sets is array (Positive range <>) of Set;
|
||||
|
||||
function Power (X : Set) return Set_Of_Sets is
|
||||
Length : constant Natural := Cardinality (X);
|
||||
Index : array (1..Length) of Integer := (others => 0);
|
||||
Result : Set_Of_Sets (1..2**Length) := (others => Empty);
|
||||
begin
|
||||
for N in Result'Range loop
|
||||
for I in 1..Length loop -- Index determines the sample N
|
||||
exit when Index (I) = 0;
|
||||
Result (N) (Element (X, Index (I))) := True;
|
||||
end loop;
|
||||
Next : for I in 1..Length loop -- Computing the index of the following sample
|
||||
if Index (I) < Length then
|
||||
Index (I) := Index (I) + 1;
|
||||
if I = 1 or else Index (I - 1) > Index (I) then
|
||||
for J in reverse 2..I loop
|
||||
Index (J - 1) := Index (J) + 1;
|
||||
end loop;
|
||||
exit Next;
|
||||
end if;
|
||||
end if;
|
||||
end loop Next;
|
||||
end loop;
|
||||
return Result;
|
||||
end Power;
|
||||
|
||||
P : Set_Of_Sets := Power ((A|C|E => True, others => False));
|
||||
begin
|
||||
for I in P'Range loop
|
||||
New_Line;
|
||||
Put (P (I));
|
||||
end loop;
|
||||
end Power_Set;
|
||||
11
Task/Power-set/AutoHotkey/power-set.ahk
Normal file
11
Task/Power-set/AutoHotkey/power-set.ahk
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
a = 1,a,-- ; elements separated by commas
|
||||
StringSplit a, a, `, ; a0 = #elements, a1,a2,... = elements of the set
|
||||
|
||||
t = {
|
||||
Loop % (1<<a0) { ; generate all 0-1 sequences
|
||||
x := A_Index-1
|
||||
Loop % a0
|
||||
t .= (x>>A_Index-1) & 1 ? a%A_Index% "," : ""
|
||||
t .= "}`n{" ; new subsets in new lines
|
||||
}
|
||||
MsgBox % RegExReplace(SubStr(t,1,StrLen(t)-1),",}","}")
|
||||
17
Task/Power-set/BBC-BASIC/power-set.bbc
Normal file
17
Task/Power-set/BBC-BASIC/power-set.bbc
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
DIM list$(3) : list$() = "1", "2", "3", "4"
|
||||
PRINT FNpowerset(list$())
|
||||
END
|
||||
|
||||
DEF FNpowerset(list$())
|
||||
IF DIM(list$(),1) > 31 ERROR 100, "Set too large to represent as integer"
|
||||
LOCAL i%, j%, s$
|
||||
s$ = "{"
|
||||
FOR i% = 0 TO (2 << DIM(list$(),1)) - 1
|
||||
s$ += "{"
|
||||
FOR j% = 0 TO DIM(list$(),1)
|
||||
IF i% AND (1 << j%) s$ += list$(j%) + ","
|
||||
NEXT
|
||||
IF RIGHT$(s$) = "," s$ = LEFT$(s$)
|
||||
s$ += "},"
|
||||
NEXT i%
|
||||
= LEFT$(s$) + "}"
|
||||
2
Task/Power-set/Burlesque/power-set.blq
Normal file
2
Task/Power-set/Burlesque/power-set.blq
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
blsq ) {1 2 3 4}R@
|
||||
{{} {1} {2} {1 2} {3} {1 3} {2 3} {1 2 3} {4} {1 4} {2 4} {1 2 4} {3 4} {1 3 4} {2 3 4} {1 2 3 4}}
|
||||
78
Task/Power-set/C++/power-set-1.cpp
Normal file
78
Task/Power-set/C++/power-set-1.cpp
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
#include <iostream>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
#include <iterator>
|
||||
#include <algorithm>
|
||||
typedef std::set<int> set_type;
|
||||
typedef std::set<set_type> powerset_type;
|
||||
|
||||
powerset_type powerset(set_type const& set)
|
||||
{
|
||||
typedef set_type::const_iterator set_iter;
|
||||
typedef std::vector<set_iter> vec;
|
||||
typedef vec::iterator vec_iter;
|
||||
|
||||
struct local
|
||||
{
|
||||
static int dereference(set_iter v) { return *v; }
|
||||
};
|
||||
|
||||
powerset_type result;
|
||||
|
||||
vec elements;
|
||||
do
|
||||
{
|
||||
set_type tmp;
|
||||
std::transform(elements.begin(), elements.end(),
|
||||
std::inserter(tmp, tmp.end()),
|
||||
local::dereference);
|
||||
result.insert(tmp);
|
||||
if (!elements.empty() && ++elements.back() == set.end())
|
||||
{
|
||||
elements.pop_back();
|
||||
}
|
||||
else
|
||||
{
|
||||
set_iter iter;
|
||||
if (elements.empty())
|
||||
{
|
||||
iter = set.begin();
|
||||
}
|
||||
else
|
||||
{
|
||||
iter = elements.back();
|
||||
++iter;
|
||||
}
|
||||
for (; iter != set.end(); ++iter)
|
||||
{
|
||||
elements.push_back(iter);
|
||||
}
|
||||
}
|
||||
} while (!elements.empty());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int values[4] = { 2, 3, 5, 7 };
|
||||
set_type test_set(values, values+4);
|
||||
|
||||
powerset_type test_powerset = powerset(test_set);
|
||||
|
||||
for (powerset_type::iterator iter = test_powerset.begin();
|
||||
iter != test_powerset.end();
|
||||
++iter)
|
||||
{
|
||||
std::cout << "{ ";
|
||||
char const* prefix = "";
|
||||
for (set_type::iterator iter2 = iter->begin();
|
||||
iter2 != iter->end();
|
||||
++iter2)
|
||||
{
|
||||
std::cout << prefix << *iter2;
|
||||
prefix = ", ";
|
||||
}
|
||||
std::cout << " }\n";
|
||||
}
|
||||
}
|
||||
25
Task/Power-set/C++/power-set-2.cpp
Normal file
25
Task/Power-set/C++/power-set-2.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());
|
||||
}
|
||||
28
Task/Power-set/C/power-set-1.c
Normal file
28
Task/Power-set/C/power-set-1.c
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static void powerset(int argc, char** argv)
|
||||
{
|
||||
unsigned int i, j, bits, i_max = 1U << argc;
|
||||
|
||||
if (argc >= sizeof(i) * CHAR_BIT) {
|
||||
fprintf(stderr, "Error: set too large\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (i = 0; i < i_max ; ++i) {
|
||||
printf("{");
|
||||
for (bits = i, j = 0; bits; bits >>= 1, ++j) {
|
||||
if (bits & 1)
|
||||
printf(bits > 1 ? "%s, " : "%s", argv[j]);
|
||||
}
|
||||
printf("}\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
powerset(argc - 1, argv + 1);
|
||||
return 0;
|
||||
}
|
||||
17
Task/Power-set/C/power-set-2.c
Normal file
17
Task/Power-set/C/power-set-2.c
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
% ./a.out 1 2 3 4
|
||||
{}
|
||||
{1}
|
||||
{2}
|
||||
{1, 2}
|
||||
{3}
|
||||
{1, 3}
|
||||
{2, 3}
|
||||
{1, 2, 3}
|
||||
{4}
|
||||
{1, 4}
|
||||
{2, 4}
|
||||
{1, 2, 4}
|
||||
{3, 4}
|
||||
{1, 3, 4}
|
||||
{2, 3, 4}
|
||||
{1, 2, 3, 4}
|
||||
6
Task/Power-set/Clojure/power-set.clj
Normal file
6
Task/Power-set/Clojure/power-set.clj
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
(use '[clojure.contrib.combinatorics :only [subsets] ])
|
||||
|
||||
(def S #{1 2 3 4})
|
||||
|
||||
user> (subsets S)
|
||||
(() (1) (2) (3) (4) (1 2) (1 3) (1 4) (2 3) (2 4) (3 4) (1 2 3) (1 2 4) (1 3 4) (2 3 4) (1 2 3 4))
|
||||
28
Task/Power-set/CoffeeScript/power-set-1.coffeescript
Normal file
28
Task/Power-set/CoffeeScript/power-set-1.coffeescript
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
print_power_set = (arr) ->
|
||||
console.log "POWER SET of #{arr}"
|
||||
for subset in power_set(arr)
|
||||
console.log subset
|
||||
|
||||
power_set = (arr) ->
|
||||
result = []
|
||||
binary = (false for elem in arr)
|
||||
n = arr.length
|
||||
while binary.length <= n
|
||||
result.push bin_to_arr binary, arr
|
||||
i = 0
|
||||
while true
|
||||
if binary[i]
|
||||
binary[i] = false
|
||||
i += 1
|
||||
else
|
||||
binary[i] = true
|
||||
break
|
||||
binary[i] = true
|
||||
result
|
||||
|
||||
bin_to_arr = (binary, arr) ->
|
||||
(arr[i] for i of binary when binary[arr.length - i - 1])
|
||||
|
||||
print_power_set []
|
||||
print_power_set [4, 2, 1]
|
||||
print_power_set ['dog', 'c', 'b', 'a']
|
||||
29
Task/Power-set/CoffeeScript/power-set-2.coffeescript
Normal file
29
Task/Power-set/CoffeeScript/power-set-2.coffeescript
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
> coffee power_set.coffee
|
||||
POWER SET of
|
||||
[]
|
||||
POWER SET of 4,2,1
|
||||
[]
|
||||
[ 1 ]
|
||||
[ 2 ]
|
||||
[ 2, 1 ]
|
||||
[ 4 ]
|
||||
[ 4, 1 ]
|
||||
[ 4, 2 ]
|
||||
[ 4, 2, 1 ]
|
||||
POWER SET of dog,c,b,a
|
||||
[]
|
||||
[ 'a' ]
|
||||
[ 'b' ]
|
||||
[ 'b', 'a' ]
|
||||
[ 'c' ]
|
||||
[ 'c', 'a' ]
|
||||
[ 'c', 'b' ]
|
||||
[ 'c', 'b', 'a' ]
|
||||
[ 'dog' ]
|
||||
[ 'dog', 'a' ]
|
||||
[ 'dog', 'b' ]
|
||||
[ 'dog', 'b', 'a' ]
|
||||
[ 'dog', 'c' ]
|
||||
[ 'dog', 'c', 'a' ]
|
||||
[ 'dog', 'c', 'b' ]
|
||||
[ 'dog', 'c', 'b', 'a' ]
|
||||
18
Task/Power-set/ColdFusion/power-set.cfm
Normal file
18
Task/Power-set/ColdFusion/power-set.cfm
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
public array function powerset(required array data)
|
||||
{
|
||||
var ps = [""];
|
||||
var d = arguments.data;
|
||||
var lenData = arrayLen(d);
|
||||
var lenPS = 0;
|
||||
for (var i=1; i LTE lenData; i++)
|
||||
{
|
||||
lenPS = arrayLen(ps);
|
||||
for (var j = 1; j LTE lenPS; j++)
|
||||
{
|
||||
arrayAppend(ps, listAppend(ps[j], d[i]));
|
||||
}
|
||||
}
|
||||
return ps;
|
||||
}
|
||||
|
||||
var res = powerset([1,2,3,4]);
|
||||
8
Task/Power-set/Common-Lisp/power-set-1.lisp
Normal file
8
Task/Power-set/Common-Lisp/power-set-1.lisp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
(defun power-set (s)
|
||||
(reduce #'(lambda (item ps)
|
||||
(append (mapcar #'(lambda (e) (cons item e))
|
||||
ps)
|
||||
ps))
|
||||
s
|
||||
:from-end t
|
||||
:initial-value '(())))
|
||||
6
Task/Power-set/Common-Lisp/power-set-2.lisp
Normal file
6
Task/Power-set/Common-Lisp/power-set-2.lisp
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
(defun powerset (l)
|
||||
(if (null l)
|
||||
(list nil)
|
||||
(let ((prev (powerset (cdr l))))
|
||||
(append (mapcar #'(lambda (elt) (cons (car l) elt)) prev)
|
||||
prev))))
|
||||
3
Task/Power-set/Common-Lisp/power-set-3.lisp
Normal file
3
Task/Power-set/Common-Lisp/power-set-3.lisp
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
(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)))
|
||||
16
Task/Power-set/D/power-set.d
Normal file
16
Task/Power-set/D/power-set.d
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import std.stdio;
|
||||
|
||||
T[][] powerSet(T)(in T[] s) pure nothrow {
|
||||
auto r = new typeof(return)(1, 0);
|
||||
foreach (e; s) {
|
||||
typeof(return) rs;
|
||||
foreach (x; r)
|
||||
rs ~= x ~ [e];
|
||||
r ~= rs;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
void main() {
|
||||
writeln(powerSet([1, 2, 3]));
|
||||
}
|
||||
9
Task/Power-set/E/power-set.e
Normal file
9
Task/Power-set/E/power-set.e
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
pragma.enable("accumulator")
|
||||
|
||||
def powerset(s) {
|
||||
return accum [].asSet() for k in 0..!2**s.size() {
|
||||
_.with(accum [].asSet() for i ? ((2**i & k) > 0) => elem in s {
|
||||
_.with(elem)
|
||||
})
|
||||
}
|
||||
}
|
||||
5
Task/Power-set/Erlang/power-set-1.erl
Normal file
5
Task/Power-set/Erlang/power-set-1.erl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
powerset(Lst) ->
|
||||
N = length(Lst),
|
||||
Max = trunc(math:pow(2,N)),
|
||||
[[lists:nth(Pos+1,Lst) || Pos <- lists:seq(0,N-1), I band (1 bsl Pos) =/= 0]
|
||||
|| I <- lists:seq(0,Max-1)].
|
||||
3
Task/Power-set/Erlang/power-set-2.erl
Normal file
3
Task/Power-set/Erlang/power-set-2.erl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
powerset([]) -> [[]];
|
||||
powerset([H|T]) -> PT = powerset(T),
|
||||
[ [H|X] || X <- PT ] ++ PT.
|
||||
6
Task/Power-set/Erlang/power-set-3.erl
Normal file
6
Task/Power-set/Erlang/power-set-3.erl
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
powerset([]) -> [[]];
|
||||
powerset([H|T]) -> PT = powerset(T),
|
||||
powerset(H, PT, PT).
|
||||
|
||||
powerset(_, [], Acc) -> Acc;
|
||||
powerset(X, [H|T], Acc) -> powerset(X, T, [[X|H]|Acc]).
|
||||
5
Task/Power-set/Factor/power-set-1.factor
Normal file
5
Task/Power-set/Factor/power-set-1.factor
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
USING: kernel prettyprint sequences arrays sets hash-sets ;
|
||||
IN: powerset
|
||||
|
||||
: add ( set elt -- newset ) 1array <hash-set> union ;
|
||||
: powerset ( set -- newset ) members { HS{ } } [ dupd [ add ] curry map append ] reduce <hash-set> ;
|
||||
19
Task/Power-set/Factor/power-set-2.factor
Normal file
19
Task/Power-set/Factor/power-set-2.factor
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
( scratchpad ) HS{ 1 2 3 4 } powerset .
|
||||
HS{
|
||||
HS{ 1 2 3 4 }
|
||||
HS{ 1 2 }
|
||||
HS{ 1 3 }
|
||||
HS{ 2 3 }
|
||||
HS{ 1 2 3 }
|
||||
HS{ 1 4 }
|
||||
HS{ 2 4 }
|
||||
HS{ }
|
||||
HS{ 1 }
|
||||
HS{ 2 }
|
||||
HS{ 3 }
|
||||
HS{ 4 }
|
||||
HS{ 1 2 4 }
|
||||
HS{ 3 4 }
|
||||
HS{ 1 3 4 }
|
||||
HS{ 2 3 4 }
|
||||
}
|
||||
8
Task/Power-set/Forth/power-set.fth
Normal file
8
Task/Power-set/Forth/power-set.fth
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
: ?print dup 1 and if over args type space then ;
|
||||
: .set begin dup while ?print >r 1+ r> 1 rshift repeat drop drop ;
|
||||
: .powerset 0 do ." ( " 1 i .set ." )" cr loop ;
|
||||
: check-none dup 2 < abort" Usage: powerset [val] .. [val]" ;
|
||||
: check-size dup /cell 8 [*] >= abort" Set too large" ;
|
||||
: powerset 1 argn check-none check-size 1- lshift .powerset ;
|
||||
|
||||
powerset
|
||||
2
Task/Power-set/Frink/power-set.frink
Normal file
2
Task/Power-set/Frink/power-set.frink
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
a = new set[1,2,3,4]
|
||||
a.subsets[]
|
||||
8
Task/Power-set/GAP/power-set.gap
Normal file
8
Task/Power-set/GAP/power-set.gap
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
# Built-in
|
||||
Combinations([1, 2, 3]);
|
||||
# [ [ ], [ 1 ], [ 1, 2 ], [ 1, 2, 3 ], [ 1, 3 ], [ 2 ], [ 2, 3 ], [ 3 ] ]
|
||||
|
||||
# Note that it handles duplicates
|
||||
Combinations([1, 2, 3, 1]);
|
||||
# [ [ ], [ 1 ], [ 1, 1 ], [ 1, 1, 2 ], [ 1, 1, 2, 3 ], [ 1, 1, 3 ], [ 1, 2 ], [ 1, 2, 3 ], [ 1, 3 ],
|
||||
# [ 2 ], [ 2, 3 ], [ 3 ] ]
|
||||
106
Task/Power-set/Go/power-set.go
Normal file
106
Task/Power-set/Go/power-set.go
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// types needed to implement general purpose sets are element and set
|
||||
|
||||
// element is an interface, allowing different kinds of elements to be
|
||||
// implemented and stored in sets.
|
||||
type element interface {
|
||||
// an element must be disinguishable from other elements to satisfy
|
||||
// the mathematical definition of a set. a.eq(b) must give the same
|
||||
// result as b.eq(a).
|
||||
eq(element) bool
|
||||
// String result is used only for printable output. Given a, b where
|
||||
// a.eq(b), it is not required that a.String() == b.String().
|
||||
String() string
|
||||
}
|
||||
|
||||
// integer type satisfying element interface
|
||||
type intEle int
|
||||
|
||||
func (i intEle) eq(e element) bool {
|
||||
if j, ok := e.(intEle); ok {
|
||||
return i == j
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (i intEle) String() string {
|
||||
return strconv.Itoa(int(i))
|
||||
}
|
||||
|
||||
// set type implemented as a simple list. methods will be added to
|
||||
// make it satisfy the element interface, allowing sets of sets.
|
||||
type set []element
|
||||
|
||||
// uniqueness of elements can be ensured by using add method
|
||||
func (s *set) add(e element) {
|
||||
for _, ex := range *s {
|
||||
if e.eq(ex) {
|
||||
return
|
||||
}
|
||||
}
|
||||
*s = append(*s, e)
|
||||
}
|
||||
|
||||
// method to satify element interface
|
||||
func (s set) eq(e element) bool {
|
||||
t, ok := e.(set)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if len(s) != len(t) {
|
||||
return false
|
||||
}
|
||||
sLoop:
|
||||
for _, se := range s {
|
||||
for _, te := range t {
|
||||
if se.eq(te) {
|
||||
continue sLoop
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// method to satify element interface
|
||||
func (s set) String() string {
|
||||
r := "{"
|
||||
for _, e := range s {
|
||||
if len(r) > 1 {
|
||||
r += " "
|
||||
}
|
||||
r += fmt.Sprint(e)
|
||||
}
|
||||
return r + "}"
|
||||
}
|
||||
|
||||
// method required for task
|
||||
func (s set) powerSet() set {
|
||||
r := set{set{}}
|
||||
for _, es := range s {
|
||||
var u set
|
||||
for _, er := range r {
|
||||
u = append(u, append(er.(set), es))
|
||||
}
|
||||
r = append(r, u...)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func main() {
|
||||
var s set
|
||||
for _, i := range []int{1, 2, 2, 3, 4, 4, 4} {
|
||||
s.add(intEle(i))
|
||||
}
|
||||
fmt.Println(s)
|
||||
fmt.Println("length =", len(s))
|
||||
ps := s.powerSet()
|
||||
fmt.Println(ps)
|
||||
fmt.Println("length =", len(ps))
|
||||
}
|
||||
14
Task/Power-set/Groovy/power-set-1.groovy
Normal file
14
Task/Power-set/Groovy/power-set-1.groovy
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
def comb
|
||||
comb = { m, List list ->
|
||||
def n = list.size()
|
||||
m == 0 ?
|
||||
[[]] :
|
||||
(0..(n-m)).inject([]) { newlist, k ->
|
||||
def sublist = (k+1 == n) ? [] : list[(k+1)..<n]
|
||||
newlist += comb(m-1, sublist).collect { [list[k]] + it }
|
||||
}
|
||||
}
|
||||
|
||||
def powerSet = { set ->
|
||||
(0..(set.size())).inject([]){ list, i -> list + comb(i,set as List)}.collect { it as LinkedHashSet } as LinkedHashSet
|
||||
}
|
||||
3
Task/Power-set/Groovy/power-set-2.groovy
Normal file
3
Task/Power-set/Groovy/power-set-2.groovy
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
def vocalists = [ "C", "S", "N", "Y" ] as LinkedHashSet
|
||||
println "${vocalists}"
|
||||
println powerSet(vocalists)
|
||||
8
Task/Power-set/Haskell/power-set-1.hs
Normal file
8
Task/Power-set/Haskell/power-set-1.hs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import Data.Set
|
||||
import Control.Monad
|
||||
|
||||
powerset :: Ord a => Set a -> Set (Set a)
|
||||
powerset = fromList . fmap fromList . listPowerset . toList
|
||||
|
||||
listPowerset :: [a] -> [[a]]
|
||||
listPowerset = filterM (const [True, False])
|
||||
2
Task/Power-set/Haskell/power-set-2.hs
Normal file
2
Task/Power-set/Haskell/power-set-2.hs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
powerset [] = [[]]
|
||||
powerset (head:tail) = acc ++ map (head:) acc where acc = powerset tail
|
||||
1
Task/Power-set/Haskell/power-set-3.hs
Normal file
1
Task/Power-set/Haskell/power-set-3.hs
Normal file
|
|
@ -0,0 +1 @@
|
|||
powerset = foldr (\x acc -> acc ++ map (x:) acc) [[]]
|
||||
15
Task/Power-set/Haskell/power-set-4.hs
Normal file
15
Task/Power-set/Haskell/power-set-4.hs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import qualified Data.Set as Set
|
||||
type Set=Set.Set
|
||||
unionAll :: (Ord a) => Set (Set a) -> Set a
|
||||
unionAll = Set.fold Set.union Set.empty
|
||||
|
||||
--slift is the analogue of liftA2 for sets.
|
||||
slift :: (Ord a, Ord b, Ord c) => (a->b->c) -> Set a -> Set b -> Set c
|
||||
slift f s0 s1 = unionAll (Set.map (\e->Set.map (f e) s1) s0)
|
||||
|
||||
--a -> {{},{a}}
|
||||
makeSet :: (Ord a) => a -> Set (Set a)
|
||||
makeSet = (Set.insert Set.empty) . Set.singleton.Set.singleton
|
||||
|
||||
powerSet :: (Ord a) => Set a -> Set (Set a)
|
||||
powerSet = (Set.fold (slift Set.union) (Set.singleton Set.empty)) . Set.map makeSet
|
||||
2
Task/Power-set/Haskell/power-set-5.hs
Normal file
2
Task/Power-set/Haskell/power-set-5.hs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
Prelude Data.Set> powerSet fromList [1,2,3]
|
||||
fromList [fromList [], fromList [1], fromList [1,2], fromList [1,2,3], fromList [1,3], fromList [2], fromList [2,3], fromList [3]]
|
||||
14
Task/Power-set/Icon/power-set-1.icon
Normal file
14
Task/Power-set/Icon/power-set-1.icon
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
procedure power_set (s)
|
||||
result := set ()
|
||||
if *s = 0
|
||||
then insert (result, set ()) # empty set
|
||||
else {
|
||||
head := set(?s) # take a random element
|
||||
# and find powerset of remaining part of set
|
||||
tail_pset := power_set (x -- head)
|
||||
result ++:= tail_pset # add powerset of remainder to results
|
||||
every ps := !tail_pset do # and add head to each powerset from the remainder
|
||||
insert (result, ps ++ head)
|
||||
}
|
||||
return result
|
||||
end
|
||||
7
Task/Power-set/Icon/power-set-2.icon
Normal file
7
Task/Power-set/Icon/power-set-2.icon
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
procedure main ()
|
||||
every s := !power_set (set(1,2,3,4)) do { # requires '!' to generate items in the result set
|
||||
writes ("[ ")
|
||||
every writes (!s || " ")
|
||||
write ("]")
|
||||
}
|
||||
end
|
||||
19
Task/Power-set/Icon/power-set-3.icon
Normal file
19
Task/Power-set/Icon/power-set-3.icon
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
procedure power_set (s)
|
||||
if *s = 0
|
||||
then suspend set ()
|
||||
else {
|
||||
head := set(?s)
|
||||
every ps := power_set (s -- head) do {
|
||||
suspend ps
|
||||
suspend ps ++ head
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
procedure main ()
|
||||
every s := power_set (set(1,2,3,4)) do { # power_set's values are generated by 'every'
|
||||
writes ("[ ")
|
||||
every writes (!s || " ")
|
||||
write ("]")
|
||||
}
|
||||
end
|
||||
1
Task/Power-set/J/power-set-1.j
Normal file
1
Task/Power-set/J/power-set-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
ps =: #~ 2 #:@i.@^ #
|
||||
9
Task/Power-set/J/power-set-2.j
Normal file
9
Task/Power-set/J/power-set-2.j
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
ps 'ACE'
|
||||
|
||||
E
|
||||
C
|
||||
CE
|
||||
A
|
||||
AE
|
||||
AC
|
||||
ACE
|
||||
6
Task/Power-set/J/power-set-3.j
Normal file
6
Task/Power-set/J/power-set-3.j
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
~.1 2 3 2 1
|
||||
1 2 3
|
||||
#ps 1 2 3 2 1
|
||||
32
|
||||
#ps ~.1 2 3 2 1
|
||||
8
|
||||
25
Task/Power-set/Java/power-set-1.java
Normal file
25
Task/Power-set/Java/power-set-1.java
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
public static ArrayList<String> getpowerset(int a[],int n,ArrayList<String> ps)
|
||||
{
|
||||
if(n<0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if(n==0)
|
||||
{
|
||||
if(ps==null)
|
||||
ps=new ArrayList();
|
||||
ps.add(" ");
|
||||
return ps;
|
||||
}
|
||||
ps=getpowerset(a, n-1, ps);
|
||||
ArrayList<String> tmp=new ArrayList<String>();
|
||||
for(String s:ps)
|
||||
{
|
||||
if(s.equals(" "))
|
||||
tmp.add(""+a[n-1]);
|
||||
else
|
||||
tmp.add(s+a[n-1]);
|
||||
}
|
||||
ps.addAll(tmp);
|
||||
return ps;
|
||||
}
|
||||
23
Task/Power-set/Java/power-set-2.java
Normal file
23
Task/Power-set/Java/power-set-2.java
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
public static <T> List<List<T>> powerset(Collection<T> list) {
|
||||
List<List<T>> ps = new ArrayList<List<T>>();
|
||||
ps.add(new ArrayList<T>()); // add the empty set
|
||||
|
||||
// for every item in the original list
|
||||
for (T item : list) {
|
||||
List<List<T>> newPs = new ArrayList<List<T>>();
|
||||
|
||||
for (List<T> subset : ps) {
|
||||
// copy all of the current powerset's subsets
|
||||
newPs.add(subset);
|
||||
|
||||
// plus the subsets appended with the current item
|
||||
List<T> newSubset = new ArrayList<T>(subset);
|
||||
newSubset.add(item);
|
||||
newPs.add(newSubset);
|
||||
}
|
||||
|
||||
// powerset is now powerset of list.subList(0, list.indexOf(item)+1)
|
||||
ps = newPs;
|
||||
}
|
||||
return ps;
|
||||
}
|
||||
16
Task/Power-set/Java/power-set-3.java
Normal file
16
Task/Power-set/Java/power-set-3.java
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
public static <T extends Comparable<? super T>> LinkedList<LinkedList<T>> BinPowSet(
|
||||
LinkedList<T> A){
|
||||
LinkedList<LinkedList<T>> ans= new LinkedList<LinkedList<T>>();
|
||||
int ansSize = (int)Math.pow(2, A.size());
|
||||
for(Integer i= 0;i< ansSize;++i){
|
||||
String bin= Integer.toString(i, 2); //convert to binary
|
||||
while(bin.length() < A.size())bin = "0" + bin; //pad with 0's
|
||||
LinkedList<T> thisComb = new LinkedList<T>(); //place to put one combination
|
||||
for(int j= 0;j< A.size();++j){
|
||||
if(bin.charAt(j) == '1')thisComb.add(A.get(j));
|
||||
}
|
||||
Collections.sort(thisComb); //sort it for easy checking
|
||||
ans.add(thisComb); //put this set in the answer list
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
14
Task/Power-set/JavaScript/power-set.js
Normal file
14
Task/Power-set/JavaScript/power-set.js
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
function powerset(ary) {
|
||||
var ps = [[]];
|
||||
for (var i=0; i < ary.length; i++) {
|
||||
for (var j = 0, len = ps.length; j < len; j++) {
|
||||
ps.push(ps[j].concat(ary[i]));
|
||||
}
|
||||
}
|
||||
return ps;
|
||||
}
|
||||
|
||||
var res = powerset([1,2,3,4]);
|
||||
|
||||
load('json2.js');
|
||||
print(JSON.stringify(res));
|
||||
1
Task/Power-set/K/power-set-1.k
Normal file
1
Task/Power-set/K/power-set-1.k
Normal file
|
|
@ -0,0 +1 @@
|
|||
ps:{x@&:'+2_vs!_2^#x}
|
||||
9
Task/Power-set/K/power-set-2.k
Normal file
9
Task/Power-set/K/power-set-2.k
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
ps "ABC"
|
||||
(""
|
||||
,"C"
|
||||
,"B"
|
||||
"BC"
|
||||
,"A"
|
||||
"AC"
|
||||
"AB"
|
||||
"ABC")
|
||||
8
Task/Power-set/Logo/power-set.logo
Normal file
8
Task/Power-set/Logo/power-set.logo
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
to powerset :set
|
||||
if empty? :set [output [[]]]
|
||||
localmake "rest powerset butfirst :set
|
||||
output sentence map [sentence first :set ?] :rest :rest
|
||||
end
|
||||
|
||||
show powerset [1 2 3]
|
||||
[[1 2 3] [1 2] [1 3] [1] [2 3] [2] [3] []]
|
||||
25
Task/Power-set/Logtalk/power-set-1.logtalk
Normal file
25
Task/Power-set/Logtalk/power-set-1.logtalk
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
:- object(set).
|
||||
|
||||
:- public(powerset/2).
|
||||
|
||||
powerset(Set, PowerSet) :-
|
||||
reverse(Set, RSet),
|
||||
powerset_1(RSet, [[]], PowerSet).
|
||||
|
||||
powerset_1([], PowerSet, PowerSet).
|
||||
powerset_1([X| Xs], Yss0, Yss) :-
|
||||
powerset_2(Yss0, X, Yss1),
|
||||
powerset_1(Xs, Yss1, Yss).
|
||||
|
||||
powerset_2([], _, []).
|
||||
powerset_2([Zs| Zss], X, [Zs, [X| Zs]| Yss]) :-
|
||||
powerset_2(Zss, X, Yss).
|
||||
|
||||
reverse(List, Reversed) :-
|
||||
reverse(List, [], Reversed).
|
||||
|
||||
reverse([], Reversed, Reversed).
|
||||
reverse([Head| Tail], List, Reversed) :-
|
||||
reverse(Tail, [Head| List], Reversed).
|
||||
|
||||
:- end_object.
|
||||
4
Task/Power-set/Logtalk/power-set-2.logtalk
Normal file
4
Task/Power-set/Logtalk/power-set-2.logtalk
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
| ?- set::powerset([1, 2, 3, 4], PowerSet).
|
||||
|
||||
PowerSet = [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3],[4],[1,4],[2,4],[1,2,4],[3,4],[1,3,4],[2,3,4],[1,2,3,4]]
|
||||
yes
|
||||
33
Task/Power-set/Lua/power-set.lua
Normal file
33
Task/Power-set/Lua/power-set.lua
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
--returns the powerset of s, out of order.
|
||||
function powerset(s, start)
|
||||
start = start or 1
|
||||
if(start > #s) then return {{}} end
|
||||
local ret = powerset(s, start + 1)
|
||||
for i = 1, #ret do
|
||||
ret[#ret + 1] = {s[start], unpack(ret[i])}
|
||||
end
|
||||
return ret
|
||||
end
|
||||
|
||||
--non-recurse implementation
|
||||
function powerset(s)
|
||||
local t = {{}}
|
||||
for i = 1, #s do
|
||||
for j = 1, #t do
|
||||
t[#t+1] = {s[i],unpack(t[j])}
|
||||
end
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
--alternative, copied from the Python implementation
|
||||
function powerset2(s)
|
||||
local ret = {{}}
|
||||
for i = 1, #s do
|
||||
local k = #ret
|
||||
for j = 1, k do
|
||||
ret[k + j] = {s[i], unpack(ret[j])}
|
||||
end
|
||||
end
|
||||
return ret
|
||||
end
|
||||
22
Task/Power-set/M4/power-set.m4
Normal file
22
Task/Power-set/M4/power-set.m4
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
define(`for',
|
||||
`ifelse($#, 0, ``$0'',
|
||||
eval($2 <= $3), 1,
|
||||
`pushdef(`$1', `$2')$4`'popdef(
|
||||
`$1')$0(`$1', incr($2), $3, `$4')')')dnl
|
||||
define(`nth',
|
||||
`ifelse($1, 1, $2,
|
||||
`nth(decr($1), shift(shift($@)))')')dnl
|
||||
define(`range',
|
||||
`for(`x', eval($1 + 2), eval($2 + 2),
|
||||
`nth(x, $@)`'ifelse(x, eval($2+2), `', `,')')')dnl
|
||||
define(`powerpart',
|
||||
`{range(2, incr($1), $@)}`'ifelse(incr($1), $#, `',
|
||||
`for(`x', eval($1+2), $#,
|
||||
`,powerpart(incr($1), ifelse(
|
||||
eval(2 <= ($1 + 1)), 1,
|
||||
`range(2,incr($1), $@), ')`'nth(x, $@)`'ifelse(
|
||||
eval((x + 1) <= $#),1,`,range(incr(x), $#, $@)'))')')')dnl
|
||||
define(`powerset',
|
||||
`{powerpart(0, substr(`$1', 1, eval(len(`$1') - 2)))}')dnl
|
||||
dnl
|
||||
powerset(`{a,b,c}')
|
||||
18
Task/Power-set/MATLAB/power-set-1.m
Normal file
18
Task/Power-set/MATLAB/power-set-1.m
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
function pset = powerset(theSet)
|
||||
|
||||
pset = cell(size(theSet)); %Preallocate memory
|
||||
|
||||
%Generate all numbers from 0 to 2^(num elements of the set)-1
|
||||
for i = ( 0:(2^numel(theSet))-1 )
|
||||
|
||||
%Convert i into binary, convert each digit in binary to a boolean
|
||||
%and store that array of booleans
|
||||
indicies = logical(bitget( i,(1:numel(theSet)) ));
|
||||
|
||||
%Use the array of booleans to extract the members of the original
|
||||
%set, and store the set containing these members in the powerset
|
||||
pset(i+1) = {theSet(indicies)};
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
5
Task/Power-set/MATLAB/power-set-2.m
Normal file
5
Task/Power-set/MATLAB/power-set-2.m
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
powerset({{}})
|
||||
|
||||
ans =
|
||||
|
||||
{} {1x1 cell} %This is the same as { {},{{}} }
|
||||
5
Task/Power-set/MATLAB/power-set-3.m
Normal file
5
Task/Power-set/MATLAB/power-set-3.m
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
powerset({{1,2},3})
|
||||
|
||||
ans =
|
||||
|
||||
{1x0 cell} {1x1 cell} {1x1 cell} {1x2 cell} %This is the same as { {},{{1,2}},{3},{{1,2},3} }
|
||||
1
Task/Power-set/Mathematica/power-set-1.math
Normal file
1
Task/Power-set/Mathematica/power-set-1.math
Normal file
|
|
@ -0,0 +1 @@
|
|||
Subsets[{a, b, c}]
|
||||
1
Task/Power-set/Mathematica/power-set-2.math
Normal file
1
Task/Power-set/Mathematica/power-set-2.math
Normal file
|
|
@ -0,0 +1 @@
|
|||
{{}, {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c}}
|
||||
3
Task/Power-set/Maxima/power-set.maxima
Normal file
3
Task/Power-set/Maxima/power-set.maxima
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
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}} */
|
||||
17
Task/Power-set/OCaml/power-set-1.ocaml
Normal file
17
Task/Power-set/OCaml/power-set-1.ocaml
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
module PowerSet(S: Set.S) =
|
||||
struct
|
||||
|
||||
include Set.Make (S)
|
||||
|
||||
let map f s =
|
||||
let work x r = add (f x) r in
|
||||
fold work s empty
|
||||
;;
|
||||
|
||||
let powerset s =
|
||||
let base = singleton (S.empty) in
|
||||
let work x r = union r (map (S.add x) r) in
|
||||
S.fold work s base
|
||||
;;
|
||||
|
||||
end;; (* PowerSet *)
|
||||
1
Task/Power-set/OCaml/power-set-2.ocaml
Normal file
1
Task/Power-set/OCaml/power-set-2.ocaml
Normal file
|
|
@ -0,0 +1 @@
|
|||
let subsets xs = List.fold_right (fun x rest -> rest @ List.map (fun ys -> x::ys) rest) xs [[]]
|
||||
17
Task/Power-set/Objective-C/power-set.m
Normal file
17
Task/Power-set/Objective-C/power-set.m
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
+ (NSArray *)powerSetForArray:(NSArray *)array {
|
||||
UInt32 subsetCount = 1 << array.count;
|
||||
NSMutableArray *subsets = [NSMutableArray arrayWithCapacity:subsetCount];
|
||||
for(int subsetIndex = 0; subsetIndex < subsetCount; subsetIndex++) {
|
||||
NSMutableArray *subset = [[NSMutableArray alloc] init];
|
||||
for (int itemIndex = 0; itemIndex < array.count; itemIndex++) {
|
||||
if((subsetIndex >> itemIndex) & 0x1) {
|
||||
[subset addObject:[array objectAtIndex:itemIndex]];
|
||||
}
|
||||
}
|
||||
[subsets addObject:subset];
|
||||
[subset release];
|
||||
}
|
||||
return subsets;
|
||||
}
|
||||
16
Task/Power-set/Oz/power-set-1.oz
Normal file
16
Task/Power-set/Oz/power-set-1.oz
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
declare
|
||||
%% Given a set as a list, returns its powerset (again as a list)
|
||||
fun {Powerset Set}
|
||||
proc {Describe Root}
|
||||
%% Describe sets by lower bound (nil) and upper bound (Set)
|
||||
Root = {FS.var.bounds nil Set}
|
||||
%% enumerate all possible sets
|
||||
{FS.distribute naive [Root]}
|
||||
end
|
||||
AllSets = {SearchAll Describe}
|
||||
in
|
||||
%% convert to list representation
|
||||
{Map AllSets FS.reflect.lowerBoundList}
|
||||
end
|
||||
in
|
||||
{Inspect {Powerset [1 2 3 4]}}
|
||||
8
Task/Power-set/Oz/power-set-2.oz
Normal file
8
Task/Power-set/Oz/power-set-2.oz
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
fun {Powerset2 Set}
|
||||
case Set of nil then [nil]
|
||||
[] H|T thens
|
||||
Acc = {Powerset2 T}
|
||||
in
|
||||
{Append Acc {Map Acc fun {$ A} H|A end}}
|
||||
end
|
||||
end
|
||||
1
Task/Power-set/PARI-GP/power-set.pari
Normal file
1
Task/Power-set/PARI-GP/power-set.pari
Normal file
|
|
@ -0,0 +1 @@
|
|||
vector(1<<#S,i,vecextract(S,i-1))
|
||||
59
Task/Power-set/PHP/power-set-1.php
Normal file
59
Task/Power-set/PHP/power-set-1.php
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
function get_subset($binary, $arr) {
|
||||
// based on true/false values in $binary array, include/exclude
|
||||
// values from $arr
|
||||
$subset = array();
|
||||
foreach (range(0, count($arr)-1) as $i) {
|
||||
if ($binary[$i]) {
|
||||
$subset[] = $arr[count($arr) - $i - 1];
|
||||
}
|
||||
}
|
||||
return $subset;
|
||||
}
|
||||
|
||||
function print_array($arr) {
|
||||
if (count($arr) > 0) {
|
||||
echo join(" ", $arr);
|
||||
} else {
|
||||
echo "(empty)";
|
||||
}
|
||||
echo '<br>';
|
||||
}
|
||||
|
||||
function print_power_sets($arr) {
|
||||
echo "POWER SET of [" . join(", ", $arr) . "]<br>";
|
||||
foreach (power_set($arr) as $subset) {
|
||||
print_array($subset);
|
||||
}
|
||||
}
|
||||
|
||||
function power_set($arr) {
|
||||
$binary = array();
|
||||
foreach (range(1, count($arr)) as $i) {
|
||||
$binary[] = false;
|
||||
}
|
||||
$n = count($arr);
|
||||
$powerset = array();
|
||||
|
||||
while (count($binary) <= count($arr)) {
|
||||
$powerset[] = get_subset($binary, $arr);
|
||||
$i = 0;
|
||||
while (true) {
|
||||
if ($binary[$i]) {
|
||||
$binary[$i] = false;
|
||||
$i += 1;
|
||||
} else {
|
||||
$binary[$i] = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$binary[$i] = true;
|
||||
}
|
||||
|
||||
return $powerset;
|
||||
}
|
||||
|
||||
print_power_sets(array());
|
||||
print_power_sets(array('singleton'));
|
||||
print_power_sets(array('dog', 'c', 'b', 'a'));
|
||||
?>
|
||||
21
Task/Power-set/PHP/power-set-2.php
Normal file
21
Task/Power-set/PHP/power-set-2.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
POWER SET of []
|
||||
POWER SET of [singleton]
|
||||
(empty)
|
||||
singleton
|
||||
POWER SET of [dog, c, b, a]
|
||||
(empty)
|
||||
a
|
||||
b
|
||||
a b
|
||||
c
|
||||
a c
|
||||
b c
|
||||
a b c
|
||||
dog
|
||||
a dog
|
||||
b dog
|
||||
a b dog
|
||||
c dog
|
||||
a c dog
|
||||
b c dog
|
||||
a b c dog
|
||||
3
Task/Power-set/Perl-6/power-set.pl6
Normal file
3
Task/Power-set/Perl-6/power-set.pl6
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
sub powerset ( *@list ) {
|
||||
reduce( -> @L, $n { [ @L, @L.map({[ $_.list, $n ]}) ] }, [[]], @list );
|
||||
}
|
||||
1
Task/Power-set/Perl/power-set-1.pl
Normal file
1
Task/Power-set/Perl/power-set-1.pl
Normal file
|
|
@ -0,0 +1 @@
|
|||
sub p{@_?map{$_,[$_[0],@$_]}p(@_[1..$#_]):[]}
|
||||
4
Task/Power-set/Perl/power-set-2.pl
Normal file
4
Task/Power-set/Perl/power-set-2.pl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
use List::Util qw(reduce);
|
||||
sub powerset {
|
||||
@{ (reduce { [@$a, map([@$_, $b], @$a)] } [[]], @_) }
|
||||
}
|
||||
7
Task/Power-set/PicoLisp/power-set.l
Normal file
7
Task/Power-set/PicoLisp/power-set.l
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
(de powerset (Lst)
|
||||
(ifn Lst
|
||||
(cons)
|
||||
(let L (powerset (cdr Lst))
|
||||
(conc
|
||||
(mapcar '((X) (cons (car Lst) X)) L)
|
||||
L ) ) ) )
|
||||
9
Task/Power-set/Prolog/power-set-1.pro
Normal file
9
Task/Power-set/Prolog/power-set-1.pro
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
power_set(T, PS) :-
|
||||
bagof(PS1, power_set(T, [], PS1), PS).
|
||||
|
||||
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([], [], []).
|
||||
18
Task/Power-set/Prolog/power-set-2.pro
Normal file
18
Task/Power-set/Prolog/power-set-2.pro
Normal 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([]).
|
||||
25
Task/Power-set/PureBasic/power-set.purebasic
Normal file
25
Task/Power-set/PureBasic/power-set.purebasic
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
If OpenConsole()
|
||||
Define argc=CountProgramParameters()
|
||||
If argc>=(SizeOf(Integer)*8) Or argc<1
|
||||
PrintN("Set out of range.")
|
||||
End 1
|
||||
Else
|
||||
Define i, j, text$
|
||||
Define.q bset=1<<argc
|
||||
Print("{")
|
||||
For i=0 To bset-1 ; check all binary combinations
|
||||
If Not i: text$= "{"
|
||||
Else : text$=", {"
|
||||
EndIf
|
||||
k=0
|
||||
For j=0 To argc-1 ; step through each bit
|
||||
If i&(1<<j)
|
||||
If k: text$+", ": EndIf ; pad the output
|
||||
text$+ProgramParameter(j): k+1 ; append each matching bit
|
||||
EndIf
|
||||
Next j
|
||||
Print(text$+"}")
|
||||
Next i
|
||||
PrintN("}")
|
||||
EndIf
|
||||
EndIf
|
||||
20
Task/Power-set/Python/power-set-1.py
Normal file
20
Task/Power-set/Python/power-set-1.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
def list_powerset(lst):
|
||||
# the power set of the empty set has one element, the empty set
|
||||
result = [[]]
|
||||
for x in lst:
|
||||
# for every additional element in our set
|
||||
# the power set consists of the subsets that don't
|
||||
# contain this element (just take the previous power set)
|
||||
# plus the subsets that do contain the element (use list
|
||||
# comprehension to add [x] onto everything in the
|
||||
# previous power set)
|
||||
result.extend([subset + [x] for subset in result])
|
||||
return result
|
||||
|
||||
# the above function in one statement
|
||||
def list_powerset2(lst):
|
||||
return reduce(lambda result, x: result + [subset + [x] for subset in result],
|
||||
lst, [[]])
|
||||
|
||||
def powerset(s):
|
||||
return frozenset(map(frozenset, list_powerset(list(s))))
|
||||
9
Task/Power-set/Python/power-set-2.py
Normal file
9
Task/Power-set/Python/power-set-2.py
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
def powersetlist(s):
|
||||
r = [[]]
|
||||
for e in s:
|
||||
print "r: %-55r e: %r" % (r,e)
|
||||
r += [x+[e] for x in r]
|
||||
return r
|
||||
|
||||
s= [0,1,2,3]
|
||||
print "\npowersetlist(%r) =\n %r" % (s, powersetlist(s))
|
||||
25
Task/Power-set/Python/power-set-3.py
Normal file
25
Task/Power-set/Python/power-set-3.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
def powersequence(val):
|
||||
''' Generate a 'powerset' for sequence types that are indexable by integers.
|
||||
Uses a binary count to enumerate the members and returns a list
|
||||
|
||||
Examples:
|
||||
>>> powersequence('STR') # String
|
||||
['', 'S', 'T', 'ST', 'R', 'SR', 'TR', 'STR']
|
||||
>>> powersequence([0,1,2]) # List
|
||||
[[], [0], [1], [0, 1], [2], [0, 2], [1, 2], [0, 1, 2]]
|
||||
>>> powersequence((3,4,5)) # Tuple
|
||||
[(), (3,), (4,), (3, 4), (5,), (3, 5), (4, 5), (3, 4, 5)]
|
||||
>>>
|
||||
'''
|
||||
vtype = type(val); vlen = len(val); vrange = range(vlen)
|
||||
return [ reduce( lambda x,y: x+y, (val[i:i+1] for i in vrange if 2**i & n), vtype())
|
||||
for n in range(2**vlen) ]
|
||||
|
||||
def powerset(s):
|
||||
''' Generate the powerset of s
|
||||
|
||||
Example:
|
||||
>>> powerset(set([6,7,8]))
|
||||
set([frozenset([7]), frozenset([8, 6, 7]), frozenset([6]), frozenset([6, 7]), frozenset([]), frozenset([8]), frozenset([8, 7]), frozenset([8, 6])])
|
||||
'''
|
||||
return set( frozenset(x) for x in powersequence(list(s)) )
|
||||
3
Task/Power-set/Python/power-set-4.py
Normal file
3
Task/Power-set/Python/power-set-4.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
def p(l):
|
||||
if not l: return [[]]
|
||||
return p(l[1:]) + [[l[0]] + x for x in p(l[1:])]
|
||||
4
Task/Power-set/Qi/power-set.qi
Normal file
4
Task/Power-set/Qi/power-set.qi
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(define powerset
|
||||
[] -> [[]]
|
||||
[A|As] -> (append (map (cons A) (powerset As))
|
||||
(powerset As)))
|
||||
1
Task/Power-set/R/power-set-1.r
Normal file
1
Task/Power-set/R/power-set-1.r
Normal file
|
|
@ -0,0 +1 @@
|
|||
library(sets)
|
||||
3
Task/Power-set/R/power-set-2.r
Normal file
3
Task/Power-set/R/power-set-2.r
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
v <- (1:3)^2
|
||||
sv <- as.set(v)
|
||||
2^sv
|
||||
3
Task/Power-set/R/power-set-3.r
Normal file
3
Task/Power-set/R/power-set-3.r
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
l <- list(a=1, b="qwerty", c=list(d=TRUE, e=1:3))
|
||||
sl <- as.set(l)
|
||||
2^sl
|
||||
29
Task/Power-set/REXX/power-set.rexx
Normal file
29
Task/Power-set/REXX/power-set.rexx
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/*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. */
|
||||
end /*chunk*/
|
||||
w=words(ps)
|
||||
do k=1 for w /*show combinations, one/line.*/
|
||||
say right(k,length(w)) word(ps,k)
|
||||
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
|
||||
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 0
|
||||
8
Task/Power-set/Racket/power-set.rkt
Normal file
8
Task/Power-set/Racket/power-set.rkt
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
;;; Direct translation of 'functional' ruby method
|
||||
(define (powerset s)
|
||||
(for/fold ([outer-set (set(set))])
|
||||
([element s])
|
||||
(set-union outer-set
|
||||
(list->set (set-map
|
||||
outer-set
|
||||
(λ(inner-set)(set-add inner-set element)))))))
|
||||
3
Task/Power-set/Rascal/power-set-1.rascal
Normal file
3
Task/Power-set/Rascal/power-set-1.rascal
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
import Set;
|
||||
|
||||
public set[set[&T]] PowerSet(set[&T] s) = power(s);
|
||||
19
Task/Power-set/Rascal/power-set-2.rascal
Normal file
19
Task/Power-set/Rascal/power-set-2.rascal
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
rascal>PowerSet({1,2,3,4})
|
||||
set[set[int]]: {
|
||||
{4,3},
|
||||
{4,2,1},
|
||||
{4,3,1},
|
||||
{4,2},
|
||||
{4,3,2},
|
||||
{4,1},
|
||||
{4,3,2,1},
|
||||
{4},
|
||||
{3},
|
||||
{2,1},
|
||||
{3,1},
|
||||
{2},
|
||||
{3,2},
|
||||
{1},
|
||||
{3,2,1},
|
||||
{}
|
||||
}
|
||||
48
Task/Power-set/Ruby/power-set.rb
Normal file
48
Task/Power-set/Ruby/power-set.rb
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
# 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.
|
||||
class Array
|
||||
# Adds a power_set method to every array, i.e.: [1, 2].power_set
|
||||
def power_set
|
||||
|
||||
# Injects into a blank array of arrays.
|
||||
# acc is what we're injecting into
|
||||
# you is each element of the array
|
||||
inject([[]]) do |acc, you|
|
||||
|
||||
# Set up a new array to add into
|
||||
ret = []
|
||||
|
||||
# For each array in the injected array,
|
||||
acc.each do |i|
|
||||
|
||||
# Add itself into the new array
|
||||
ret << i
|
||||
|
||||
# Merge the array with a new array of the current element
|
||||
ret << i + [you]
|
||||
end
|
||||
|
||||
# Return the array we're looking at to inject more.
|
||||
ret
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# A more functional and even clearer variant.
|
||||
def func_power_set
|
||||
inject([[]]) { |ps,item| # for each item in the Array
|
||||
ps + # take the powerset up to now and add
|
||||
ps.map { |e| e + [item] } # it again, with the item appended to each element
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
#A direct translation of the "power array" version above
|
||||
class Set
|
||||
def powerset
|
||||
inject(Set[Set[]]) do |ps, item|
|
||||
ps.union ps.map {|e| e.union (Set.new [item])}
|
||||
end
|
||||
end
|
||||
end
|
||||
1
Task/Power-set/Scala/power-set.scala
Normal file
1
Task/Power-set/Scala/power-set.scala
Normal file
|
|
@ -0,0 +1 @@
|
|||
def powerset[A](s: Set[A]) = s.foldLeft(Set(Set.empty[A])) { case (ss, el) => ss ++ ss.map(_ + el) }
|
||||
13
Task/Power-set/Scheme/power-set-1.ss
Normal file
13
Task/Power-set/Scheme/power-set-1.ss
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
(define (power-set set)
|
||||
(if (null? set)
|
||||
'(())
|
||||
(let ((rest (power-set (cdr set))))
|
||||
(append (map (lambda (element) (cons (car set) element))
|
||||
rest)
|
||||
rest))))
|
||||
|
||||
(display (power-set (list 1 2 3)))
|
||||
(newline)
|
||||
|
||||
(display (power-set (list "A" "C" "E")))
|
||||
(newline)
|
||||
23
Task/Power-set/Scheme/power-set-2.ss
Normal file
23
Task/Power-set/Scheme/power-set-2.ss
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
(define (power-set lst)
|
||||
(define (iter yield)
|
||||
(let recur ((a '()) (b lst))
|
||||
(if (null? b) (set! yield
|
||||
(call-with-current-continuation
|
||||
(lambda (resume)
|
||||
(set! iter resume)
|
||||
(yield a))))
|
||||
(begin (recur (append a (list (car b))) (cdr b))
|
||||
(recur a (cdr b)))))
|
||||
|
||||
;; signal end of generation
|
||||
(yield 'end-of-seq))
|
||||
|
||||
(lambda () (call-with-current-continuation iter)))
|
||||
|
||||
(define x (power-set '(1 2 3)))
|
||||
(let loop ((a (x)))
|
||||
(if (eq? a 'end-of-seq) #f
|
||||
(begin
|
||||
(display a)
|
||||
(newline)
|
||||
(loop (x)))))
|
||||
7
Task/Power-set/Scheme/power-set-3.ss
Normal file
7
Task/Power-set/Scheme/power-set-3.ss
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
(1 2)
|
||||
(1 3)
|
||||
(1)
|
||||
(2 3)
|
||||
(2)
|
||||
(3)
|
||||
()
|
||||
29
Task/Power-set/Seed7/power-set.seed7
Normal file
29
Task/Power-set/Seed7/power-set.seed7
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
const func array bitset: powerSet (in bitset: baseSet) is func
|
||||
result
|
||||
var array bitset: pwrSet is [] (bitset.value);
|
||||
local
|
||||
var integer: element is 0;
|
||||
var integer: index is 0;
|
||||
var bitset: aSet is bitset.value;
|
||||
begin
|
||||
for element range baseSet do
|
||||
for key index range pwrSet do
|
||||
aSet := pwrSet[index];
|
||||
if element not in aSet then
|
||||
incl(aSet, element);
|
||||
pwrSet &:= aSet;
|
||||
end if;
|
||||
end for;
|
||||
end for;
|
||||
end func;
|
||||
|
||||
const proc: main is func
|
||||
local
|
||||
var bitset: aSet is bitset.value;
|
||||
begin
|
||||
for aSet range powerSet({1, 2, 3, 4}) do
|
||||
writeln(aSet);
|
||||
end for;
|
||||
end func;
|
||||
7
Task/Power-set/Smalltalk/power-set-1.st
Normal file
7
Task/Power-set/Smalltalk/power-set-1.st
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
Collection extend [
|
||||
power [
|
||||
^(0 to: (1 bitShift: self size) - 1) readStream collect: [ :each || i |
|
||||
i := 0.
|
||||
self select: [ :elem | (each bitAt: (i := i + 1)) = 1 ] ]
|
||||
]
|
||||
].
|
||||
5
Task/Power-set/Smalltalk/power-set-2.st
Normal file
5
Task/Power-set/Smalltalk/power-set-2.st
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
#(1 2 4) power do: [ :each |
|
||||
each asArray printNl ].
|
||||
|
||||
#( 'A' 'C' 'E' ) power do: [ :each |
|
||||
each asArray printNl ].
|
||||
1
Task/Power-set/Standard-ML/power-set.ml
Normal file
1
Task/Power-set/Standard-ML/power-set.ml
Normal file
|
|
@ -0,0 +1 @@
|
|||
fun subsets xs = foldr (fn (x, rest) => rest @ map (fn ys => x::ys) rest) [[]] xs
|
||||
14
Task/Power-set/TXR/power-set.txr
Normal file
14
Task/Power-set/TXR/power-set.txr
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
@(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)
|
||||
8
Task/Power-set/Tcl/power-set-1.tcl
Normal file
8
Task/Power-set/Tcl/power-set-1.tcl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
proc subsets {l} {
|
||||
set res [list [list]]
|
||||
foreach e $l {
|
||||
foreach subset $res {lappend res [lappend subset $e]}
|
||||
}
|
||||
return $res
|
||||
}
|
||||
puts [subsets {a b c d}]
|
||||
12
Task/Power-set/Tcl/power-set-2.tcl
Normal file
12
Task/Power-set/Tcl/power-set-2.tcl
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
proc powersetb set {
|
||||
set res {}
|
||||
for {set i 0} {$i < 2**[llength $set]} {incr i} {
|
||||
set pos -1
|
||||
set pset {}
|
||||
foreach el $set {
|
||||
if {$i & 1<<[incr pos]} {lappend pset $el}
|
||||
}
|
||||
lappend res $pset
|
||||
}
|
||||
return $res
|
||||
}
|
||||
1
Task/Power-set/UNIX-Shell/power-set-1.sh
Normal file
1
Task/Power-set/UNIX-Shell/power-set-1.sh
Normal file
|
|
@ -0,0 +1 @@
|
|||
p() { [ $# -eq 0 ] && echo || (shift; p "$@") | while read r ; do echo -e "$1 $r\n$r"; done }
|
||||
5
Task/Power-set/UNIX-Shell/power-set-2.sh
Normal file
5
Task/Power-set/UNIX-Shell/power-set-2.sh
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
|p `cat` | sort | uniq
|
||||
A
|
||||
C
|
||||
E
|
||||
^D
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue