in reply to 52 Perl Pickup

The inner loop can be replaced with a hash to change the algorithm from O(N2) to O(N), a major improvement.

my @groups; my %in_num_groups; while (@pile) { my $item = shift @pile; push @{$groups[$in_num_groups{$item}++]}, $item; }

I also fixed the needless use of a reference to the array of groups and the inability to store false values in @pile.

Update: For fun, did you know @pile could also be initialized using

@pile = shuffle( (@card) x $n );

Replies are listed 'Best First'.
Re^2: 52 Perl Pickup
by dogz007 (Scribe) on Aug 02, 2007 at 06:10 UTC
    ikegami, your new solution is flawless. Indeed, I had intended to ask for improvements upon my method at the end of my note, but apparently forgot to. Your hash method improves the exact portion of my algorithm that I felt the least confident about. I hate using flags like $found ... too ugly I guess. I knew there surely was a better way, but couldn't quite wrap my mind around it. Thank you very much, as I will be sure to remember this method for future problems.