permutation

Is there an expand.grid like function in R, returning permutations ?

to become more specific, here is an example: > expand.grid(5, 5, c(1:4,6),c(1:4,6)) Var1 Var2 Var3 Var4 1 5 5 1 1 2 5 5 2 1 3 5 5 3 1 4 5 5 4 1 5 5 5 6 1 6 5 5 1 2 7 5 5 2 2 8 5 5 3 2 9 5 5 4 2 10 5 5 6 2 1...

Strings Generation

Hi,I would like to know how can I create various string from some given characters eg: given characters: a, b I would like to generate the following strings: aa ab ba bb What I have thought of is having (for 2 inputs only) two for-loops one inside another, and then loop each to the number of inputs which in this case is 2 and the ou...

Permutations of Varying Size

I'm trying to write a function in PHP that gets all permutations of all possible sizes. I think an example would be the best way to start off: $my_array = array(1,1,2,3); Possible permutations of varying size: 1 1 // * See Note 2 3 1,1 1,2 1,3 // And so forth, for all the sets of size 2 1,1,2 1,1,3 1,2,1 // And so forth, for all the...

How to generate all variations with repetitions of a string?

I want to generate all variations with repetitions of a string in C++ and I'd highly prefer a non-recursive algorithm. I've come up with a recursive algorithm in the past but due to the complexity (r^n) I'd like to see an iterative approach. I'm quite surprised that I wasn't able to find a solution to this problem anywhere on the web or...

Reducing permutations of many nested loops

I decided to do an image to explain this better, I just want to check my thinking is ok, and that I can reduce total permutations by 75%: ...

Find all possible partitions of n elements with k-sized subsets, where two elements share same set only once

I have n elements that need to be partitioned into x sets, each set has to hold exactly k=4 elements. I need to find all possible partitions with the constraint that each pair of elements only shares the same set once. So if I start with [1 2 3 4] [5 6 7 8] [...], all consecutive partitions cannot hold e.g. [1 2 X X] or [X X 1 3]. sets...

Create text files of every combination of specific lines within a base text file.

Ok, so hopefully I can explain this in enough detail for somebody to be able to help me.. I am writing a program in C# that is supposed to take a text file and replace specific text, which happen to be names of files, and print a new text file for every single combination of the given filenames. The specific places to change the text of ...

Generating Balls in Boxes

Given two sorted vectors a and b, find all vectors which are sums of a and some permutation of b, and which are unique once sorted. You can create one of the sought vectors in the following way: Take vector a and a permutation of vector b. Sum them together so c[i]=a[i]+b[i]. Sort c. I'm interested in finding the set of b-permutatio...

random permutation

I would like to genrate a random permutation as fast as possible. The problem: The knuth shuffle which is O(n) involves generating n random numbers. Since generating random numbers is quite expensive. I would like to find an O(n) function involving a fixed O(1) amount of random numbers. I realize that this question has been asked before...

Knuth Permutation Algorithm Weird behaviour

Hi All, I have attached a code which gives weird outputs basing on cout statements. This program essentially computes the Knuth's Permutations. Input is say: run1 The code runs for first pass fine: Call trace will be: r un1 ur n1 nur 1 1nur n1ur nu1r nur1 After this run of code, the call returns correctly to step where urn...

generating permutations with repetitions in python

I know about itertools, but it seems it can only generate permutations without repetitions. for example, I'd like to generate all possible dice rolls for 2 dice. So I need all permutations of size 2 of [1, 2, 3, 4, 5, 6] including repetitions: (1, 1), (1, 2), (2, 1)... etc If possible I don't want to implement this from scratch ...

Regex to match 1234, 1324, 2341 (all permutations of {1,2,3,4})

I am implementing the following problem in ruby. Here's the pattern that I want : 1234, 1324, 1432, 1423, 2341 and so on i.e. the digits in the four digit number should be between [1-4] and should also be non-repetitive. to make you understand in a simple manner I take a two digit pattern and the solution should be : 12, 21 i.e. the...

C++ <algorithm> permutation

Why is this code note working (the code compiles and run fine, but is not actually showing the permutations): int main(int argc, char *argv[]) { long number; vector<long> interval; vector<long>::const_iterator it; cout << "Enter number: "; cin >> number; while(number-->0){ interval.push_back(number); ...

gwt define-property doesn't generate new compilation permutation

I added the following to myModule.gwt.xml. I expected it would generate 6*2 permutations when compile. However, it still only generated six permutation. Did I miss anything there? <define-property name="mobile.user.agent" values="mobile, not_mobile" /> <property-provider name="mobile.user.agent"><![CDATA[ { var ua = window.navigator.u...

combinations/permutations with no repeats across groupings

Hi all - I'm looking for C or Python code to implement either of the two pseudocode functions: function 1: list1 = [0,1,2] #any list of single-integer elements list2 = [0,3,4] list3 = [0,2,4] function1(list1, list2, list3) >>> (0,3,2),(0,3,4),(0,4,2),(1,0,2),(1,0,4),(1,3,0),(1,3,2),(1,3,4), (1,4,0),(1,4,2),(2,0,4),(2,3,0),(2,3,4...

I want to generate n! permutations for a given n in R.

suppose n=3 then output should be: a vector containing vectors: 123 213 132 231 321 ...

How do I generate all permutations of certain size with repetitions in Scheme?

I am learning Scheme and I am trying to generate permutations with repetitions of certain size. For example, given n=4 and set S = {a, b, c, d, e, f}, I'd like to generate all possible permutations: {a,a,a,a},{a,a,a,b},...,{a,a,a,f},{a,a,b,a},{a,a,b,b},...,{a,a,b,f},...{f,a,a,a},{f,a,a,b}...,{f,a,a,f},...{f,f,f,f}. The trouble is that ...

Algorithm to find all permutations of n characters

Possible Duplicates: Generating permutations lazily How to generate all permutations of a list in Python Algorithm to generate all possible permutations of a list? I didn't think this could be tough, but I'm having problem to come up with an algorithm to find all variations of n characters. We can assume each character onl...

Use next_permutation to permutate a vector of classes

Is it possible to use std::next_permutation() to permutate the elements of a vector of a class i created? How does the comparison parameter in next_permutation() work? ...

Generating permutations via templates

I'd like a function, or function object, that can generate a permutation of its inputs with the permutation specified at compile time. To be clear, I am not looking to generate all of the permutations, only a specific one. For instance, permute<1,4,3,2>( a, b, c, d ) would return (a,d,c,b). Obviously, it is straightforward to do this ...