Questions tagged [permutation]

A permutation is an arrangement of objects into a particular order.

A permutation is an arrangement of objects into a particular order.

For example, permutations of a set such as {1,2,3} are (1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), and (3,2,1). The number of permutation of n objects is n! = n*(n-1)*(n-2)*...*3*2*1

References

5231 questions
14
votes
3 answers

Python permutations with constraints

I am using python 3 and I am trying to find a way to get all the permutations of a list while enforcing some constraints. For instance, I have a list L=[1, 2, 3, 4, 5, 6, 7] I want to find all permutations. However, My constraints are: 1 should…
Keeto
  • 4,074
  • 9
  • 35
  • 58
14
votes
1 answer

algorithm to find square chained permutations

A permutation is square chained if the sum of consecutive numbers is always a perfect square. For example, 8 1 15 10 6 3 13 12 4 5 11 14 2 7 9 16 is a squared chain permutation of the numbers 1 to 16. I want to write a program to find a square…
A B
  • 141
  • 3
14
votes
4 answers

All possibilities to split a list into two lists

I have a list with some elements and want to iterate over all possible ways to divide this list into two lists. By that I mean all combinations, so the order doesn't matter (i.e. Element 1 and 3 could be in the one list and Element 2 in the other).…
PattuX
  • 297
  • 2
  • 11
14
votes
3 answers

Find ith permutation in javascript

Given an array arr of size n, and and index 0<=i
Harry
  • 183
  • 1
  • 8
14
votes
4 answers

Generate all possible outcomes of k balls in n bins (sum of multinomial / categorical outcomes)

Suppose we have n bins in which we are throwing k balls. What is a fast (i.e. using numpy/scipy instead of python code) way to generate all possible outcomes as a matrix? For example, if n = 4 and k = 3, we'd want the following numpy.array: 3 0 0…
Andrew Mao
  • 35,740
  • 23
  • 143
  • 224
14
votes
5 answers

Generating all permutations of N balls in M bins

I want to generate a set of permutations of n balls in m bins. The following set of nested lists generates those permutations. n <- 3 m <- 4 v <- rep(0,m) for (i in n:0){ for (j in (n-sum(i)):0){ for (k in (n-sum(i,j)):0){ for (l in (n -…
Andy W
  • 5,031
  • 3
  • 25
  • 51
14
votes
1 answer

What are the Ruby equivalent of Python itertools, esp. combinations/permutations/groupby?

Python's itertools module provides a lots of goodies with respect to processing an iterable/iterator by use of generators. For example, permutations(range(3)) --> 012 021 102 120 201 210 combinations('ABCD', 2) --> AB AC AD BC BD CD [list(g) for…
Hai-Anh Trinh
  • 1,142
  • 11
  • 11
14
votes
7 answers

Get permutation with specified degree by index number

I've been working on this for hours but couldn't figure it out. Define a permutation's degree to be the minimum number of transpositions that need to be composed to create it. So a the degree of (0, 1, 2, 3) is 0, the degree of (0, 1, 3, 2) is 1,…
Ram Rachum
  • 84,019
  • 84
  • 236
  • 374
14
votes
3 answers

Algorithm to Generate All Possible Black and White Pixel Images in 640 x 360 Dimensions?

I have very minimal programming experience. I would like to write a program that will generate and save as a gif image every possible image that can be created using only black and white pixels in 640 by 360 px dimensions. In other words, each pixel…
user3084984
  • 149
  • 1
  • 3
14
votes
6 answers

Prolog performance and recursion type

I was playing with permutation in a couple of programs and stumbled upon this little experiment: Permutation method 1: permute([], []). permute([X|Rest], L) :- permute(Rest, L1), select(X, L, L1). Permutation method 2: permute([],…
lurker
  • 56,987
  • 9
  • 69
  • 103
14
votes
3 answers

More efficient way to get integer permutations?

I can get integer permutations like this: myInt = 123456789 l = itertools.permutations(str(myInt)) [int(''.join(x)) for x in l] Is there a more efficient way to get integer permutations in Python, skipping the overhead of creating a string, then…
jumbopap
  • 3,969
  • 5
  • 27
  • 47
13
votes
5 answers

find a cost optimized path through a grid/matrix in c++

I am stuck with a problem and could not find much help online. I need to find the minimum cost combination of numbers from multiple vectors of numbers. The vector size is same for all vectors. For example, consider the following : row [0]: a b c …
coolcav
  • 131
  • 1
  • 6
13
votes
5 answers

Generate All Permutations in Excel using LAMBDA

This is a frequently asked and answered question: how can I generate all permutations in Excel: 2011 2016 2017 2017 superuser 2018 2021 and now in 2022 which did not get an answer before it was closed as a duplicate, which is unfortunate because…
mark fitzpatrick
  • 3,162
  • 2
  • 11
  • 23
13
votes
2 answers

Why Time complexity of permutation function is O(n!)

Consider following code. public class Permutations { static int count=0; static void permutations(String str, String prefix){ if(str.length()==0){ System.out.println(prefix); } else{ for(int…
user5350118
13
votes
5 answers

Is there an algorithm to generate all unique circular permutations of a multiset?

I encountered this problem when doing some enthusiastic programming. The problem can be expressed as follows: For a multiset A, let P(A) denote the set of all possible permutations of A. P(A) is naturally divided into disjoint subsets that…
Cong Ma
  • 10,692
  • 3
  • 31
  • 47