in reply to Re: amount permutations
in thread amount permutations

Upon closer inspection your code has a bug where it would eliminate two checks with the same amount since you're checking an array of amounts. :( I'll have to modify it to allow that and run some tests to see if it will still result in a speed increase...

Replies are listed 'Best First'.
Re: Re: Re: amount permutations
by Limbic~Region (Chancellor) on Mar 04, 2004 at 22:43 UTC
    gr0k,
    You will have to explain what you mean or else explain what you think the following is doing:
    @_ == keys %{@_,reverse @_}
    Besides - this is a terribly inefficient way to go about what you are trying to do as explained by everyone. I was just showing ways to improve the efficiency of what you already had.

    L~R

        tye,
        Thanks, but I knew what it was. It is gr0k that I didn't think understood it. He indicated I had a bug where I removed duplicates:

        your code has a bug where it would eliminate two checks with the same amount

        I was only asking that he explain how my code (which was a bit more efficient) was different than that snippet.

        Cheers - L~R

      Ah, what I meant was, with your code, if there were two checks with the same amount. ie. A = 10 and G = 10, it wouldn't match a search for amount 20 since your array would look like this: my $RS = [10, 5, 13, 3, 15, 1, 4, 10]; And your line here return 0 if $uniq{$val} > 1; would ignore it thinking it was a duplicate in the sequence.

      It was a very slight change to pass the array of unique checks instead of the array of amounts and then:

      $sum += $rs_ref->{$check}->{'amount'};

        gr0k,
        If you have a requirement to allow duplicates in the combination but no more than is present in the original list then the code is easy enough to change.
        @_ == keys %{@_,reverse @_);
        That is misleading because it also removes duplicates. The difference is you are using letters instead of numbers so there should never be any duplicate. The change you have made to dereference slows the code back down again.

        Again, this is a terrible approach since you are spinning cycles needlessly. If you don't believe me:

        Formula for determining number of unique combinations of k values in a set of n letters is n!/k!(n-k)!

        • 1 = 6
        • 2 = 15
        • 3 = 20
        • 4 = 15
        • 5 = 6
        • 6 = 1

        That is a total of 63 possible combinations you should be testing, but your misuse of Algorithm::Loops has lead to testing 55,986. Unique combination certainly are not permutations.

        L~R