Avi has asked for the wisdom of the Perl Monks concerning the following question:

I have a small fun project I'm working on to compute NCAA tournament pool points. I've got that working perfectly, but I had the idea of seeing who has the best percentage of winning by getting all possible outcomes from the sweet sixteen to the championship.

The way I have it set up now is that for each team's progression in the tournament, they get a point. So, if they lost in the first round, they get a 0, if they get to the sweet sixteen, they get a 2, and if they become the champs, they get a 6.

So, from the sweet sixteen, there's the following possiblity:
2,2,2,2,2,2,2,2,3,3,3,3,4,4,5,6
disregarding 0's and 1's (first and second rounds respectively).

My problem now is by using permutations (I'm now using the genperm function provided by AltBlue), there are duplicates in the permutations, which not only would screw up the percentage, but also take a couple days to compute. Does anyone know a way to permute an array with no duplicates? Meaning switches 2's would not result in another permutation. Or if people know of a better way I should be doing this, I'm very open to suggestion.

Thanks in advance.
Avi

Replies are listed 'Best First'.
(tye)Re: Advanced array permutation
by tye (Sage) on Mar 19, 2002 at 22:18 UTC
Re: Advanced array permutation
by Avi (Acolyte) on Mar 19, 2002 at 22:57 UTC
    Worked like a charm. Thanks tye.
    I'm still a bit of a novice to perl so I have one more basic question to your solution. I usually place my subroutines below my main code. When I did this with your code, it did not work. Only when I placed it above the main code it worked.
    What's the reasoning behind the madness?
    Thanks,
    Avi
      tye's solution uses prototypes. Because it does, it must be declared above where it is used. You can either define it above the usage or just declared it above, as seen here:
      sub foo (@$); .... # Some usage of foo() here .... sub foo (@$) { # Blah blah blah }

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        Alternately, you can delete the prototype and pass in a reference to the array directly: nextpermute(\@list).

        BTW, Avi, next time, if you reply to my (or whoever's) node rather than to your original question, you are likely to get a faster answer as I get notified of replies to my nodes (see User Settings).

                - tye (but my friends call me "Tye")