-2

I want to "shuffle" in some way a Tuple<bool,int[]>[] but i dont need exactly shuffling i want to switch the place of the first one with the second one than switch the place of the first one with the third one i.e the first one should switch place with all the other tuples than the second one should do that. I will try to explain it with some simple number like in the lottery :

We have 1,2,3 let's assume that those are my 3 tuples now :

  • First we switch 1 with 2 and we get 2,1,3;
  • Second we switch 1 with 3 and we get 2,3,1;
  • Again we switch 1 with 2 and we get 1,3,2;
  • Again we switch 1 with 3 and we get 3,1,2;
  • Lastly we switch 1 with 2 and we get 3,2,1;

That's what i want to achieve with my tuple :

Tuple<bool, int[]>[] hodove =
        {
            new Tuple<bool, int[]>(Nadqsno(red, 1) && Napred(kolona, 2), new[]
            {
                count++,
                novMinatRed = red + 1,
                novaMinataKolona = kolona + 2
            }),
            new Tuple<bool, int[]>(Nadqsno(red, 2) && Napred(kolona, 1), new[]
            {
                count++,
                novMinatRed = red + 2,
                novaMinataKolona = kolona + 1
            }),
            new Tuple<bool, int[]>(Nalqvo(red, 1) && Napred(kolona, 2), new[]
            {
                count++,
                novMinatRed = red - 1,
                novaMinataKolona = kolona + 2
            }),
            new Tuple<bool, int[]>(Nalqvo(red, 2) && Napred(kolona, 1), new[]
            {
                count++,
                novMinatRed = red - 2,
                novaMinataKolona = kolona + 1
            }),
            new Tuple<bool, int[]>(Nadqsno(red, 2) && Nazad(kolona, 1), new[]
            {
                count++,
                novMinatRed = red + 2,
                novaMinataKolona = kolona - 1
            }),
            new Tuple<bool, int[]>(Nalqvo(red, 2) && Nazad(kolona, 1), new[]
            {
                count++,
                novMinatRed = red - 2,
                novaMinataKolona = kolona - 1
            }),
            new Tuple<bool, int[]>(Nadqsno(red, 1) && Nazad(kolona, 2), new[]
            {
                count++,
                novMinatRed = red + 1,
                novaMinataKolona = kolona - 2
            }),
            new Tuple<bool, int[]>(Nalqvo(red, 1) && Nazad(kolona, 2), new[]
            {
                count++,
                novMinatRed = red - 1,
                novaMinataKolona = kolona - 2
            }),
        };
KOPEUE
  • 260
  • 4
  • 15
  • You ultimately want the array reversed. So why not simply reverse the array? You already have `Array.Reverse` function in C#. So it is just one line code. Or am I missing something? – Pradeep Kumar Mar 14 '16 at 20:08
  • How is the reverse even helping ? It just sort's the array backwards. I see it now i probably wasn't clear enough when i look at it it's actually quite messy.. sorry I didn't want just to have it 3,2,1 it might have ended like 2,3,1 i just want every single tuple in the array to switch place with every other tuple inside – KOPEUE Mar 14 '16 at 20:12
  • So you want to make it in random sequence? – Pradeep Kumar Mar 14 '16 at 20:16
  • You know that in the lottery there are a few millions of possible combinations right ? Well i want to shift the array in every possible combination for my 8 arrays. I dont want it random because it has chance of repeating a previous done combination – KOPEUE Mar 14 '16 at 20:17
  • In that case, I think you should generate a random sequence. Then compare the output with the previous ones. If found, you repeat it and generate another. Otherwise whatever algorithm you use, there is a chance your lottery ticket number would be predictable. – Pradeep Kumar Mar 14 '16 at 20:22
  • Random and checking previous works if you don't want all possible combinations. If you do, then random-and-check will be very slow, as you'll have to win the lottery to get the last few remaining ones. – Chris Mar 14 '16 at 20:26
  • You are looking for permutations check this : [http://stackoverflow.com/questions/11208446/generating-permutations-of-a-set-most-efficiently](http://stackoverflow.com/questions/11208446/generating-permutations-of-a-set-most-efficiently) – kurin123 Mar 14 '16 at 20:26
  • exactly what i was looking for please post it as an answer so i can mark it – KOPEUE Mar 14 '16 at 20:53

2 Answers2

0

If what you want is all the permutations of a string, you can use a swapping algorithm like the one found here

Instead of a string you use a tuple array

Chris
  • 846
  • 6
  • 16
0

Are you trying to enumerate all permutations by progressively swapping elements within your array? Something like Heap's algorithm?

T.C.
  • 279
  • 1
  • 2
  • 6
  • This would be better as a comment since it is a clarifying question. It technically answers his question since you suggest Heap's algorithm, but I would say ask the first question in a comment, and if he says that is what he is looking for, then put together a full answer rather than a vague suggestion of using Heap's algorithm. – Kevin Mar 14 '16 at 20:44
  • @kevin-wells, I agree, but the StackOverflow rules don't allow me to comment. – T.C. Nov 29 '16 at 15:37
  • In that case I would recommend rewriting your answer to be a complete answer assuming that Heap's algorithm is what he is looking for and if it isn't then you can edit it later. The restriction on commenting shouldn't be evaded by writing comments as answers – Kevin Nov 29 '16 at 16:32