Family Day update
This commit is contained in:
parent
aac6731f2c
commit
9ad63ea473
2442 changed files with 39761 additions and 8255 deletions
16
Task/Power-set/D/power-set-3.d
Normal file
16
Task/Power-set/D/power-set-3.d
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
// Haskell definition:
|
||||
// foldr f z [] = z
|
||||
// foldr f z (x:xs) = x `f` foldr f z xs
|
||||
S foldr(T, S)(S function(T, S) f, S z, T[] rest) {
|
||||
return (rest.length == 0) ? z : f(rest[0], foldr(f, z, rest[1..$]));
|
||||
}
|
||||
|
||||
// Haskell definition:
|
||||
//powerSet = foldr (\x acc -> acc ++ map (x:) acc) [[]]
|
||||
T[][] powerset(T)(T[] set) {
|
||||
import std.algorithm;
|
||||
import std.array;
|
||||
// Note: The types before x and acc aren't needed, so this could be made even more concise, but I think it helps
|
||||
// to make the algorithm slightly clearer.
|
||||
return foldr( (T x, T[][] acc) => acc ~ acc.map!(accx => x ~ accx).array , [[]], set );
|
||||
}
|
||||
|
|
@ -1,14 +1,6 @@
|
|||
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 powerSetRec(head, tail) {
|
||||
if (!tail) return [head]
|
||||
powerSetRec(head, tail.tail()) + powerSetRec(head + [tail.head()], tail.tail())
|
||||
}
|
||||
|
||||
def powerSet = { set ->
|
||||
(0..(set.size())).inject([]){ list, i -> list + comb(i,set as List)}.collect { it as LinkedHashSet } as LinkedHashSet
|
||||
}
|
||||
def powerSet(set) { powerSetRec([], set as List) as Set}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
def vocalists = [ "C", "S", "N", "Y" ] as LinkedHashSet
|
||||
println "${vocalists}"
|
||||
def vocalists = [ 'C', 'S', 'N', 'Y' ] as Set
|
||||
println vocalists
|
||||
println powerSet(vocalists)
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
powerset = function(set){
|
||||
ps = list()
|
||||
ps[[1]] = numeric() #Start with the empty set.
|
||||
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")
|
||||
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".
|
||||
ps <- c(ps,temp) #Add the additional subsets ("temp") to "ps".
|
||||
}
|
||||
return(ps)
|
||||
ps
|
||||
}
|
||||
|
||||
powerset(1:4)
|
||||
|
|
|
|||
|
|
@ -6,29 +6,26 @@ N= words(S); do chunk=1 for N /*traipse through the items in
|
|||
@=@ combN(N, chunk) /*take N items, a CHUNK at a time. */
|
||||
end /*chunk*/
|
||||
w= length(2**N) /*the number of items in the power set.*/
|
||||
do k=1 for words(@) /* [↓] show combinations, one per line*/
|
||||
do k=1 for words(@) /* [↓] show combinations, 1 per line.*/
|
||||
say right(k, w) word(@, k) /*display a single combination to term.*/
|
||||
end /*k*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
combN: procedure expose S; parse arg x,y; base= x + 1; bbase= base - y
|
||||
combN: procedure expose S; parse arg x,y; base= x + 1; bbase= base - y
|
||||
!.= 0
|
||||
do p=1 for y; !.p= p
|
||||
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*/
|
||||
|
||||
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 a partial powerset chunk.*/
|
||||
return strip($) /*return with a partial power set chunk*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
.combU: procedure expose !. y bbase; parse arg d; if d==0 then return 1
|
||||
.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*/
|
||||
do u=d to y; !.u= p + 1; if !.u==bbase+u then return .combU(u-1)
|
||||
p= !.u
|
||||
end /*u*/
|
||||
return 0
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue