# Set, the card game

**Set, the card game**

You are encouraged to solve this task according to the task description, using any language you may know.

The card game, Set, is played with a pack of 81 cards, each of which depicts either one, two, or three diamonds, ovals, or squiggles. The symbols are coloured red, green, or purple, and the shading is either solid, striped, or open. No two cards are identical.

In the game a number of cards are layed out face up and the players try to identify "sets" within the cards.

A set is three cards where either the symbols on the cards are the same or they are all different, the number of symbols on the cards are all the same or all different, the colours are all the same or all different, and the shadings are all the same or all different.

For example, this is a set:

two solid green ovals one open green squiggle three striped green diamonds

because each card depicts a different symbol, the number of symbols on each card is different, the colours are all the same, and the shadings are all different.

This is not a set:

two solid purple ovals one open green squiggle three striped green diamonds

because two of the cards are green and one is purple, so the colours are neither all the same nor all different.

- task

- Create a representation of a pack of Set cards, shuffle it, select a specified number of cards from the pack and list them in the output.

- Identify the sets in the selected cards and list them.

- Also see

- The wikipedia article, Set (card game)

## Julia

```
import Base: vec, string, print
import Random: shuffle
import Combinatorics: combinations
const NUMBERS = ["one", "two", "three"]
const SHADINGS = ["solid", "striped", "open"]
const COLORS = ["red", "green", "purple"]
const SYMBOLS = ["diamond", "oval", "squiggle"]
struct SetCard
number::UInt8
shading::UInt8
color::UInt8
symbol::UInt8
function SetCard(num, sha, col, sym)
@assert all(i -> 1 <= i <= 3, (num, sha, col, sym))
return new(num, sha, col, sym)
end
end
num(s::SetCard) = NUMBERS[s.number]
sha(s::SetCard) = SHADINGS[s.shading]
col(s::SetCard) = COLORS[s.color]
sym(s::SetCard) = SYMBOLS[s.symbol]
Base.vec(sc::SetCard) = [sc.number, sc.shading, sc.color, sc.symbol]
function Base.string(sc::SetCard)
plu = sc.number == 1 ? "" : "s"
return "(" * join([num(sc), sha(sc), col(sc), sym(sc)], " ") * plu * ")"
end
Base.print(io:: IO, sc::SetCard) = print(io, string(sc))
Base.print(io:: IO, vsc::Vector{SetCard}) = print(io, "[" * join(string.(vsc), ", ") * "]")
""" True if the 3 cards form a set according to the Set game rules """
function allsameordifferent(sc1::SetCard, sc2, sc3)
a, b, c = vec(sc1), vec(sc2), vec(sc3)
return all(i -> a[i] == b[i] == c[i] || a[i] != b[i] && a[i] != c[i] && b[i] != c[i], eachindex(a))
end
""" Return a vector of the sets in the vector of SetCards """
function process_deal(cards::Vector{SetCard})
return Iterators.filter(combo -> allsameordifferent(combo...), collect(combinations(cards, 3)))
end
function testcardsets()
pack = vec([SetCard(n, sh, c, sy) for n in 1:3, sh in 1:3, c in 1:3, sy in 1:3])
numcards = 81
while !isnothing(numcards)
print("\n\nEnter number of cards to deal (3 to 81, or just a space to exit) => ")
numcards = tryparse(Int, readline())
if !isnothing(numcards) && 3 <= numcards <= 81
deal = shuffle(pack)[begin:numcards]
sets = collect(process_deal(deal))
println("\nThe deal is:\n$deal\n\nThere are $(length(sets)) sets.")
foreach(println, sets)
end
end
end
testcardsets()
```

- Output:

