subset
Find all subsets of length k in an array
Recursion is your friend for this task. For each element – “guess” if it is in the current subset, and recursively invoke with the guess and a smaller superset you can select from. Doing so for both the “yes” and “no” guesses – will result in all possible subsets. Restraining yourself to a certain length … Read more
creating a new list with subset of list using index in python
Suppose a = [‘a’, ‘b’, ‘c’, 3, 4, ‘d’, 6, 7, 8] and the list of indexes is stored in b= [0, 1, 2, 4, 6, 7, 8] then a simple one-line solution will be c = [a[i] for i in b]
Finding all the subsets of a set
It’s very simple to do this recursively. The basic idea is that for each element, the set of subsets can be divided equally into those that contain that element and those that don’t, and those two sets are otherwise equal. For n=1, the set of subsets is {{}, {1}} For n>1, find the set of … Read more
Calculating all of the subsets of a set of numbers
What you want is called a Powerset. Here is a simple implementation of it: public static Set<Set<Integer>> powerSet(Set<Integer> originalSet) { Set<Set<Integer>> sets = new HashSet<Set<Integer>>(); if (originalSet.isEmpty()) { sets.add(new HashSet<Integer>()); return sets; } List<Integer> list = new ArrayList<Integer>(originalSet); Integer head = list.get(0); Set<Integer> rest = new HashSet<Integer>(list.subList(1, list.size())); for (Set<Integer> set : powerSet(rest)) { Set<Integer> … Read more