This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1,15 @@
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() {
import std.stdio;
[1, 2, 3].powerSet.writeln;
}

View file

@ -0,0 +1,28 @@
auto powerSet(T)(T[] xs) {
auto output = new T[xs.length];
immutable size_t len = 1U << xs.length;
struct Result {
size_t bits;
@property empty() const pure nothrow { return bits == len; }
void popFront() pure nothrow { bits++; }
@property save() pure nothrow { return this; }
T[] front() nothrow {
size_t pos = 0;
foreach (immutable size_t i; 0 .. xs.length)
if (bits & (1 << i))
output[pos++] = xs[i];
return output[0 .. pos];
}
}
return Result();
}
version (power_set2_main) {
void main() {
import std.stdio;
[1, 2, 3].powerSet.writeln;
}
}

View file

@ -38,13 +38,19 @@ func (i intEle) String() string {
type set []element
// uniqueness of elements can be ensured by using add method
func (s *set) add(e element) {
func (s *set) addEle(e element) {
if !s.hasEle(e) {
*s = append(*s, e)
}
}
func (s *set) hasEle(e element) bool {
for _, ex := range *s {
if e.eq(ex) {
return
return true
}
}
*s = append(*s, e)
return false
}
// method to satify element interface
@ -56,12 +62,8 @@ func (s set) eq(e element) bool {
if len(s) != len(t) {
return false
}
sLoop:
for _, se := range s {
for _, te := range t {
if se.eq(te) {
continue sLoop
}
if !t.hasEle(se) {
return false
}
}
@ -95,8 +97,8 @@ func (s set) powerSet() set {
func main() {
var s set
for _, i := range []int{1, 2, 2, 3, 4, 4, 4} {
s.add(intEle(i))
for _, i := range []intEle{1, 2, 2, 3, 4, 4, 4} {
s.addEle(i)
}
fmt.Println(s)
fmt.Println("length =", len(s))

View file

@ -1 +1,3 @@
library(sets)
for each element in the set:
for each subset constructed so far:
new subset = (subset + element)

View file

@ -1,3 +1,14 @@
v <- (1:3)^2
sv <- as.set(v)
2^sv
powerset = function(set){
ps = list()
ps[[1]] = numeric() #Start with the empty set.
for(element in set){ #For each element in the set, take all subsets
temp = vector(mode="list",length=length(ps)) #currently in "ps" and create new subsets (in "temp")
for(subset in 1:length(ps)){ #by adding "element" to each of them.
temp[[subset]] = c(ps[[subset]],element)
}
ps=c(ps,temp) #Add the additional subsets ("temp") to "ps".
}
return(ps)
}
powerset(1:4)

View file

@ -1,3 +1 @@
l <- list(a=1, b="qwerty", c=list(d=TRUE, e=1:3))
sl <- as.set(l)
2^sl
library(sets)

View file

@ -0,0 +1,3 @@
v <- (1:3)^2
sv <- as.set(v)
2^sv

View file

@ -0,0 +1,3 @@
l <- list(a=1, b="qwerty", c=list(d=TRUE, e=1:3))
sl <- as.set(l)
2^sl

View file

@ -8,23 +8,12 @@ class Array
# 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]
ret = [] # Set up a new array to add into
acc.each do |i| # For each array in the injected array,
ret << i # Add itself into the new array
ret << i + [you] # Merge the array with a new array of the current element
end
# Return the array we're looking at to inject more.
ret
ret # Return the array we're looking at to inject more.
end
end
@ -39,10 +28,16 @@ class Array
end
#A direct translation of the "power array" version above
require 'set'
class Set
def powerset
inject(Set[Set[]]) do |ps, item|
ps.union ps.map {|e| e.union (Set.new [item])}
end
def powerset
inject(Set[Set[]]) do |ps, item|
ps.union ps.map {|e| e.union (Set.new [item])}
end
end
end
p [1,2,3,4].power_set
p %w(one two three).func_power_set
p Set[1,2,3].powerset

View file

@ -0,0 +1,5 @@
(define (power_set_iter set)
(let loop ((res '(())) (s set))
(if (empty? s)
res
(loop (append (map (lambda (i) (cons (car s) i)) res) res) (cdr s)))))

View file

@ -0,0 +1,32 @@
'((e d c b a)
(e d c b)
(e d c a)
(e d c)
(e d b a)
(e d b)
(e d a)
(e d)
(e c b a)
(e c b)
(e c a)
(e c)
(e b a)
(e b)
(e a)
(e)
(d c b a)
(d c b)
(d c a)
(d c)
(d b a)
(d b)
(d a)
(d)
(c b a)
(c b)
(c a)
(c)
(b a)
(b)
(a)
())