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 ); }

In reply to Re: 52 Perl Pickup by misc
in thread 52 Perl Pickup by dogz007

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.