Card(red, two, oval, open)
Card(red, three, diamond, open)</pre>
===Alternative Version===
{{incorrect|D|Output doesn't show correct sets.}}
This uses the 4th D entry in the Combinations Task.
{{trans|Python}}
<lang d>import std.stdio, std.algorithm, std.random, std.string, std.array,
std.range, std.typecons, combinations4;
const string[][] features;
alias Card = Tuple!(int, int, int, int);
const Card[] deck;
static this() {
features = ["green purple red",
"one two three",
"oval diamond squiggle",
"open striped solid"].map!split.array;
deck = cartesianProduct(3.iota, 3.iota, 3.iota, 3.iota).array;
}
enum dealt = 9;
void printCard(Card card) {
writefln("%-(%8s %)",
zip(features, [card[]]).map!(fi => fi[0][fi[1]]));
}
const(Card)[] getDeal(in int d=dealt) {
//return deck.randomCover.take(d).array;
auto result = deck.randomSample(d).array.dup;
result.randomShuffle;
return result;
}
const(Card)[][] getSets(in Card[] deal) {
typeof(return) sets;
outer: foreach (comb; Comb.On(deal, 3)) {
foreach (feature; zip([comb[0][]], [comb[1][]], [comb[2][]])) {
immutable l = [feature[]].sort().uniq.walkLength;
if (l != 1 && l != 3)
continue outer;
}
sets ~= comb;
}
return sets;
}
void printIt(in Card[] deal, in Card[][] sets) {
writefln("Dealt %d cards:", deal.length);
foreach (card; deal)
card.printCard;
writefln("\nFound %d sets:", sets.length);
foreach (s; sets) {
foreach (card; s)
card.printCard;
writeln;
}
}
void main() {
while (true) {
const deal = getDeal;
const sets = deal.getSets;
if (sets.length == dealt / 2) {
printIt(deal, sets);
break;
}
}
}</lang>
{{out}}
<pre>Dealt 9 cards:
purple three diamond open
purple two squiggle striped
purple one oval solid
purple three oval solid
green one diamond open
red three oval solid
red two diamond striped
red one squiggle open
purple one diamond open
Found 4 sets:
red two diamond striped
red one squiggle open
purple one diamond open
red two diamond striped
red one squiggle open
purple one diamond open
red two diamond striped
red one squiggle open
purple one diamond open
red two diamond striped
red one squiggle open
purple one diamond open</pre>
=={{header|J}}==
|