Need help cloning? Visit
is a free Git and Mercurial client for Windows.
is a free Git and Mercurial client for Mac.
+ Create a fresh deck of Set cards.
+ Cards have 4 characteristics, with 3 varients of each characteristic.
+ chars = ('A','B','C')
+ deck = 
+ for a in chars:
+ for b in chars:
+ for c in chars:
+ for d in chars:
+ card = a + b + c + d
+ return deck
+def deal_cards(deck, hand=12):
+ Pass in a list of cards (the deck), returns a deal of the defined hand size (default 12).
+ deal = [deck.pop(random.randint(0, len(deck)-1)) for i in range(0, hand)]
+ return deal
+def refresh_deal(deck, deal, hand=12):
+ Refresh the deal up to the required hand size.
+ You can pass in a fresh deck, or a used one.
+ while (len(deal) < hand):
+ draw = deck.pop(random.randint(0, len(deck)-1))
+ if draw in deal:
+ Pass in a tuple of two cards.
+ Compare two cards. If they only share one characteristic, return True and the
+ index of the matching characteristic as a tuple.
+ Otherwise return False.
+ # Generate a list of strings, containing each type of characteristic of the two cards.
+ # E.g. ['AA','AB','CB','BA']
+ first_card = cards
+ second_card = cards
+ # If the cards match, return False
+ if first_card == second_card: return (False, -1)
+ compare = [first_card[i] + second_card[i] for i in range(0,4)]
+ elements_matched = 0
+ elements_index = 0
+ for index, value in enumerate(compare):
+ # If the first element matches the second element, increment a counter.
+ if value==value:
+ elements_matched += 1
+ elements_index = index
+ if elements_matched == 1:
+ # In ONLY one pair of characteristics matched, return True.
+ return (True, elements_index)
+ return (False, -1)
+ Find a set of three cards in a deal (a list of cards passed in).
+ cards_set = 
+ first_match = False
+ second_match = False
+ index1 = -1
+ index2 = -1
+ # Generate a list of all possible card pairs from this deal, as tuples.
+ card_pairs = 
+ for i in deal:
+ for j in deal:
+ pair = (i, j)
+ if not i == j:
+ # Start iterating through this list to find a possible set pair
+ for i in card_pairs:
+ first_card = i
+ second_card = i
+ (first_match, index1) = compare_cards((first_card, second_card))
+ # When we find a set pair, take the first of the pair and start to
+ # compare it to the other cards in the deal.
+ if first_match:
+ for third_card in deal:
+ if third_card == first_card or third_card == second_card:
+ (second_match, index2) = compare_cards((first_card, third_card))
+ # If we found a possible set pair with the same matching characteristic,
+ # return the three cards.
+ if second_match and index1 == index2:
+ return (first_card, second_card, third_card, index1)