I had a thought about figuring out poker hands on the subway this morning and I gave it a whirl. I'm happy with my solution, but I don't know how elegant it is, so I thought I would see if the monks want to give a crack at it.
Also, I thought it might be nice to substitute short for elegant and see how low it can be golfed.
Here is the problem definition:
Given a 5 element array of cards, each a string of the form 'RS', where 'R' is the rank (2..10,J,Q,K,A) and where 'S' is the suit ('H','D','S','C'), determine what is the best poker hand that can be made with them. It is not necessary to determine striations with in a hand type (i.e. straight is acceptable, rather than 10-high straight). An example hand would be ('2H','9C','8H','JD','9S')


I have attached a scaffolding for the problem (I left use strict and warnings off for the golfers). The scaffolding allows you to enter a hand on the command-line for easy testing:
use strict; use warnings; use List::Util qw(shuffle min max); use Data::Dumper; my @cards; foreach my $r (2..10,'J','Q','K','A') { foreach my $s (qw(H S D C)) { push @cards,"$r$s"; } } my @deck = shuffle @cards; my @hand = sort @deck[0..4]; @hand = @ARGV if @ARGV; print join(", ",@hand) . ": "; # your solution here, put answer in $hand # end print $hand;


-pete
"Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."

In reply to Golf/Elegance: Poker Hands by dreadpiratepeter

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.