You can
use PDL to simplify these calculations. As you have it, a card is just an array of four numbers mod 3. You are using 1, 2 and 3 but you can also use 0, 1 and 2.
Then a set is just three cards X, Y, Z where X+Y+Z = (0,0,0,0) mod 3.
use PDL;
sub is_a_set {
all (($_[0] + $_[1] + $_[2]) % 3) == 0;
}
sub third_card {
(pdl(3,3,3,3)-$_[0]-$_[1]) % 3;
}
my $X = pdl(0,1,2,2);
my $Y = pdl(0,2,2,1);
my $Z = pdl(0,0,2,0);
if (is_a_set($X, $Y, $Z)) {
print "$X, $Y and $Z form a set\n";
} else {
print "$X, $Y and $Z do not form a set\n";
}
print "To form a set with $X and $Y you need: ", third_card($X, $Y), "
+\n";
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.