Algorithm to determine all possible ways a group of values can be removed from a sequence

(Because it was unclear in the original version of the question whether you meant to remove a subsequence or an unordered list, I’ve updated my answer to address both cases.) 1. Removing a sub-sequence in order We get a sequence of values, e.g. [1,5,1,3,4,2,3,2,1,3,1,2], and a sub-sequence of values to be removed from the first … Read more

Generating all possible combinations of a list, “itertools.combinations” misses some results

Use itertools.permutations: >>> import itertools >>> stuff = [1, 2, 3] >>> for L in range(0, len(stuff)+1): for subset in itertools.permutations(stuff, L): print(subset) … () (1,) (2,) (3,) (1, 2) (1, 3) (2, 1) (2, 3) (3, 1) …. Help on itertools.permutations: permutations(iterable[, r]) –> permutations object Return successive r-length permutations of elements in the … Read more

Algorithm to get all the combinations of size n from an array (Java)? [closed]

This is a well-studied problem of generating all k-subsets, or k-combinations, which can be easily done without recursion. The idea is to have array of size k keeping sequence of indices of elements from the input array (which are numbers from 0 to n – 1) in increasing order. (Subset then can be created by … Read more

Generate a matrix containing all combinations of elements from n vectors (Cartesian product)

The ndgrid function almost gives the answer, but has one caveat: n output variables must be explicitly defined to call it. Since n is arbitrary, the best way is to use a comma-separated list (generated from a cell array with ncells) to serve as output. The resulting n matrices are then concatenated into the desired … Read more