Click here to Skip to main content
15,887,214 members
Home / Discussions / C#
   

C#

 
GeneralRe: Capturing key presses or key strokes Pin
Anthony Mushrow23-Oct-09 16:54
professionalAnthony Mushrow23-Oct-09 16:54 
AnswerRe: Capturing key presses or key strokes Pin
Henry Minute23-Oct-09 12:38
Henry Minute23-Oct-09 12:38 
GeneralRe: Capturing key presses or key strokes Pin
Rajesh R Subramanian23-Oct-09 23:10
professionalRajesh R Subramanian23-Oct-09 23:10 
GeneralRe: Capturing key presses or key strokes Pin
Henry Minute25-Oct-09 1:10
Henry Minute25-Oct-09 1:10 
GeneralRe: Capturing key presses or key strokes Pin
Rajesh R Subramanian25-Oct-09 1:55
professionalRajesh R Subramanian25-Oct-09 1:55 
GeneralRe: Capturing key presses or key strokes Pin
Henry Minute25-Oct-09 4:40
Henry Minute25-Oct-09 4:40 
AnswerRe: Capturing key presses or key strokes Pin
Rajesh R Subramanian23-Oct-09 23:06
professionalRajesh R Subramanian23-Oct-09 23:06 
QuestionSending a command to an IEnumerable method Pin
Lutosław23-Oct-09 9:13
Lutosław23-Oct-09 9:13 
Hi!
I am not sure how to express what I need. However...

I've got a code which generates all permutations of an array of numbers 0 to n-1, which can be used to generate permutations of any array, since every element can be mapped to it's index and vice versa.

Here it is:
public IEnumerable<int[]> GetPermutations()
{
    if (n == 1)
    {
        yield return indexes;
        yield break;
    }
    var generator = new PermutationGenerator(n - 1); // recursion
    bool even = false;
    foreach (int[] permutation in generator.GetPermutations())
    {
        if (even)
            for (int i = 0; i < n; i++)
                yield return InsertN(permutation, i);
        else
            for (int i = n - 1; i >= 0; i--)
                yield return InsertN(permutation, i);
        even = !even;
    }
}
In an algorithm I am currently writing, sometimes it is good to skip some permutations. E.g. I have reached [12345678], and realize that I don't need any perm. which starts with [123.....], so I would like to skip 5! permutations and go in one step to [124.....]. It basically means that I would like to put a "return" in a given level of recursion. The code would look like this I suppose:
foreach (int[] permutation in GetPermutations())
{
    // (...)
    SkipPermutations(4);
    // (...)
}
Of course I will not be satisfied with a solution like running empty for loop to skip a number of permutations.

The problem here is recursion. I have inspected an Enumerator class generated by a compiler using Reflector and it was very complicated. Any Ideas how to achieve it a simple and elegant way?

Thanks in advance --

Greetings - Jacek

AnswerRe: Sending a command to an IEnumerable method Pin
Ian Shlasko23-Oct-09 9:55
Ian Shlasko23-Oct-09 9:55 
GeneralRe: Sending a command to an IEnumerable method Pin
Lutosław23-Oct-09 10:27
Lutosław23-Oct-09 10:27 
GeneralRe: Sending a command to an IEnumerable method Pin
Ian Shlasko23-Oct-09 10:38
Ian Shlasko23-Oct-09 10:38 
GeneralRe: Sending a command to an IEnumerable method Pin
Lutosław24-Oct-09 1:01
Lutosław24-Oct-09 1:01 
AnswerRe: Sending a command to an IEnumerable method [modified] Pin
harold aptroot23-Oct-09 10:08
harold aptroot23-Oct-09 10:08 
GeneralRe: Sending a command to an IEnumerable method Pin
Ian Shlasko23-Oct-09 10:33
Ian Shlasko23-Oct-09 10:33 
GeneralRe: Sending a command to an IEnumerable method Pin
harold aptroot23-Oct-09 10:35
harold aptroot23-Oct-09 10:35 
GeneralRe: Sending a command to an IEnumerable method Pin
Ian Shlasko23-Oct-09 10:50
Ian Shlasko23-Oct-09 10:50 
GeneralRe: Sending a command to an IEnumerable method Pin
harold aptroot23-Oct-09 10:55
harold aptroot23-Oct-09 10:55 
GeneralRe: Sending a command to an IEnumerable method Pin
Ian Shlasko23-Oct-09 11:13
Ian Shlasko23-Oct-09 11:13 
GeneralRe: Sending a command to an IEnumerable method Pin
harold aptroot23-Oct-09 11:19
harold aptroot23-Oct-09 11:19 
GeneralRe: Sending a command to an IEnumerable method Pin
Ian Shlasko23-Oct-09 12:12
Ian Shlasko23-Oct-09 12:12 
GeneralRe: Sending a command to an IEnumerable method Pin
harold aptroot23-Oct-09 12:23
harold aptroot23-Oct-09 12:23 
GeneralRe: Sending a command to an IEnumerable method Pin
Ian Shlasko23-Oct-09 12:27
Ian Shlasko23-Oct-09 12:27 
GeneralRe: Sending a command to an IEnumerable method Pin
Lutosław23-Oct-09 23:55
Lutosław23-Oct-09 23:55 
GeneralRe: Sending a command to an IEnumerable method Pin
Lutosław24-Oct-09 1:18
Lutosław24-Oct-09 1:18 
GeneralRe: Sending a command to an IEnumerable method Pin
Lutosław24-Oct-09 1:04
Lutosław24-Oct-09 1:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.