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
38
votes
5 answers

Generate permutations of JavaScript array

I have an array of n different elements in javascript, I know there are n! possible ways to order these elements. I want to know what's the most effective (fastest) algorithm to generate all possible orderings of this array? I have this code: var…
DSB
  • 597
  • 2
  • 6
  • 11
38
votes
8 answers

Get all permutations of a PHP array?

Given a PHP array of strings, e.g.: ['peter', 'paul', 'mary'] How to generate all possible permutations of elements of this array? i.e.: peter-paul-mary peter-mary-paul paul-peter-mary paul-mary-peter mary-peter-paul mary-paul-peter
ohho
  • 50,879
  • 75
  • 256
  • 383
36
votes
4 answers

Code Golf: Countdown Number Game

Challenge Here is the task, inspired by the well-known British TV game show Countdown. The challenge should be pretty clear even without any knowledge of the game, but feel free to ask for clarifications. And if you fancy seeing a clip of this game…
Noldorin
  • 144,213
  • 56
  • 264
  • 302
35
votes
3 answers

How to generate permutations or combinations of object in R?

How to generate sequences of r objects from n objects? I'm looking for a way to do either permutations or combinations, with/without replacement, with distinct and non-distinct items (aka multisets). This is related to twelvefold way. The "distinct"…
Randy Lai
  • 3,084
  • 2
  • 22
  • 23
34
votes
3 answers

algorithm for python itertools.permutations

Can someone please explain algorithm for itertools.permutations routine in Python standard lib 2.6? I don't understand why it works. Code is: def permutations(iterable, r=None): # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC …
zaharpopov
  • 16,882
  • 23
  • 75
  • 93
33
votes
9 answers

Permutations without recursive function call

Requirement: Algorithm to generate all possible combinations of a set , without duplicates , or recursively calling function to return results. The majority , if not all of the Answers provided at Permutations in JavaScript? recursively call a…
guest271314
  • 1
  • 15
  • 104
  • 177
32
votes
4 answers

How do I generate permutations of length LEN given a list of N Items?

Note: I'm working in python on this. For example, given a list: list = ['a','b','c','d','e','f','g','h','i','j'] I want to generate a list of lists with all possible 3-item combinations: ['a','b','c'], ['a','b','d'], ['a','b','e'] The permutations…
Odj fourth
  • 649
  • 1
  • 9
  • 16
32
votes
6 answers

How to generate all permutations of an array in sorted order?

I have an array, and the user can insert a string. And I have this code: int main(){ char anagrama[13]; cin >> anagrama; for(int j = 0; j < strlen(anagrama); j++){ cout << anagrama[j]; for(int k = 0; k < strlen(anagrama); k++){ …
Code Geas Coder
  • 1,839
  • 4
  • 23
  • 29
31
votes
6 answers

Secret Santa - Generating 'valid' permutations

My friends invited me home to play the game of Secret Santa, where we are supposed to draw a lot & play the role of 'Santa' for a friend in the group. So, we write all our names and pick a name randomly. If any of us ends up having their own name…
fritz
  • 698
  • 6
  • 12
31
votes
2 answers

How to re-order units based on their degree of desirable neighborhood ? (in Processing)

I would need help to implement an algorithm allowing the generation of building plans, that I've recently stumbled on while reading Professor Kostas Terzidis' latest publication: Permutation Design: Buildings, Texts and Contexts…
solub
  • 1,291
  • 17
  • 40
30
votes
6 answers

Generate permutations of list with repeated elements

In Python, it is quite simple to produce all permutations of a list using the itertools module. I have a situation where the sequence I'm using only has two characters (i.e. '1122'). I want to generate all unique permutations. For the string '1122',…
JoshD
  • 12,490
  • 3
  • 42
  • 53
30
votes
10 answers

How would you calculate all possible permutations of 0 through N iteratively?

I need to calculate permutations iteratively. The method signature looks like: int[][] permute(int n) For n = 3 for example, the return value would be: [[0,1,2], [0,2,1], [1,0,2], [1,2,0], [2,0,1], [2,1,0]] How would you go about doing this…
Bob Aman
  • 32,839
  • 9
  • 71
  • 95
30
votes
6 answers

how to generate permutations of array in python?

i have an array of 27 elements,and i don't want to generate all permutations of array (27!) i need 5000 randomly choosed permutations,any tip will be useful...
user257522
  • 525
  • 2
  • 5
  • 7
29
votes
3 answers

How to generate a random permutation in Java?

What is the best way to generate a random permutation of n numbers? For example, say I have a set of numbers 1, 2 and 3 (n = 3) Set of all possible permutations: {123, 132, 213, 231, 312, 321} Now, how do I generate: one of the elements of the…
Shankar Raju
  • 4,356
  • 6
  • 33
  • 52
29
votes
13 answers

How do you efficiently generate a list of K non-repeating integers between 0 and an upper bound N

The question gives all necessary data: what is an efficient algorithm to generate a sequence of K non-repeating integers within a given interval [0,N-1]. The trivial algorithm (generating random numbers and, before adding them to the sequence,…
tucuxi
  • 17,561
  • 2
  • 43
  • 74