Enter number of cards to deal (3 to 81, or just a space to exit) => 4 The deal is: [(one striped red squiggle), (one striped purple diamond), (three solid purple diamonds), (three open red ovals)] There are 0 sets. Enter number of cards to deal (3 to 81, or just a space to exit) => 12 The deal is: [(one striped green squiggle), (one solid green oval), (one open green oval), (one striped red diamond), (one open purple oval), (two open purple squiggles), (one solid red diamond), (three open purple diamonds), (three open green diamonds), (three striped red ovals), (three open green ovals), (two open red ovals)] There are 3 sets. [(one striped green squiggle), (one open purple oval), (one solid red diamond)] [(one open purple oval), (two open purple squiggles), (three open purple diamonds)] [(one open purple oval), (three open green ovals), (two open red ovals)] Enter number of cards to deal (3 to 81, or just a space to exit) => 16 The deal is: [(three open purple squiggles), (one open red diamond), (three striped purple diamonds), (three solid red diamonds), (one solid purple diamond), (one solid green oval), (three solid red ovals), (three solid purple ovals), (one solid green diamond), (two solid green diamonds), (two open purple squiggles), (one open purple squiggle), (three striped purple squiggles), (two solid green squiggles), (three solid red squiggles), (one open purple diamond)] There are 6 sets. [(three open purple squiggles), (three striped purple diamonds), (three solid purple ovals)] [(three open purple squiggles), (two open purple squiggles), (one open purple squiggle)] [(one open red diamond), (three striped purple diamonds), (two solid green diamonds)] [(three solid red diamonds), (one solid purple diamond), (two solid green diamonds)] [(three solid red diamonds), (three solid red ovals), (three solid red squiggles)] [(one solid purple diamond), (three solid red ovals), (two solid green squiggles)] Enter number of cards to deal (3 to 81, or just a space to exit) =>

## Phix

with javascript_semantics constant nums = {"one", "two", "three"}, shades = {"solid", "striped", "open"}, colours = {"red", "green", "purple"}, symbols = {"diamond", "oval", "squiggle"} function decode(integer t) t -= 1 integer n = remainder(t,3)+1, s = remainder(floor(t/3),3)+1, c = remainder(floor(t/9),3)+1, m = remainder(floor(t/27),3)+1 return {n,s,c,m,iff(n=1?"":"s")} end function procedure showcard(sequence card) integer {n,s,c,m} = card printf(1,"%s %s %s %s%s\n",{nums[n],shades[s],colours[c],symbols[m],card[5]}) end procedure procedure showsets(sequence hand) integer lh = length(hand) printf(1,"Cards dealt: %d\n%n",{lh,lh!=81}) if lh!=81 then for c in hand do showcard(decode(c)) end for end if sequence sets = {} for t in combinations(hand,3) do sequence {c1,c2,c3} = apply(t,decode) bool found = true for i=1 to 4 do if not (c1[i]=c2[i] and c2[i]=c3[i]) and not (c1[i]!=c2[i] and c2[i]!=c3[i] and c1[i]!=c3[i]) then found = false exit end if end for if found then sets = append(sets,{c1,c2,c3}) end if end for printf(1,"\nSets present: %d\n\n",length(sets)) if lh!=81 then for s in sets do for c in s do showcard(c) end for printf(1,"\n") end for end if end procedure sequence pack = tagset(81) for deal in {4,8,12,81} do pack = shuffle(pack) showsets(pack[1..deal]) end for

- Output:

Cards dealt: 4 three open purple ovals two solid green ovals three solid red squiggles three striped purple diamonds Sets present: 0 Cards dealt: 8 two striped purple squiggles three striped red squiggles one striped green squiggle two open purple diamonds three solid green squiggles two solid green squiggles one striped purple oval two solid purple squiggles Sets present: 1 three striped red squiggles one striped green squiggle two striped purple squiggles Cards dealt: 12 two open green diamonds two striped purple diamonds two open purple ovals two solid red ovals three solid purple squiggles three striped green ovals three solid green diamonds one striped purple diamond three solid green ovals one open purple oval three solid red diamonds three solid purple ovals Sets present: 5 three solid red diamonds two open green diamonds one striped purple diamond three solid red diamonds three solid green ovals three solid purple squiggles one striped purple diamond two open purple ovals three solid purple squiggles two striped purple diamonds one open purple oval three solid purple squiggles two solid red ovals three striped green ovals one open purple oval Cards dealt: 81 Sets present: 1080

