227 questions
8
votes
3
answers
1k
views
How can I create an array of arrays of strings of varying lengths in C?
I am trying to solve the problem posed in this question. Here the OP asks that given a 15 element set, its 2^15 element power set be generated in a manner such that the elements of the power set are ...
0
votes
0
answers
60
views
I struggle understanding the "Powerset" Operator in the language clingo
First of all: how is the operator "{}" really called? The documentation ist not very good at answering this.
I call it powerset operator because it creates a powerset of all elements.
{ z(1)...
1
vote
2
answers
85
views
Powerset where each element can be either "positive" or "negative"
I'm looking for a simple way to generate the powerset of an iterable where each element can be "positive" or "negative", but not both in the same combination.
There are no ...
0
votes
1
answer
90
views
Constructing a modified powerset in Rust - filtering out certain subsets based on their content
What is the most efficient way (and/or most idiomatic) to make a modified powerset of vector of 2-tuples in Rust? By modified, what I mean is the following rule
(a,b),(c,b) not in any set, for any a,b,...
-4
votes
2
answers
92
views
Powerset of a list with equal elements in Java [closed]
Chat-GPT generated me the following code in Java, but it still doesn't work with equal elements.
The supposed output is incorrect, since the equal elements get simply ignored and I can't figure out ...
1
vote
1
answer
149
views
Haskell Power Set Function
I get an error:
working.hs:186:25: error:
* Couldn't match expected type: Set (Set a)
with actual type: [[a]]
* In the expression:
union (map (insert x) (powerSet s)) (powerSet s)
...
0
votes
1
answer
56
views
Javascript: Array being passed as parameter doesn't retain values
I am trying to solve the problem Subsets using Javascript. The task is to print the powerset i.e., all subsets from an array. In the function below, I am passing an array as a parameter to collect all ...
0
votes
2
answers
126
views
Finding best result matching sets of items. Powerset optimization
I'm having this combination problem where I'm trying to find subsets of matching combinations of 2 items which results in the highest amount of total matches.
Every item can only be in a match once.
...
1
vote
1
answer
121
views
More efficient powerset algorithm haskell
I Have a powerset function which creates a list [[a]] but the largest [a] is worked out first, meaning the whole algorithm has to run before I can get the smaller values.
I need a function which ...
-1
votes
2
answers
70
views
Obtaining power set in mathematical order
The powerset of {1, 2, 3} is:
{{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}
I have a String array in java,
String elements={"apple","mango","banana"};
...
0
votes
1
answer
54
views
How to make a powerset in ascending order for the first item in a list?
I am trying to make a powerset of a list for the first item of that list in ascending order. However, I couldn't find on StackOverflow how to tackle this specific problem.
When making a powerset of ...
2
votes
1
answer
223
views
Time complexity of Powerset in Python
The time complexity of this code to create a powerset from distinct integers is listed as O(n * 2^n) at all places including the Leetcode solution.
I listed the complexity of each step as a code ...
0
votes
2
answers
481
views
generating powerset with backtracking where subsets with lower number of elements appear earlier and they are sorted
I was solving the classic powerset generation using backtracking. Now, let's say, I add another constraint, the output should appear in a specific order.
For input = [1, 2, 3], the output should be [[]...
0
votes
1
answer
98
views
How to generate powersets with a maximum size in C#?
I am trying to generate all powersets from a given list within a limit of given maximum size. I've found some great answers for how to generate powersets in general and admire the solution using ...
0
votes
0
answers
71
views
How to modify this power set function to only include 'valid' subsets?
I am using a binomial tree as a list with 2^T - 1 'nodes' and want to create a set of subsets that work within some given criteria (outlined below) on the elements of the list. Right now, I use the ...
1
vote
2
answers
473
views
Power set using BST in haskell
I have implemented a Set datatype in Haskell using Binary search tree. I have not used any of the in-built functions nor imported any module.
My set datatype is as follows:
data Set a = Empty | Node a ...
1
vote
1
answer
749
views
Time Complexity of recursive Power Set function
I am having trouble with simplifying the time complexity for this recursive algorithm for finding the Power-Set of a given Input Set. I not entirely sure if what I have got is correct so far either.
...
0
votes
1
answer
274
views
How to use Iterator next() method to generate power set of LinkedSet
For my class I have to create an iterator that will generate a power set of a LinkedSet. Per my professor, I have to iterate through a bitString of the int current. Where current is the subset to ...
0
votes
2
answers
143
views
Convert itertools powerset into columnized numpy array
Given a tuple items, I get the powerset with itertools:
items = tuple(('a', 'b', 'c'))
combos = powerset(items)
def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3)...
0
votes
1
answer
203
views
How to find the power set of a given set without using left shift bit?
I'm trying to figure out how to implement an algorithm to find a power set given a set, but I'm having some trouble. The sets are actually vectors so for example I am given Set<char> set1{ 'a','...
1
vote
1
answer
202
views
Python Combinations of letters to boxes . boxes can be empty
I am trying to figure out an optimized Python code that, given a set of letters for example [A,B,C] it will arrange them in n_boxes boxes.
each box can have no letters, one letter, or a combination ...
2
votes
2
answers
394
views
Error when using pointers to append into slice [][]int
While I was trying to solve a problem "Subset II" from LC, I came across a strange problem. The code generates a power set from a given set.
However, when I run the code it failed because ...
0
votes
3
answers
154
views
How to get powerset from a set with 3600 elements using as little memory as possible
I have been looking for a language and code to help me calculate all possible subsets of a set of 3600 elements. At first my search started with python, then I went through JavaScript and then came to ...
0
votes
1
answer
112
views
Find all subsets upto length k while calculating power set of n?
Given a set {1, 2, 3, 4, 5 ... n} of n elements, we need to find all subsets of length up to k.
For example,
Input: n = 4 and k = 2
Output: {{1}, {2}, {3}, {4}, {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, ...
4
votes
3
answers
919
views
Generating Power Set with Recursion and Yield Statement in Python
I am a bit new to Python and am working through programming exercises. I have written the following recursive method to generate the power set based on an input list in Python. It should return a ...
2
votes
0
answers
65
views
how to write Elasticsearch(NEST for .net) query to produce result like power set?
I'm using Elasticsearch with default implementation for searching products of my eCommerce site. This default implementation is producing considerably relevant search results. However, I want my ...
3
votes
1
answer
259
views
Patterns for managing/representing selection from a set of options in Haskell
Algebraic data types make it easy to allow one item from a set to be selected: just use an appropriate sum type.
I'm curious how one would implement the option of selecting strictly n items or fewer ...
1
vote
1
answer
73
views
Tower of powersets
I would like to define a family of types PowersetTower : Type -> nat -> Type such that:
PowersetTower A 0 = A
PowersetTower A (n+1) = Ensemble (PowersetTower A n) (that is, PowersetTower A n -&...
0
votes
0
answers
171
views
Python powerset - memory error when unpacking the itertools.chain object into a list
Hi I am fairly new to python. I am trying to generate a powerset of all combinations for a list of integers, using the recommended code:
def powerset(iterable):
s = list(iterable)
return chain....
0
votes
1
answer
252
views
Java 8 - Generate power set of list
I have written the following method to generate power set using Java 8 map function
public static List<List<Integer>> powerSet(List<Integer> arr){
List<List<Integer>&...
0
votes
1
answer
44
views
how to find all the subsets with restrictions in python?
So i am doing a comb. auction algorithm
and i like to give him N number of items for example
(A,B,C)
and i want the algorithm to give me back the following result
(A,B,C)
(ABC)
(AB,C)
(AC,B)
(BC,A)
...
0
votes
2
answers
243
views
Power Sets too slow python
I am using the following function to find the subsets of a list L. However, when converting the output of the function powerset into a list it takes way too long. Any suggestion?
For clarification, ...
2
votes
1
answer
364
views
Find subsets of a set in python (order does not matter)
I need to find the subsets of a set L = [0, 3, 4, 6, 9, 11, 12, 13].
I saw online the following solution:
def powerset(s):
x = len(s)
masks = [1 << i for i in range(x)]
for i in ...
0
votes
0
answers
520
views
Recursive backtracking to find all subsets
I have tried to solve a problem of generating all subsets of an array which contains no duplicates from leetcode : https://leetcode.com/problems/subsets/ .
e.g if input is [1,2,3], output should be [[]...
2
votes
1
answer
165
views
Powerset of a list using abstract list functions
Is it possible to make a racket function to return powerset of a given list?
Constraints-
without explicit recursion
use abstract list functions
code contained in 2 lines (actual requirement)
For eg:...
0
votes
1
answer
101
views
Find powersets for a given set
I'm trying to learn how to code and stumbled upon a problem generating a powerset of a source set in java.
I try to write a method that returns a list of lists of doubles for a input of list of ...
3
votes
1
answer
89
views
Python - Unexpected behavior when generating the powerset in range [0, n-1]
I am trying to recursively generate the powerset in the range [0, n - 1] without passing extra parameters to the inner search function.
def powerSet(n):
all_subsets = []
curr_subset = []
def ...
0
votes
1
answer
58
views
Power set of an input set as custom collection
I have been reading the Effective Java book and I have stuck with this code I am unable to understand how this code is generating power set.
Code:
public class PowerSet {
public static final <...
1
vote
1
answer
916
views
Racket - A function that takes a set as input, and outputs its power set
I'm trying to write a function that takes in a set as an argument and returns its power set, also as a set of sets.
Example use:
(power-set (set 1 2))
; is supposed to output
=> (set (set) (set ...
0
votes
1
answer
110
views
How to save the contents of a powerSet into a 2d array in Java
I'm trying to save the contents of a PowerSet, obtained from a 1d Array into a 2d Array. I tried assigning the values in the array inside the "if" Statement but I'm getting the indices completely ...
0
votes
1
answer
1k
views
Generate all combinations of subsets which contain exactly the elements in the original set
Given the powerset of 1, 2, 3 I get 8 subsets: [], [1], [2], [2,1], [3], [3,1], [3,2], [3,2,1]. I would like to then generate the subsets of this new set which have exactly 1 of the original set.
The ...
1
vote
1
answer
171
views
Finding the power set of a generic set
I had been given a question to utilize java generics and create a Set class. I have been able to perform other functions such as union, intersection, complement etc using this Set class.
But the ...
1
vote
3
answers
76
views
Recursively defining a list in python, all in the list are replaced by the last item
I've just started working with Python, and I just came across some behavior that I don't understand. I've searched the site for an explanation, but I haven't been able to find it. Perhaps I don't know ...
0
votes
2
answers
114
views
Generating Power Set algorithm implementation
What is a good implementation of Power Set algorithm?
Recently I needed this algorithm for building a solver for my puzzle game. Generally, the solver should try strategies (sets of turns, the ...
0
votes
1
answer
117
views
Power Set using Recursion
#include <stdio.h>
void powerSet(int* a, int index, int *curr, int N) {
if (index == N)
return;
printf("(");
for(int i = 0; i <= index; i++)
printf("%d, ", curr[...
0
votes
2
answers
6k
views
Time Complexity of Finding All Possible Sub Array of an Array
As per the implementation of finding all possible sub-array from a given array as follows:
public class AllPossibleSubArray {
public static void main(String[] args) {
int[] arr = { 1, 2, ...
-1
votes
1
answer
62
views
Why doesn't my code work for outputting of all subsets of a set work?
I'm trying to write code to output all subsets of a set (represented by a list of integers).
Why doesn't the following code work?
public static List<List<Integer>> powerSet(List<...
0
votes
1
answer
45
views
Initialise Power set as anonymous interface
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class PowerSet {
public static final <E> Collection&...
3
votes
3
answers
546
views
Is there a function for finding all subgroups from one main group with filtering some of the subgroups?
I am coding with java so if you can share code with java it would be nice :)
Lets say I have a group of (1,2,3,4,5) and I want to create all subgroups of this group with maximum size of a given ...
1
vote
1
answer
1k
views
Haskell - implement powerset function of a set
I've just started to learn Haskell and had difficult times of solving this problem:
powerSet :: Set a -> Set (Set a)
Here's my attempt:
powerSet :: Tree a -> Tree (Tree a)
powerSet Empty = ...