Demo entry 6340642

Test

   

Submitted by anonymous on Dec 28, 2016 at 14:37
Language: Python 3. Code size: 1.9 kB.

def hand_to_features(hand):
    # Detects if the hand is a flush
    suits = [hand[0], hand[2], hand[4], hand[6], hand[8]]
    ranks = [hand[1], hand[3], hand[5], hand[7], hand[9]]
 
    def flush(suits):
        f = {}
        for suit in suits:
            if suit in f:
                f[suit] += 1
            else:
                f[suit] = 1
        if any(i == 5 for i in dict.values(f)):
            return 1
        else:
            return 0
 
    # Detects if the hand has a pair, three of a kind, four of a kind...
    def kind(ranks):
        k = {}
        for rank in ranks:
            if rank in k:
                k[rank] += 1
            else:
                k[rank] = 1
        return max(dict.values(k))
 
    # Detects if the hand has another pair.
    def kind2(ranks):
        k2 = {}
        for rank in ranks:
            if rank in k2:
                k2[rank] += 1
            else:
                k2[rank] = 1
        temp = max(dict.values(k2))
        for i in k2:
            if k2[i] == temp:
                k2.pop(i)
                break
 
        return max(dict.values(k2))
 
    # Detects the highest card.
    def high_low(ranks):
        ranks = sorted(ranks)
        if 1 in ranks:
            h = 1
            while 1 in ranks: ranks.remove(1)
            l = ranks[0]
        else:
            h = ranks[-1]
            l = ranks[0]
 
        return h, l
 
    # Detects if there is a straight
    def straight(ranks):
        h, l = high_low(ranks)
        detects = [(rank - l + 13)%13 for rank in ranks]
        detects = sorted(detects)
        if detects[-1] == 4:
            return 1
        else:
            return 0
 
    kind = kind(ranks)
    kind2 = kind2(ranks)
    flush = flush(suits)
    high, low = high_low(ranks)
    straight = straight(ranks)
 
    return [kind, kind2, flush, high, low, straight]

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).