Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
15
Task/Power-set/D/power-set-1.d
Normal file
15
Task/Power-set/D/power-set-1.d
Normal 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;
|
||||
}
|
||||
28
Task/Power-set/D/power-set-2.d
Normal file
28
Task/Power-set/D/power-set-2.d
Normal 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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -1 +1,3 @@
|
|||
library(sets)
|
||||
for each element in the set:
|
||||
for each subset constructed so far:
|
||||
new subset = (subset + element)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -1,3 +1 @@
|
|||
l <- list(a=1, b="qwerty", c=list(d=TRUE, e=1:3))
|
||||
sl <- as.set(l)
|
||||
2^sl
|
||||
library(sets)
|
||||
|
|
|
|||
3
Task/Power-set/R/power-set-4.r
Normal file
3
Task/Power-set/R/power-set-4.r
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
v <- (1:3)^2
|
||||
sv <- as.set(v)
|
||||
2^sv
|
||||
3
Task/Power-set/R/power-set-5.r
Normal file
3
Task/Power-set/R/power-set-5.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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
5
Task/Power-set/Scheme/power-set-4.ss
Normal file
5
Task/Power-set/Scheme/power-set-4.ss
Normal 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)))))
|
||||
32
Task/Power-set/Scheme/power-set-5.ss
Normal file
32
Task/Power-set/Scheme/power-set-5.ss
Normal 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)
|
||||
())
|
||||
Loading…
Add table
Add a link
Reference in a new issue