[Codewars] Ranking Poker Hands

Ranking Poker Hands

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
class PokerHand(object):
card = "23456789TJQKA"
RESULT = ["Loss", "Tie", "Win"]

def __init__(self, hand):
values = ''.join(sorted(hand[::3], key=self.card.index))
suits = set(hand[1::3])
flush = len(suits) == 1
straight = values in self.card
self.score = (2 * sum(values.count(c) for c in values) + 13 * straight + 14 * flush, [self.card.index(c) for c in values[::-1]])

def compare_with(self, other):
return self.RESULT[(self.score > other.score) - (self.score < other.score) + 1]
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/06/07/PS/Codewars/ranking-poker-hands/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.