So today I found a large pile of cards laying about my coffee table. After spotting the two empty card boxes on the floor nearby, I realized that picking up all of the cards would then require organizing them into their respective decks before replacing them into their boxes. My immediate thought was, "Man, I sure wish I could whip up a quick Perl script to do this for me!", a comment which surprised me, since it was the first of its kind. I guess I can be pretty lazy, but it was the first time my first thought was to turn to Perl for a physical-world problem.

Later on I began to think that perhaps there might exist a similar problem in cyberspace. A hypothetical "pile" of data items might require organizing into distinct groups that contain only one of each item. Not sorted within each group per say, just organized. The script that follows is my solution. I attempted to generalize the algorithm to organize piles of any item. I used the card-pile pickup problem only as a test case.

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; # i think i'll try to generalize the algorithm such that it organizes # the pile into distinct groups that each have only one of each item. # in this test case, it will organize the pile into the appropriate # number of decks, each containing only one of each card. my $group = [[]]; while (my $item = shift @pile) { # search the current groups and add if item not found my $found; foreach my $i (0..$#$group) { unless ( grep { $item eq $_ } @{$group->[$i]} ) { push @{$group->[$i]}, $item; $found = 1; last; } } # if not found, make a new group with that item in it push @$group, [$item] unless $found; } # now print the results to see our handiwork foreach (0..$#$group) { print join(" ", @{$group->[$_]}), "\n\n" }

Prints the following (will differ for each shuffle):

10S QH 9S 6D 4S 10H JD JS KS AD 9C 7C KH 6S 8S 4D 10D 5D 10C QC 7S 9D +7H QS KD 2D 6H JC 5H 3D 8H JH 2S 9H 5C AC 2H AS 3C 8C KC 4C 5S 7D 3H +QD 8D 3S 2C AH 4H 6C 9C JS KH KS 4D QH 10S 7S AD 4S QS QC 5D 5H 8S 6H JD KD 9S 10D 5C 10H 8 +H 10C AS JH 2D 7D 6S 3H KC 3C 9H AC 9D 3D 4C 2H QD AH 8C 2C 3S JC 2S +7H 4H 6D 7C 6C 8D 5S 10S 4S QS 6H 8S 7S KS 10C 7D KD 4D AD JH 9C 9S QH KH 3D 5D 9D 6S 3H QD + 5H 2H JC 8C 2D 2S JD 3C 3S 10H AH AS 6D 2C QC 8H 4C 7C 9H 10D 4H JS +AC KC 5C 8D 6C 5S 7H QD 4S 7S JC JH AD 3H 8S QH 3D QS KS 4D 10C KD 9D 6H 3S QC 6S 6D 8C 10S + 8H 7C AS 2D 2S AH 7D JD 5C 9C KC 8D 9H 10H KH 3C 2H 4H 4C 5D JS 10D +2C 5S 9S AC 5H 7H 6C

Now that the problem's solved, I wonder if there really is a need for this algorithm. But, I guess that's what makes it a meditation.


In reply to 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.