in reply to Re: Array Shuffle
in thread Array Shuffle

It is, and I am/was having a lot of trouble determining a solution to the given problem. Part of the problem is that I am relatively new to perl, and its been a while since I last programmed in Java, so I am quite rusty. However, I would not have just copied and pasted any code and put it off as my own either. More looking for some pseudo code/algorithm to accomplish this.

Replies are listed 'Best First'.
Re^3: Array Shuffle
by spiritway (Vicar) on Feb 28, 2006 at 06:07 UTC

    OK, then how about this:

    1. Take your array, and make a copy.
    2. Shuffle one of them.
    3. Compare the elements of the arrays.
    4. If you find a match, then go back to step 2.
    5. If not, then quit - you've found a unique shuffling.

    I have to admit that this is a fairly simple-minded approach, but consider the source ;-). I don't doubt that there is a Perlish way of doing it - there always is. But I don't know it, yet.

Re^3: Array Shuffle
by duff (Parson) on Feb 28, 2006 at 06:43 UTC

    Here's another mechanism for you:

    1. Make a copy of your array
    2. shuffle it (using List::Util::shuffle)
    3. do an element-wise comparison of your two arrays and if you find a match swap that element with an adjacent element
    4. goto 3 until you swap no more