Just another Perl shrine PerlMonks

### Re: 52 Perl Pickup

by misc (Pilgrim)
 on Aug 02, 2007 at 19:47 UTC ( #630371=note: print w/replies, xml ) Need Help??

in reply to 52 Perl Pickup

I've written a solution, too. :-)

I haven't been able to determine the complexity, I'm too bad in mathematics, maybe someone else could ?

However, the lower bound clearly tops all other algorithms, its g(1).
Ok, the upper bound could be g(unlimited), I'd guess.

```use strict;
use List::Util qw(shuffle);

# construct a standard deck
my @card;
foreach my \$suit (qw(H C S D)) {
foreach my \$value (2..10, qw(J Q K A)) {
push @card, \$value . \$suit;
}
}

# now shuffle us up a pile of cards, perhaps composed of any
# number of decks
my @pile;
my \$n = 4;      # number of decks in shuffled pile
push @pile, @card for (1..\$n);
@pile = shuffle @pile;

my \$decks;
do {
\$decks = grab ( \@pile ); # split the pile into \$n decks
@pile = lookup( \$decks ); # Look if we got it right..
} while ( @pile > 0 );

print "Got it!\n";

for my \$a (0..\$n){
print "\$_ " foreach ( @{\$decks->{\$a}} );
print "\n\n";
}
# Exits here.. happily

sub grab{
my \$pile = shift;

my \$decks;
# Grab the cards
foreach my \$card (@{\$pile}){
push @{\$decks->{int(rand(\$n))}}, \$card;
}
return \$decks;
}

sub lookup{
my \$decks = shift;

for my \$a (0..\$n-1) {
my %cards;
foreach my \$card ( @{\$decks->{\$a}} ){
if ( defined( \$cards{\$card} ) ){
damnit( \$decks );
return throw ( \$decks );
} else {
\$cards{\$card} = 1; # Remember this. An
+d dont forget it!
}
}
}
}

sub damnit{
my \$decks = shift;

print "Damnit!!!\n";
for my \$a (0..\$n){
print "\$_ " foreach ( @{\$decks->{\$a}} );
print "\n\n";
}
}

sub throw{
my \$decks = shift;

my @pile;
foreach ( values %{\$decks} ){
push @pile, @{\$_};
}
return shuffle( @pile );
}

Create A New User
Node Status?
node history
Node Type: note [id://630371]
help
Chatterbox?
 [Cosmic37]: Permissions are interesting earthlings. Did nature determine who gives permission and who asks permission. Who was the first to get permission? Are you not related to them as one big earthling family? [karlgoethebier]: Cojones! We need cojones! [karlgoethebier]: Ouch! Permissions! We need permissions! [BarApp]: I can not use modules. I gain temporary access and still can not use modules. [Cosmic37]: ta erix - this szabo geezer is pretty cool methinks and he writes about undef but I cannot see instructions for redefining the record separator after having undefined it [Corion]: \$/ = "wahtever";

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2017-06-29 16:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (672 votes). Check out past polls.