## Python

```
from itertools import combinations
from itertools import product
from random import shuffle
from typing import Iterable
from typing import List
from typing import NamedTuple
from typing import Tuple
NUMBERS = ("one", "two", "three")
SHAPES = ("diamond", "squiggle", "oval")
SHADING = ("solid", "striped", "open")
COLORS = ("red", "green", "purple")
class Card(NamedTuple):
number: str
shading: str
color: str
shape: str
def __str__(self) -> str:
s = " ".join(self)
if self.number != "one":
s += "s"
return s
Cards = List[Card]
def new_deck() -> Cards:
"""Return a new shuffled deck of 81 unique cards."""
deck = [Card(*features) for features in product(NUMBERS, SHADING, COLORS, SHAPES)]
shuffle(deck)
return deck
def deal(deck: Cards, n: int) -> Tuple[Cards, Cards]:
"""Return _n_ cards from the top of the deck and what remains of the deck."""
return deck[:n], deck[n:]
def is_set(cards: Tuple[Card, Card, Card]) -> bool:
"""Return _True_ if _cards_ forms a set."""
return (
same_or_different(c.number for c in cards)
and same_or_different(c.shape for c in cards)
and same_or_different(c.shading for c in cards)
and same_or_different(c.color for c in cards)
)
def same_or_different(features: Iterable[str]) -> bool:
"""Return _True_ if _features_ are all the same or all different."""
return len(set(features)) in (1, 3)
def print_sets_from_new_deck(n: int) -> None:
"""Display sets found in _n_ cards dealt from a new shuffled deck."""
table, _ = deal(new_deck(), n)
print(f"Cards dealt: {n}\n")
print("\n".join(str(card) for card in table), end="\n\n")
sets = [comb for comb in combinations(table, 3) if is_set(comb)]
print(f"Sets present: {len(sets)}\n")
for _set in sets:
print("\n".join(str(card) for card in _set), end="\n\n")
print("----")
if __name__ == "__main__":
for n in (4, 8, 12):
print_sets_from_new_deck(n)
```

- Output:

Cards dealt: 4 two open green diamonds three striped green ovals two open purple ovals two open red squiggles Sets present: 1 two open green diamonds two open purple ovals two open red squiggles ---- Cards dealt: 8 three striped purple diamonds one solid purple oval two open purple diamonds three solid purple diamonds one solid green squiggle three open green squiggles three open purple squiggles three solid purple ovals Sets present: 1 three striped purple diamonds three open purple squiggles three solid purple ovals ---- Cards dealt: 12 two open green squiggles three solid purple ovals three open red diamonds two open red squiggles three open purple ovals three open red squiggles three striped red squiggles two open purple diamonds three solid red squiggles one solid red squiggle two striped purple diamonds one solid red diamond Sets present: 2 two open red squiggles three striped red squiggles one solid red squiggle three open red squiggles three striped red squiggles three solid red squiggles ----

## Quackery

`transpose`

is defined at Matrix transposition#Quackery.

`comb`

and `arrange`

are defined at Combinations#Quackery.

```
[ true swap transpose
witheach
[ 0 swap witheach +
3 mod 0 > if
[ not conclude ] ] ] is isset ( [ --> b )
[ [ [] 81 times
[ i 4 times
[ 3 /mod swap ]
drop
3 times join
nested join ] ] constant
shuffle swap split drop ] is cards ( n --> [ )
[ [] swap
dup size swap
3 rot comb
witheach
[ dip dup arrange
dup isset iff
[ nested rot
join swap ]
else drop ]
drop ] is sets ( [ --> [ )
[ unpack dup dip
[ [ table
$ "one"
$ "two"
$ "three" ] do echo$ sp
[ table
$ "solid"
$ "striped"
$ "open" ] do echo$ sp
[ table
$ "red"
$ "green"
$ "purple" ] do echo$ sp
[ table
$ "diamond"
$ "squiggle"
$ "oval" ] do echo$ ]
0 > if [ say "s" ]
cr ] is echocard ( [ --> )
[ dup cards swap
cr say "Cards dealt: " echo cr cr
dup witheach echocard
cr
sets dup size
say "Sets present: " echo cr cr
witheach
[ witheach echocard
cr ] ] is play ( n --> )
' [ 4 8 12 ] witheach
[ play say "-----" ]
```

