You can read minds
Solution
""" solution.py """
# You can read minds
import random
from collections import namedtuple
def codify(d, cards):
""" codify cards to indicate d """
ref = {1: (0, 1, 2), 2: (0, 2, 1), 3: (1, 0, 2),
4: (1, 2, 0), 5: (2, 0, 1), 6: (2, 1, 0)}
cs = sorted(cards, key=lambda x: x.value)
return [cs[x] for x in ref[d]]
def order(cards):
""" order such that difference is six or less """
x, y = cards
d = x.rank - y.rank
if 0 < d <= 6:
return [x, y], d
if d > 6:
return [y, x], 13 - d
d = abs(d)
if d <= 6:
return [y, x], d
return [x, y], 13 - d
def group(cards):
""" group into two of same suit and remainder """
pair, remainder = None, None
index = None
for m in range(4):
for n in range(m + 1, 5):
if cards[m].suit == cards[n].suit:
pair, index = order([cards[m], cards[n]])
remainder = [cards[c] for c in {0, 1, 2, 3, 4} - {m, n}]
break
if pair is not None:
break
return pair + codify(index, remainder)
random.seed()
PlayingCard = namedtuple('Card', ['suit', 'rank', 'value'])
ranks = {1: 'Ace', 11: 'Jack', 12: 'Queen', 13: 'King'}
suits = {0: 'Spades', 1: 'Hearts', 2: 'Clubs', 3: 'Diamonds'}
deck = [PlayingCard(suit, rank, suit * 13 + rank)
for suit in [0, 1, 2, 3]
for rank in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]]
for i in range(52):
j = random.randint(0, 51)
deck[i], deck[j] = deck[j], deck[i]
hand = [deck.pop() for _ in range(5)]
hand = group(hand)
for h in hand:
print(ranks.get(h.rank, h.rank), suits[h.suit])Output
8 Spades
7 Spades
9 Hearts
Queen Clubs
Ace Diamonds