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
835
votes
40 answers

How do I generate all permutations of a list?

How do I generate all the permutations of a list? For example: permutations([]) [] permutations([1]) [1] permutations([1, 2]) [1, 2] [2, 1] permutations([1, 2, 3]) [1, 2, 3] [1, 3, 2] [2, 1, 3] [2, 3, 1] [3, 1, 2] [3, 2, 1]
Ricardo Reyes
  • 13,256
  • 4
  • 27
  • 19
821
votes
14 answers

Shuffle DataFrame rows

I have the following DataFrame: Col1 Col2 Col3 Type 0 1 2 3 1 1 4 5 6 1 ... 20 7 8 9 2 21 10 11 12 2 ... 45 13 14 15 3 46 16 17 18 3 ... The DataFrame…
JNevens
  • 11,202
  • 9
  • 46
  • 72
211
votes
41 answers

Permutations in JavaScript?

I'm trying to write a function that does the following: takes an array of integers as an argument (e.g. [1,2,3,4]) creates an array of all the possible permutations of [1,2,3,4], with each permutation having a length of 4 the function below (I…
pepperdreamteam
  • 2,772
  • 3
  • 17
  • 15
205
votes
9 answers

Is java.util.Random really that random? How can I generate 52! (factorial) possible sequences?

I've been using Random (java.util.Random) to shuffle a deck of 52 cards. There are 52! (8.0658175e+67) possibilities. Yet, I've found out that the seed for java.util.Random is a long, which is much smaller at 2^64 (1.8446744e+19). From here, I'm…
Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52
194
votes
28 answers

Listing all permutations of a string/integer

A common task in programming interviews (not from my experience of interviews though) is to take a string or an integer and list every possible permutation. Is there an example of how this is done and the logic behind solving such a problem? I've…
GurdeepS
  • 65,107
  • 109
  • 251
  • 387
164
votes
36 answers

Generate list of all possible permutations of a string

How would I go about generating a list of all possible permutations of a string between x and y characters in length, containing a variable list of characters. Any language would work, but it should be portable.
UnkwnTech
  • 88,102
  • 65
  • 184
  • 229
128
votes
6 answers

std::next_permutation Implementation Explanation

I was curious how std:next_permutation was implemented so I extracted the the gnu libstdc++ 4.7 version and sanitized the identifiers and formatting to produce the following demo... #include #include #include using…
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
127
votes
36 answers

Algorithm to generate all possible permutations of a list?

Say I have a list of n elements, I know there are n! possible ways to order these elements. What is an algorithm to generate all possible orderings of this list? Example, I have list [a, b, c]. The algorithm would return [[a, b, c], [a, c, b,], [b,…
fent
  • 17,861
  • 15
  • 87
  • 91
127
votes
13 answers

Fast permutation -> number -> permutation mapping algorithms

I have n elements. For the sake of an example, let's say, 7 elements, 1234567. I know there are 7! = 5040 permutations possible of these 7 elements. I want a fast algorithm comprising two functions: f(number) maps a number between 0 and 5039 to a…
ijw
  • 4,376
  • 3
  • 25
  • 29
120
votes
28 answers

Finding all possible permutations of a given string in python

I have a string. I want to generate all permutations from that string, by changing the order of characters in it. For example, say: x='stack' what I want is a list like this, l=['stack','satck','sackt'.......] Currently I am iterating on the list…
Nihar Sarangi
  • 4,845
  • 8
  • 27
  • 32
120
votes
9 answers

How to randomize (or permute) a dataframe rowwise and columnwise?

I have a dataframe (df1) like this. f1 f2 f3 f4 f5 d1 1 0 1 1 1 d2 1 0 0 1 0 d3 0 0 0 1 1 d4 0 1 0 0 1 The d1...d4 column is the rowname, the f1...f5 row is the columnname. To…
a83
  • 1,211
  • 2
  • 8
  • 3
100
votes
4 answers

shuffle vs permute numpy

What is the difference between numpy.random.shuffle(x) and numpy.random.permutation(x)? I have read the doc pages but I could not understand if there was any difference between the two when I just want to randomly shuffle the elements of an…
DotPi
  • 3,977
  • 6
  • 33
  • 53
88
votes
20 answers

permutations with unique values

itertools.permutations generates where its elements are treated as unique based on their position, not on their value. So basically I want to avoid duplicates like this: >>> list(itertools.permutations([1, 1, 1])) [(1, 1, 1), (1, 1, 1), (1, 1, 1),…
xyz-123
  • 2,227
  • 4
  • 21
  • 27
84
votes
10 answers

JavaScript - Generating combinations from n arrays with m elements

I'm having trouble coming up with code to generate combinations from n number of arrays with m number of elements in them, in JavaScript. I've seen similar questions about this for other languages, but the answers incorporate syntactic or library…
quano
  • 18,812
  • 25
  • 97
  • 108
81
votes
10 answers

Generate list of all possible combinations of elements of vector

I am trying to generate all possible combinations of 0 and 1's in a vector of length 14. Is there an easy way of getting that output as a list of vectors, or even better, a dataframe? To demonstrate better what I am looking for, let's suppose that I…
Mayou
  • 8,498
  • 16
  • 59
  • 98
1
2 3
99 100