过了88%
# 翻牌
# @param inHand string字符串 一组以单空格间隔的手牌(例如:SA HK H9 D8 C5 S5 H4)
# @return string字符串
#
from collections import defaultdict
class Solution:
def is_continuous(self,nums):
count = 1
record = [nums[0]]
for i in range(1,len(nums)):
if(count>=4 and record[0]==13 and nums[-1]==1): # 最高顺子
return "B"
if(count>=5): # 顺子
return "b"
if(nums[i-1]==nums[i]+1):
count += 1
record.append(nums[i])
elif(nums[i-1]==nums[i]):
pass
else:
count = 1
record = [nums[i]]
if(count>=4 and record[0]==13 and nums[-1]==1): # 最高顺子
return "B"
if(count>=5): # 顺子
return "b"
return False
def check_same(self,nums):
same_num = []
count = 1
for i in range(1,len(nums)):
if(nums[i-1]==nums[i]):
count += 1
else:
if(count>1):
same_num.append(count)
count = 1
if(count>1):
same_num.append(count)
return same_num
def showDown(self , inHand: str) -> str:
cards = inHand.split(" ")
attr = set()
# check tonghua
all_type = "SHCD"
buckets = defaultdict(int)
flag = False
for c in cards:
for t in all_type:
if(t in c):
buckets[t] += 1
if(buckets[t]>=5):
attr.add("a")
flag = True
break
if(flag):
break
# check shunzi
nums = []
for c in cards:
if(c[1:]=="A"):
nums.append(1)
elif(c[1:]=="J"):
nums.append(11)
elif(c[1:]=="Q"):
nums.append(12)
elif(c[1:]=="K"):
nums.append(13)
else:
nums.append(int(c[1:]))
nums.sort(reverse=-1)
flag = self.is_continuous(nums)
if(flag=="B" and "a" in attr):
return "HuangJiaTongHuaShun"
if(flag=="b" and "a" in attr):
return "TongHuaShun"
if(flag!=False):
attr.add(flag)
# check same number
same_num = self.check_same(nums)
for num in same_num:
if(num==4):
return "SiTiao"
if(("c"+str(num)) in attr):
attr.add("c"+str(num)+".")
else:
attr.add("c"+str(num))
# other situations
if("c3" in attr and "c2" in attr):
return "HuLu"
if("a" in attr):
return "TongHua"
if("b" in attr&nbs***bsp;"B" in attr):
return "ShunZi"
if("c3" in attr):
return "SanTiao"
if("c2" in attr and "c2." in attr):
return "LiangDui"
if("c2" in attr):
return "YiDui"
nums = [
"SA SK SQ SJ S10 H10 C9", # 皇家同花顺
"S2 SK SQ SJ H10 S10 S9", # 同花顺
"S10 S10 SQ SJ S10 H10 C9", # 四条
"S10 H10 SQ SJ C10 CQ", # HULU
"SA SK S3 SJ S10 H10 C9", # tonghua
"SA SK S3 SJ S10 H10 C9", # tonghua
"S10 H10 SQ SJ C10 C9", # SANTIAO
"SK HQ H5 S4 S3 C2 CA", # SHUNZI
"SK HK H4 S4 S3 C3 CK" # LIANGDUI
]
s = Solution()
for num in nums:
print(s.showDown(num))