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
16
votes
7 answers

Generate a random derangement of a list

How can I randomly shuffle a list so that none of the elements remains in its original position? In other words, given a list A with distinct elements, I'd like to generate a permutation B of it so that this permutation is random and for each n,…
georg
  • 211,518
  • 52
  • 313
  • 390
16
votes
8 answers

Generate equation with the result value closest to the requested one, have speed problems

I am writing some quiz game and need computer to solve 1 game in the quiz if players fail to solve it. Given data : List of 6 numbers to use, for example 4, 8, 6, 2, 15, 50. Targeted value, where 0 < value < 1000, for example 590. Available…
Saša Šijak
  • 8,717
  • 5
  • 47
  • 82
16
votes
7 answers

Smart way to generate permutation and combination of String

String database[] = {'a', 'b', 'c'}; I would like to generate the following strings sequence, based on given database. a b c aa ab ac ba bb bc ca cb cc aaa ... I can only think of a pretty "dummy" solution. public class JavaApplication21 { …
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
16
votes
7 answers

Good hash function for permutations?

I have got numbers in a specific range (usually from 0 to about 1000). An algorithm selects some numbers from this range (about 3 to 10 numbers). This selection is done quite often, and I need to check if a permutation of the chosen numbers has…
martinus
  • 17,736
  • 15
  • 72
  • 92
16
votes
2 answers

algorithm that will take numbers or words and find all possible combinations

I am looking for an algorithm that will take numbers or words and find all possible variations of them together and also let me define how many values to look for together. Example lets say the string or array is: cat dog fish then the…
JasonDavis
  • 48,204
  • 100
  • 318
  • 537
16
votes
7 answers

Random sequence iteration in O(1) memory?

Say you want to iterate over a sequence [0 to n] in a random order, visiting every element exactly once. Is there any way to do this in O(1) memory, i.e. without creating an [1..n] sequence with std::iota and running it through…
4ZM
  • 1,463
  • 1
  • 11
  • 21
16
votes
6 answers

C move memory parts inplace

I am implementing several datastructures and one primitive I want to use is the following: I have a memory chunk A[N] (it has a variable length, but I take 100 for my examples) and inside this chunk, there is a smaller part C of length K (lets say…
Lykos
  • 1,039
  • 6
  • 25
16
votes
7 answers

permutations of a string using iteration

I'm trying to find permutation of a given string, but I want to use iteration. The recursive solution I found online and I do understand it, but converting it to an iterative solution is really not working out. Below I have attached my code. I would…
ueg1990
  • 1,003
  • 2
  • 19
  • 39
15
votes
1 answer

Efficiently determine "how sorted" a list is, eg. Levenshtein distance

I'm doing some research on ranking algorithms, and would like to, given a sorted list and some permutation of that list, calculate some distance between the two permutations. For the case of the Levenshtein distance, this corresponds to calculating…
15
votes
4 answers

Using R to solve the Lucky 26 game

I am trying to show my son how coding can be used to solve a problem posed by a game as well as seeing how R handles big data. The game in question is called "Lucky 26". In this game numbers (1-12 with no duplicates) are positioned on 12 points on a…
DesertProject
  • 213
  • 1
  • 4
15
votes
8 answers

Generating Permutations using LINQ

I have a set of products that must be scheduled. There are P products each indexed from 1 to P. Each product can be scheduled into a time period 0 to T. I need to construct all permutations of product schedules that satisfy the following…
cordialgerm
  • 8,403
  • 5
  • 31
  • 47
15
votes
9 answers

Print all permutation in lexicographic order

I want to print all permutation of string in lexicographic order. I wrote this code: void permute(char *a, int i, int n) { if (i == (n-1)) printf("\"%s\"\n", a); else { for (int j = i; j < n; j++) { swap((a+i), (a+j)); …
lorantalas
  • 284
  • 1
  • 3
  • 14
15
votes
7 answers

Calculating permutations in F#

Inspired by this question and answer, how do I create a generic permutations algorithm in F#? Google doesn't give any useful answers to this. EDIT: I provide my best answer below, but I suspect that Tomas's is better (certainly shorter!)
Benjol
  • 63,995
  • 54
  • 186
  • 268
15
votes
2 answers

Please explain this algorithm to get all permutations of a String

The following code generates all the permutations for a string: def permutations(word): if len(word)<=1: return [word] #get all permutations of length N-1 perms=permutations(word[1:]) char=word[0] result=[] #iterate…
gran_profaci
  • 8,087
  • 15
  • 66
  • 99
15
votes
14 answers

Python recursion permutations

I'm having trouble trying to make a permutation code with recursion. This is suppose to return a list back to the use with all the possible position for each letter. For the word cat, it is suppose to return ['cat','act',atc,'cta','tca','tac']. So…
brian Chiem
  • 235
  • 1
  • 6
  • 13