- Output:

Cards dealt: 4 two striped green squiggles one open purple oval one solid purple diamond three open red diamonds Sets present: 0 ----- Cards dealt: 8 three open purple squiggles two open purple ovals three solid purple ovals three solid red squiggles two striped purple diamonds two solid green squiggles one striped green oval one open purple diamond Sets present: 1 three open purple squiggles two open purple ovals one open purple diamond ----- Cards dealt: 12 one solid green diamond one striped red diamond one open purple squiggle two solid green diamonds two striped green squiggles two solid red ovals two solid green squiggles one open green squiggle two solid green ovals two solid red diamonds one open purple diamond three striped purple diamonds Sets present: 3 two solid red ovals one open green squiggle three striped purple diamonds two solid green diamonds two solid green squiggles two solid green ovals one solid green diamond one striped red diamond one open purple diamond -----

## Wren

Note that entering 81 for the number of cards to deal confirms that there are 1080 possible sets.

```
import "random" for Random
import "./ioutil" for Input
import "./fmt" for Fmt
import "./perm" for Comb
var nums = ["one", "two", "three"]
var shas = ["solid", "striped", "open"]
var cols = ["red", "green", "purple"]
var syms = ["diamond", "oval", "squiggle"]
var pack = List.filled(81, null)
var i = 0
for (num in 0..2) {
for (sha in 0..2) {
for (col in 0..2) {
for (sym in 0..2) {
pack[i] = [nums[num], shas[sha], cols[col], syms[sym]]
i = i + 1
}
}
}
}
var printCards = Fn.new { |cards|
for (card in cards) {
var pl = card[0] != "one" ? "s" : ""
Fmt.print("$s $s $s $s$s", card[0], card[1], card[2], card[3], pl)
}
}
var findSets = Fn.new { |cards|
var sets = []
var trios = Comb.list(cards, 3)
for (trio in trios) {
var t1 = trio[0]
var t2 = trio[1]
var t3 = trio[2]
var found = true
for (i in 0..3) {
if (t1[i] == t2[i] && t2[i] == t3[i]) continue
if (t1[i] != t2[i] && t2[i] != t3[i] && t1[i] != t3[i]) continue
found = false
break
}
if (found) sets.add(trio)
}
Fmt.print("Sets present: $d\n", sets.count)
if (sets.count > 0) {
for (set in sets) {
printCards.call(set)
System.print()
}
}
}
var prompt = "Enter number of cards to deal - 3 to 81 or 2 to exit: "
while(true) {
Random.new().shuffle(pack) // shuffle for each deal
var i = Input.integer(prompt, 2, 81)
if (i == 2) return
var dealt = pack[0...i]
System.print()
printCards.call(dealt)
System.print()
findSets.call(dealt)
}
```

- Output:

Sample run:

Enter number of cards to deal - 3 to 81 or 2 to exit: 4 three solid green diamonds one solid red diamond one solid green oval three striped purple squiggles Sets present: 0 Enter number of cards to deal - 3 to 81 or 2 to exit: 8 one open green squiggle one open purple squiggle one solid green squiggle three solid purple squiggles three open green squiggles one striped red diamond one striped green oval one striped green squiggle Sets present: 1 one open green squiggle one solid green squiggle one striped green squiggle Enter number of cards to deal - 3 to 81 or 2 to exit: 12 three open green ovals three striped green diamonds one solid purple oval one striped purple diamond two open green diamonds three solid red diamonds three solid red ovals three solid green diamonds three striped red ovals three striped red squiggles two open red squiggles one solid green oval Sets present: 3 three striped green diamonds one solid purple oval two open red squiggles one solid purple oval two open green diamonds three striped red squiggles one striped purple diamond two open green diamonds three solid red diamonds Enter number of cards to deal - 3 to 81 or 2 to exit: 2