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

All possible ways to interleave two strings

I am trying to generate all possible ways to interleave any two arbitrary strings in Python. For example: If the two strings are 'ab' and 'cd', the output I wish to get is: ['abcd', 'acbd', 'acdb', 'cabd', 'cadb', 'cdab'] See a is always before b…
Seb
  • 481
  • 2
  • 6
  • 18
19
votes
8 answers

Print all the permutations of a string in C

I am learning backtracking and recursion and I am stuck at an algorithm for printing all the permutations of a string. I solved it using the bell algorithm for permutation but I am not able to understand the recursion method. I searched the web and…
poorvank
  • 7,524
  • 19
  • 60
  • 102
19
votes
2 answers

Python itertools permutations how to include repeating characters

I have some code like: def perm(n,i): b = 0 while b < n: n= n -1 from itertools import permutations as p file.write('\n'.join([''.join(item) for item in p(i,n)])) perm(4,'0123') which produces output…
jkdba
  • 2,378
  • 3
  • 23
  • 33
19
votes
9 answers

Solving Puzzle in Python

I got one puzzle and I want to solve it using Python. Puzzle: A merchant has a 40 kg weight which he used in his shop. Once, it fell from his hands and was broken into 4 pieces. But surprisingly, now he can weigh any weight between 1 kg to 40…
Abid Rahman K
  • 51,886
  • 31
  • 146
  • 157
18
votes
1 answer

Provably correct permutation in less than O(n^2)

Written in Haskell, here is the data type that proves that one list is a permutation of another: data Belongs (x :: k) (ys :: [k]) (zs :: [k]) where BelongsHere :: Belongs x xs (x ': xs) BelongsThere :: Belongs x xs xys -> Belongs x (y ': xs) (y…
18
votes
10 answers

Calculate all permutations of a string in Swift

For the string "ABC" the code snippet below calculates 5 of the 6 total permutations. My strategy was to insert each character at each index possible index. But the function never gets "CBA" as a possible permutation. What am I missing? var…
kiaraRobles
  • 587
  • 1
  • 4
  • 14
18
votes
4 answers

Efficiently determine the parity of a permutation

I have an int[] array of length N containing the values 0, 1, 2, .... (N-1), i.e. it represents a permutation of integer indexes. What's the most efficient way to determine if the permutation has odd or even parity? (I'm particularly keen to avoid…
mikera
  • 105,238
  • 25
  • 256
  • 415
18
votes
5 answers

Interleave array in constant space

Suppose we have an array a1, a2,... , an, b1, b2, ..., bn. The goal is to change this array to a1, b1, a2, b2, ..., an, bn in O(n) time and in O(1) space. In other words, we need a linear-time algorithm to modify the array in place, with no more…
Matt
  • 199
  • 1
  • 3
  • 5
17
votes
6 answers

Generate all permutations of all lengths

How would you generate all the possible permutations of list b(1,6,8,3,9,5) including ones of different length? Example: List a = [1,2,3] generateperms(a) 1,2,3 3,1,2 3,2,1 1,3,2 2,1,3 2,3,1 2,3 1,2 1,3 2,1 3,2 3,1 And so forth and getting all the…
a sandwhich
  • 4,352
  • 12
  • 41
  • 62
17
votes
7 answers

Algorithm to count the number of valid blocks in a permutation

Possible Duplicate: Finding sorted sub-sequences in a permutation Given an array A which holds a permutation of 1,2,...,n. A sub-block A[i..j] of an array A is called a valid block if all the numbers appearing in A[i..j] are consecutive numbers…
inexorable
  • 171
  • 4
17
votes
7 answers

Generating all possible permutations of a list recursively

I'm trying to recursively generate all items in a list recursively. I've seen a few solutions to similar questions to this, but I haven't been able to get my code to work. Could someone point out how I can fix my code? This is open to all S/O'ers,…
varatis
  • 14,494
  • 23
  • 71
  • 114
16
votes
3 answers

Optimizing (minimizing) the number of lines in file: an optimization problem in line with permutations and agenda scheduling

I have a calendar, typically a csv file containing a number of lines. Each line corresponds to an individual and is a sequence of consecutive values '0' and '1' where '0' refers to an empty time slot and '1' to an occupied slot. There cannot be two…
16
votes
5 answers

Algorithm to find isomorphic set of permutations

I have an array of set of permutations, and I want to remove isomorphic permutations. We have S sets of permutations, where each set contain K permutations, and each permutation is represented as and array of N elements. I'm currently saving it as…
Chan Kha Vu
  • 9,834
  • 6
  • 32
  • 64
16
votes
3 answers

Heap's algorithm permutation generator

I need to iterate over permutations of a tuple of integers. The order has to be generated by swapping a pair of elements at each step. I found the Wikipedia article (http://en.wikipedia.org/wiki/Heap%27s_algorithm) for Heap's algorithm, which is…
EvenAdam
  • 313
  • 1
  • 2
  • 7
16
votes
4 answers

Why is the Big-O of this algorithm N^2*log N

Fill array a from a[0] to a[n-1]: generate random numbers until you get one that is not already in the previous indexes. This is my implementation: public static int[] first(int n) { int[] a = new int[n]; int count = 0; while (count !=…
btrballin
  • 1,420
  • 3
  • 25
  • 41