Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
use strict; use warnings; @_ = (2..10,'J','Q','K','A'); comb('', 4, 2..4,6..10,'J','Q','K','A',@_,@_,@_); sub comb { my ($str, $depth, @items) = @_; if (!$depth--) { interpret($str); return; } comb("$str $items[$_]", $depth, @items[($_+1)..$#items]) for (0..$ +#items) } sub interpret { my @cards = split / /, "5$_[0]"; # Do whatever with @cards; }
Actually though, the possible set can't contain any 5's except the original 5, nor can it contain any 10, J, Q, K - since all hands involving those will have at least 2 points. This cuts the number of combinations significantly.
@_ = (2..4,6..9,'A'); comb('', 4, @_,@_,@_,@_);
Nor can you have more than one of any card.
comb('', 4, 2..4,6..9,'A');
It should be fairly easy to figure out whether there are straights or 15's in the permutations? of this set. Code to follow...
use strict; use warnings; comb('', 4, 1..4,6..9); sub comb { my ($str, $depth, @items) = @_; if (!$depth--) { interpret($str); return; } comb("$str $items[$_]", $depth, @items[($_+1)..$#items]) for (0..$ +#items) } sub interpret { my @cards = split / /, "5$_[0]"; @cards = sort {$b <=> $a} @cards; my $flag = 1; for (0..3) { $flag = 0 if $cards[$_+1] != ($cards[$_]-1); } return if $flag; for (perm(@cards)) { return if sum(split //) == 15; } print join " ", @cards; } BEGIN { my @c_out; sub perm { @c_out = (); permute('', $_, @_) for (0..$#_); return @c_out; } sub permute { my ($str, $depth, @chars) = @_; if (!$depth--) { push @c_out, $str.$_ for @chars; } else { permute($str.$chars[$_], $depth, @chars[($_+1)..($#chars)] +) for (0..$#chars); } } } sub sum { my $n; $n += $_ for @_; return $n; }
Very rough code, but upshot is that there are no hands with a 5 that don't have at least 2 points. I could actually have done this whole thing on paper, since there's very few sets of cards that have to be tested for 15's and straights.

In reply to Re: Generating all 5-card hands by TedPride
in thread Generating all 5-card hands by thor

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (5)
    As of 2019-04-18 12:19 GMT
    Find Nodes?
      Voting Booth?
      I am most likely to install a new module from CPAN if:

      Results (103 votes). Check out past polls.

      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!