# Questions tagged [permutation]

1442 questions

1

votes

3

answer

556

Views

### Permutations of English Alphabet of a given length

So I have this code. Not sure if it works because the runtime for the program is still continuing.
void permute(std::vector& wordsVector, std::string prefix, int length, std::string alphabet) {
if (length == 0) {
//end the recursion
wordsVector.push_back(prefix);
}
else {
for (int i = 0; i < alp...

1

votes

2

answer

182

Views

### R: combinatorics, number of permutations for one combination

I have three different events (1,2,3) with different probabilities (0.15, 0.76, 0.09) and I would like to draw 5 times with replacement.
I can now determine the number of possible combinations using
nsimplex(3,5) ### =21
from the combinat-package.
And I can determine the probabilities of each combi...

-2

votes

3

answer

34

Views

### Permutations of a series of coloured balls, without 2 of the same colour next to each other?

Input is a string (a, b, c, d,...). In this string, a means a balls of the same color, b means b balls of another color, etc. So (2, 1, 3) would mean 2 red balls, 1 blue ball and 3 yellow balls.
Output 1 is the number of possible permutations, without having 2 balls of the same color next to each ot...

0

votes

0

answer

11

Views

### Knuth Permutation Vector

I've been coding on and off for 3 months now and tend to draft out my code by hand before I implement it, so please go easy on me. ;-)
here's a link for reference.
The function in question is ‘solutions’
The print function is just an iterator I use to print vectors for testing.
My full project...

1

votes

1

answer

35

Views

### PHP: get permutations of nested array with recursion

I have the nested array structure below.
$letters = array(
$A = array(1,2),
$B = array(3,4),
$C = array(5,6)
);
My goal is to find all possible permutations of the numbers, while the letters must keep the order A-B-C. The expected output is:
1-3-5
1-3-6
1-4-5
1-4-6
2-3-5
2-3-6
2-4-5
2-4-6
Of course,...

1

votes

1

answer

36

Views

### Unique Sets Permutations R [duplicate]

This question already has an answer here:
R: Permutations and combinations with/without replacement and for distinct/non-distinct items/multiset
2 answers
Is there a way to generate all the unique sets of the following permutations, where I am able to change N and R easily.
library(gtools)
x

1

votes

0

answer

10

Views

### check for permutation in Scheme

I need to write a function in a scheme that takes two lists and returns true if one list is a permutation of the other.
For example
(permutation '(3 4 7) '(7 3 4)) would return #t
(permutation '(3 4 7) '(c 3 4 5)) would return #f
This is the code I got so far but I'm stuck:
(define (permutation l...

11

votes

3

answer

166

Views

### Algorithm to mix colours on 7 individual pieces of toy

I'm a woodworker trying to seek some math and algorithm help on the expertise here.
I'm trying to make 28 sets of Tangram for gifting relatives, like this:
DanielHolth + RobotE at nl:Wp [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)], from Wikimedia Commons
The toys consists of 7 pie...

1

votes

2

answer

107

Views

### Mapping unique combinations to numbers

I am trying to come up with a solution to a problem I thought of. I have the number of permutations of 26 characters with 6 possible spots as 26^6 = 308 915 776. I was trying to make a way so that I could map each number to a unique combination and be able to go back and forth from combination to nu...

1

votes

2

answer

636

Views

### Find unique combination of identifiers in a comma-delimited list

I've got a complicated query whose results I have stored in a temporary table for the time being for simplicity's sake. I've got a comma=delimited list of certain combinations of three character unique identifiers such as the screen cap below:
You can see in rows 2 and 3 that the 3 character identi...

1

votes

3

answer

374

Views

### How to get all permutations of sizes {n, n-1,n-2, … 1} from list of size n efficiently?

I am trying to find all permutations from a list that are the same size or smaller than the list.
For example:
>>>allPermutations([a,b])
[[a,b], [b,a], [a], [b]]
This is the iterative code I currently have in python. I'm not sure how efficient it currently is.
import itertools
def getAllPossibleSub...

1

votes

1

answer

97

Views

### Finding and storing combinations

I have a n-digit number composed of 1's only. I want to replace 1's with 0's in all possible combinations and store the combinations in an array.
How do I find all combinations?
I was thinking of starting with one zero and then increasing the number if zeroes that will replace 1's.
If there are 2 ze...

1

votes

1

answer

947

Views

### generating binary numbers of size n as tuples : itertools.product(*[(0, 1)] * n)

I just found this instruction
itertools.product(*[(0, 1)] * n)
posted by PAG.
Can someone explain how it works?
I am trying to find a way of doing permutations without repetition of n tuples in 3 bags
and I only can use itertools if I want. Thanks

1

votes

1

answer

173

Views

### Format of a permutation of a python dictionary

Hi I have this code...
x = {'stack': ['2', '3'], 'overflow': ['1', '2']}
for i in x.values():
heroes = {x[0]:x[1:] for x in permutations(i)}
print heroes
This gives me...
{'3': ('2',), '2': ('3',)}
{'1': ('2',), '2': ('1',)}
Later on in my program I need the values of the keys to be in list form, no...

1

votes

2

answer

2.4k

Views

### create something like a for in swi prolog

i want to create something like a for in swi prolog
for(int i=1;i

1

votes

1

answer

62

Views

### Generating every single combination from a few hashes

I have a few arrays, I'll just say four. For instance:
LOCATION = [:new_york, :california, :alaska, :missouri, :mississippi, :florida]
TYPE = [:high_rise, :mid_rise, :low_rise, :loft, :townhouse, :hotel]
OWNERSHIP = [:coop, :condo, :condop, :rental, :single_family, :multi_family]
BLDG_AGE = [...

1

votes

2

answer

154

Views

### Permutation with redundant overlaps? Python

I used itertools to run a permutation on a list that I have.
mylist = [a, b, c, d, e, f]
mypermutations = itertools.permutations(mylist,2)
mypermutations_list = list(mypermutations)
print mypermutations_list
prints:
[(a, b), (a, c), (a, d)...]
However, the permutation list doesn't include (a, a...

1

votes

1

answer

338

Views

### Haskell Data.List permutations of functions

How to fix the permutation error on a list of functions?
> :m + Data.List
> permutations [(+1),(-2),(*3)]
No instance for (Num a0) arising from a use of `+'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential ins...

1

votes

2

answer

347

Views

### How does this recursive code to find all permutations of a string work?

I am doing Project Euler problems. I am currently working on the circular primes problem
The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.
There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and...

1

votes

2

answer

911

Views

### non-recursive version Permutations

I have two functions, prod and perm. They are very similar.Both of them using the recursive.Now I wan't replace recursion with for loop. prod2 worked correct, but perm2 doesn't, How can I fix it?
#Recursive version:
def prod(A,k):
return [[]] if k==0 else [[a]+b for a in A for b in prod(A,k-1)]
def...

1

votes

1

answer

171

Views

### J-Language Functions similar to the Anagram Index (A.)

Are there really simple ways to accomplish something similar to the Anagram Index (A.) functions on an "n choose k" permutation in J?
So, I love A. in J. Just to show that I understand it slightly:
A. 0 3 4 2 1
17
17 A. i.5
0 3 4 2 1
I also know that I can use it to permute a list (17 A. things) or...

1

votes

2

answer

112

Views

### Program to list permutations of carbohydrate chains

I was tasked with creating a program that takes a hydrocarbon molecule, e.g. Pentene, C5H10, and outputs, preferably graphically, all of the possible permutations of its molecular chain. I'm not asking for it to be written for me (in fact, please don't), but could someone give me a good starting poi...

1

votes

2

answer

1.9k

Views

### Random Permutation Generation

I am having a hard time understand this code example out of my text book. I hope someone can better explain a part of this code then my book. I am sorry for the lack of comments in this program so I will try my best to be very specific in what I am asking. Ok has the title suggest this program is a...

1

votes

2

answer

158

Views

### (Java) Permutation of N Lists with flushing to Disk

I'm referring to this question/answer: Permutation of N Lists (permutation of N lists to create all combinations of a search space).
I implemented this for my case but having the problem to run out of memory as I work with a huge search space (currently millions or billions of combinations, increasi...

1

votes

2

answer

281

Views

### Counting existing permutations in R

I have a large dataset with columns IDNum, Var1, Var2, Var3, Var4, Var5, Var6. The variables are boolean with value either 0 or 1. Each row could be one of 64 different possible permutations. I would like to count the number of rows corresponding to each permutation present. Is there an efficient wa...

1

votes

3

answer

1k

Views

### How to get all permutations of letters as well as case for a string in python? [duplicate]

This question already has an answer here:
How to generate all permutations of a list in Python
29 answers
Finding all possible case permutations in Python
1 answer
How to get all possible combination of cases as well as letters for a string in python?
Example
input:'ab' output:aB', 'Ba', 'BA',...

1

votes

1

answer

200

Views

### Number of permutations between strings

For the word "BOOKKEEPER". It's sorted representation is "BEEEKKOOPR" How can I find the different permutations of the word between "BEEEKKOOPR" and "BOOKKEEPER"?
similar example:
For "BBAA"
AABB - 1
ABAB - 2
ABBA - 3
BAAB - 4
BABA - 5
BBAA - 6
6 - 1 = 5 So there are 5 words before "BBAA"
"BEEEKKOO...

1

votes

2

answer

1.3k

Views

### Find all permutations of a 4-letter string that starts with “j” and includes “w”?

I need to find all possible 4-letter combinations that start with "j" and have "w". I can imagine doing it with nested loops, but am sure there is a better way to achieve it with array combination or permutation.
To clarify - I was after permutations (characters can be repeated), as long as it satis...

1

votes

3

answer

965

Views

### Permutations of list of lists

Supposing there is a list of list of elements. Where each list can have any no of elements. For example [[1,2,3,4],[2,3],[4,5,6,7],[1]] . I am trying to generate permutations of such lists possible where I am supposed to select only one from the innermost lists in one such permutation. So the output...

1

votes

1

answer

875

Views

### Permutating lists too large for RAM in Python

I have written a program to read a list of words from a text file (one word per line) and combine them to produce every permutation of 3 words before writing an output file of the permutations, again one per line.
import itertools
wordList = open("wordlist.txt", "r").readlines() # import words into...

1

votes

3

answer

93

Views

### Aggregating data based on unique triads in R

I was referred here Counting existing permutations in R
for previous related question but I can't apply it to my problem. Here is the data I have
One

1

votes

2

answer

389

Views

### Most efficient way to get all permutations in c++

I am trying to calculate a lot of combinations in C++. I came up with the following implement by myself, but its efficiency is not satisfactory. It takes more than 3 seconds to get C-18-2 (every 2 combination of 18), I believe this can be done in much less time.
vector Mytool::combo2(int len){
Matri...

1

votes

2

answer

152

Views

### SQL - combining multiple columns into 1 string in all possible unique permutations [closed]

I have the following table:
AddressId SubBuildingName BuildingName BuildingNumber Postcode
12345 Flat 1 SomeBuilding 80 MK34 1PU
12346 Flat 2 SomeBuilding 80 MK34 1PU
12347 Flat 7 OtherBuilding...

1

votes

3

answer

3.1k

Views

### Java recursion to find all the possible permutations & combinations in Arraylist quits too early

I'm writing a recursive permutations program in Java to find all the possible permutation of the Strings in an Arraylist.
I know there are a lot of posts similar to this, but I haven't found one that addresses this issue specifically.
The problem I have with my code is that it only runs the first...

1

votes

2

answer

66

Views

### How would I append all the combinations in a list?

var combinations = function(numArr, choose, callback) {
var n = numArr.length;
var c = [];
var inner = function(start, choose_) {
if (choose_ == 0) {
callback(c);
} else {
for (var i = start; i

1

votes

2

answer

142

Views

### How to get all possible combinations of two different lists?

I'm having so much trouble understanding my issue:
I got 2 lists:
from = ['A', 'B', 'C']
to = ['D', 'E', 'F']
I need to produce a matrix that combines each item from one list to the other as such:
final = [[['A', 'D'], ['B', 'E'], ['C', 'F']],
[['A', 'D'], ['B', 'F'], ['C', 'E']],
[['A', 'E'], ['B',...

1

votes

2

answer

488

Views

### Generate a truth table of arbitrary length haskell

for an assignment I have to generate a truth table like this:
combinations :: Int -> [[Bool]]
combinations 3 should output:
[[False, False, False],[False, False, True],[False, True, False],[False, True, True][True, False, False][True, False, True],[True, True, False],[True, True, True]]
I can do a l...

1

votes

2

answer

79

Views

### Fastest way to get pairs from a list ordered by their distance in the list

Given a range(n) what would be the fastest way to get pairs from the range that are ordered by their respective distance from each in the list i.e. for two elements in a list A and B the distance is abs(A-B).
This is the implementation I came up with:
sorted(combinations(range(n), 2), key=lambda a:...

1

votes

4

answer

117

Views

### Sorting permutation efficiently

Given an unsorted permutation of [n], I want to collect the numbers by iterating from left to right in order to sort the premutation (1...n).
What is the number of iteration I have to do in order to acheieve this goal?
For example:
Given '3, 7, 4, 2, 10, 8, 9, 1, 6, 5'- the number of iterations is 6...

1

votes

1

answer

480

Views

### Create all combinations of array children

I know this question popped up alot of times here. I did search before but didn't find anything that fits.
I'm sitting here since like 5 hours and my brain is not getting me anywhere at the moment. D:
I've got an array:
[rahmenfarbe] => Array
(
[0] => Graphite
[1] => Aluminium
[2] => Smoke
)
[ruecke...