permutation

Is it possible to output the permutations of 1,...,n using only iterators?

Here a couple of examples in a pseudocode to show what I mean. This produces the combinations (selections disregarding order without repetition) of 1,...,n taking 3 at a time. Do[Print[i,j,k], {i,1...n-2}, {j,i+1...n-1}, {k,j+1...n}] The loop works from left to right---for each i, the iterator j will go through its values and for ea...

Permutation generator function F#

I need to generate a list of all distinct permutations of 1..n x 1..n where teh first value does not equal the second (i.e. generate 3 -> [(3,2):: (3,1):: (2,3) ::(2,1)::(1,3)::(1,2)] the exact scenario is you have a pool of objects(cards) and one is dealt to each player. If a player is dealt a card, no other player can be dealt that ca...

c# ordered combinations algorithm

Hi, I'm trying to develop a c# application that will generate a list of all possible permutations, within a limit, and cost. For example, I have a list of 80 jobs. Each job has a value (1-5) (typically 3) and each engineer has a limit of how much they can do, typically a value of 20. At the moment I've started by producing a list of ...

Calculating voting power indexes in R

Dear Coding Experts I have a project in which i need to be able to calculate different voting power indexes in R. As a first attempt at this I wrote a small function to calculate the banzhaf index. It takes two arguments, a dataframe that has two columns which must be labelled member and vote, and how many votes are needed for a majorit...

Why does Perl's Math::Combinatorics complain "must use next_permutation of 'frequency' argument not passed to constructor"?

I'm trying to generate unique permutations of an array using Math::Combinatorics. As CPAN page says it can be done using next_string(): use Math::Combinatorics; my @arr = [1,1,1,0,0]; $c = Math::Combinatorics->new( count=>5, data=>[\@arr], frequency=>[3,2] ); while (@permu = $c->next_string()){ print "@permu\n"; } However this cod...

C# Algorithm that Re-arranges Chars in a String

I would like a C# algorithm that re-arranges the chars in a string that is dynamic in length. Having trouble finding one and I know there has to be one out there. The algorithm has to realign elements to form new strings in all possible combinations. For instance, "cat" would produce the following: cat cta tca tac act atc ...

Permutation for numbers in C

Hi programming masses, I'm trying to write a C function to list all permutations of a set of numbers, in groups of five, including repeat numbers: 15-11-49-43-5 2-30-34-6-11 So it's easy enough to write a function to grab all permutations of a number set and throw them out, but mapped to a certain group size, i'm somewhat stuck.. T...

Python - Speed up generation of permutations of a list (and process of checking if permuations in Dict)

I need a faster way to generate all permutations of a list, then check if each one is in a dictionary. for x in range (max_combo_len, 0, -1): possible_combos = [] permutations = list(itertools.permutations(bag,x)) for item in permutations: possible_combos.append(" ".join(item))...

Generating permutations of NSArray elements

Let's say I have an NSArray of NSNumbers like this: 1, 2, 3 Then the set of all possible permutations would look something like this: 1, 2, 3 1, 3, 2 2, 1, 3 2, 3, 1 3, 1, 2 3, 2, 1 What's a good way to do this in XCode? ...

What kind of algorithm do I need?

I'm trying to figure out all the different ways I can create groups of 4 from 6 objects using objective-c. For example, if I had the following objects: a, b, c, d, e, f Then I could create groups like a, b, c, d b, c, d, e a, d, e, f and so on. Order doesn't matter. If I wanted to figure out all the different possibilities, what ki...

Does Repeating a Biased Random Shuffle Reduce the Bias?

I'd like to produce fast random shuffles repeatedly with minimal bias. It's known that the Fisher-Yates shuffle is unbiased as long as the underlying random number generator (RNG) is unbiased. To shuffle an array a of n elements: for i from n − 1 downto 1 do j ← random integer with 0 ≤ j ≤ i exchange a[j] and a[i] But...

Generating all 5 card poker hands

This problem sounds simple at first glance, but turns out to be a lot more complicated than it seems. It's got me stumped for the moment. There are 52c5 = 2,598,960 ways to choose 5 cards from a 52 card deck. However, since suits are interchangeable in poker, many of these are equivalent - the hand 2H 2C 3H 3S 4D is equivalent to 2D 2S ...

Algorithm to generate all permutation by selecting some or all charaters

Hello All, I need to generate all permutation of a string with selecting some of the elements. Like if my string is "abc" output would be { a,b,c,ab,ba,ac,ca,bc,cb,abc,acb,bac,bca,cab,cba }. I thought a basic algorithm in which I generate all possible combination of "abc" which are {a,b,c,ab,ac,bc,abc} and then permute all of them. S...

Generating permutations using bash

is it possible to write a bash script that can read in each line from a file and generate permutations for each? Using awk / perl is fine. File ---- ab abc Output ------ ab ba abc acb bac bca cab cba ...

Optimizing a cycle sort implementation

Cycle sort is an in-place sort based on the idea that the permutation you're sorting can be factored into cycles. If you rotate each cycle one position, the array will be sorted. This can easily be coded so that the number of writes to the array is the theoretical minimum needed for any in-place sort (which is nice for huge data sets on,...

Generate a list of length n with m possible elements

Hi there, I need to generate a ton of lists in Python. Every list is of length 13, and I have 4 possible values that can go into each element. These are [1, -1, i, -i], but it could be whatever. Thus I should get 4 * 4 * 4 ... * 4 = 4^13 = 67,108,864 lists, or more generally, m^n, given the info in the subject. I tried the combination...

Problem related to finding permuations and combinations using Python

I have 2 variables - a and b. I need to fill up k places using these variables. So if k = 3 output should be [a,a,a], [a,a,b] , [a,b,a], [b,a,a], [a,b,b], [b,a,b], [b,b,a] and [b,b,b] Input - k Output - All the combinations How do I code this in Python? Can itertools be of any help here? ...

Algorithm for permutations of operators and operands

I came across this question on an interview website - We are given 4 numbers say n1, n2, n3, n4. We can place them in any order and we can use the mathematical operators +, -, *, / in between them to have the final result as 24. Write an algorithm for this - it will take 4 numbers and return false or true whether final result 24 is p...

Permutation with repetition without allocate memory

I'm looking for an algorithm to generate all permutations with repetition of 4 elements in list(length 2-1000). Java implementation The problem is that the algorithm from the link above alocates too much memory for calculation. It creates an array with length of all possible combination. E.g 4^1000 for my example. So i got heap space e...

Max size in a connected by prior Oracle

Hi, I've got some help turning my table of the sort: Col 23 25 15 53 ... into something like 23,25,15,53... The query that does it is SELECT max(ltrim(sys_connect_by_path(flow_run_id, ','), ',')) FROM (select flow_run_id, rownum rn from table where CREATED_DATE < sysdate - 32 and flow_id = 3 order by 1 desc) STA...