Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Magic with 5 cards by marcopunteri
from collections import Counter
from operator import attrgetter
RANKS = tuple('A 2 3 4 5 6 7 8 9 10 J Q K'.split())
SUITS = tuple('♣♦♥♠')
class Card:
def __init__(self,str_value):
rank, suit = str_value.split()
self.rank = rank
self.suit = suit
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __lt__(self,other):
self_index, other_index = RANKS.index(self.rank), RANKS.index(other.rank)
if self_index < other_index:
low = True
elif self_index > other_index:
low = False
else:
low = True if SUITS.index(self.suit) < SUITS.index(other.suit) else False
return low
def __le__(self,other):
return self < other or self==other
def __repr__(self):
return f"{self.rank} {self.suit}"
def __sub__(self,other):
return RANKS.index(self.rank) - RANKS.index(other.rank)
def bot(*cards, n=1):
cards = [Card(card) for card in cards]
suits = Counter(attrgetter('suit')(c) for c in cards)
couple = [card for card in sorted(cards,key=attrgetter('suit')) if suits[card.suit]==max( suits.values())][:2]
couple.sort()
card_to_hide, card_to_show = couple if couple[1] - couple[0] >= 7 else couple[::-1]
delta = (card_to_hide - card_to_show)%13
cards_to_show = []
cards_left = sorted([card for card in cards if card not in couple])
if delta > 4:
cards_to_show.append(cards_left[0])
elif delta > 2:
cards_to_show.append(cards_left[1])
else:
cards_to_show.append(cards_left[2])
cards_left.remove(cards_to_show[-1])
if delta%2 == 1:
cards_to_show.append(cards_left[0])
else:
cards_to_show.append(cards_left[1])
cards_left.remove(cards_to_show[-1])
cards_to_show.append(cards_left[0])
n = n%4 if n%4 > 0 else 4
cards_to_show.insert(n-1,card_to_show)
return [card.__repr__() for card in cards_to_show]
def magician(*cards, n=1):
"""Determine the fifth card with only four cards."""
n = n%4 - 1 if n%4 > 0 else 3
cards = [Card(c) for c in cards]
base_card = cards.pop(n)
suit = base_card.suit
base_rank = base_card.rank
index_max = sorted(cards).index(cards[0])
if index_max == 2:
delta = 1
elif index_max == 1:
delta = 3
else:
delta = 5
cards.pop(0)
if cards[0] > cards[1]:
print(cards)
delta += 1
rank = RANKS[(RANKS.index(base_rank) + delta )%13]
return f"{rank} {suit}"
if __name__ == '__main__':
assert list(bot('A ♥', '3 ♦', 'K ♠', 'Q ♣', 'J ♦')) == ['J ♦', 'A ♥', 'Q ♣', 'K ♠']
assert magician('J ♦', 'A ♥', 'Q ♣', 'K ♠') == '3 ♦'
assert list(bot('10 ♦', 'J ♣', 'Q ♠', 'K ♥', '7 ♦', n=2)) == ['Q ♠', '7 ♦', 'J ♣', 'K ♥']
assert magician('Q ♠', '7 ♦', 'J ♣', 'K ♥', n=2) == '10 ♦'
print(bot('K ♦', 'K ♠', '2 ♣', '8 ♠', '10 ♠', n=3))
print(bot('2 ♦', '9 ♥', 'K ♦', '4 ♥', '2 ♥', n=5))
print(magician('K ♣', '4 ♥', '5 ♥', '10 ♦', n=8))
July 22, 2021
Comments: