RosettaCodeData/Task/Power-set/R/power-set-2.r

15 lines
486 B
R
Raw Permalink Normal View History

2020-02-17 23:21:07 -08:00
powerset <- function(set){
ps <- list()
ps[[1]] <- numeric() #Start with the empty set.
2013-10-27 22:24:23 +00:00
for(element in set){ #For each element in the set, take all subsets
2020-02-17 23:21:07 -08:00
temp <- vector(mode="list",length=length(ps)) #currently in "ps" and create new subsets (in "temp")
2013-10-27 22:24:23 +00:00
for(subset in 1:length(ps)){ #by adding "element" to each of them.
temp[[subset]] = c(ps[[subset]],element)
}
2020-02-17 23:21:07 -08:00
ps <- c(ps,temp) #Add the additional subsets ("temp") to "ps".
2013-10-27 22:24:23 +00:00
}
2020-02-17 23:21:07 -08:00
ps
2013-10-27 22:24:23 +00:00
}
powerset(1:4)