问题描述
在一场经典的德州扑克游戏中,有一种牌型叫做“葫芦”。“葫芦”由五张牌组成,其中包括三张相同牌面值的牌 aa 和另外两张相同牌面值的牌 bb。如果两个人同时拥有“葫芦”,我们会优先比较牌 aa 的大小,若牌 aa 相同则再比较牌 bb 的大小,牌面值的大小规则为:1 (A) > K > Q > J > 10 > 9 > ... > 2,其中 1 (A) 的牌面值为1,K 为13,依此类推。
在这个问题中,我们对“葫芦”增加了一个限制:组成“葫芦”的五张牌牌面值之和不能超过给定的最大值 maxmax。
给定一组牌,你需要找到符合规则的最大的“葫芦”组合,并输出其中三张相同的牌面和两张相同的牌面。如果找不到符合条件的“葫芦”,则输出 “0, 0”。
样例1:
输入:n = 9, max = 34, array = [6, 6, 6, 8, 8, 8, 5, 5, 1]
输出:[8, 5]
说明:array数组中可组成4个葫芦,分别为[6,6,6,8,8],[6,6,6,5,5],[8,8,8,6,6],[8,8,8,5,5]。其中[8,8,8,6,6]的牌面值为36,大于34不符合要求。剩下的3个葫芦的大小关系为[8,8,8,5,5]>[6,6,6,8,8]>[6,6,6,5,5],故返回[8,5]
这个题的几个注意点:
① array的拆分,葫芦的重组(Counter计数器+A的处理(数值映射))
② max的判断条件处理
③ 1(A)的存在,算max的时候牌面值1为最小,但是比较起来确是最大的当作14用
from collections import Counter
def solution(n, max, array):
# 映射牌面值
value_map = {1: 14, 13: 13, 12: 12, 11: 11, 10: 10, 9: 9, 8: 8, 7: 7, 6: 6, 5: 5, 4: 4, 3: 3, 2: 2}
# 将牌面值映射到数值系统中
mapped_array = [value_map[x] for x in array]
counter = Counter(mapped_array) # counter生成一个字典计数器
print(counter[14])
possible_hulus = [] #提取函数有问题
for three in counter:
if counter[three] >= 3: # 如果有四个相同的数,那更能组成hulu。
for two in counter:
if two != three and counter[two] >= 2:
possible_hulus.append((three, two))
valid_hulus = []
for three, two in possible_hulus:
if three == 14:
three=1
if two == 14:
two=1
if 3 * three + 2 * two <= max:
if three==1:
three=14
if two==1:
two=14 #映射回去
valid_hulus.append((three, two))
#选择最大的葫芦组合
if valid_hulus: #按德州扑克规则比较
valid_hulus.sort(key=lambda x:(x[0],x[1]),reverse=True) #对元组按先第一个,后第二个进行排序
max_hulu0=0
max_hulu1=0
if valid_hulus[0][0]==14:
max_hulu0=valid_hulus[0][0]
max_hulu0=1
else:
max_hulu0=valid_hulus[0][0]
if valid_hulus[0][1]==14:
max_hulu1=valid_hulus[0][1]
max_hulu1=1 #映射back
else:
max_hulu1=valid_hulus[0][1]
return [max_hulu0,max_hulu1]
return [0, 0]
if __name__ == "__main__":
# Add your test cases here
print(solution(31,42,[3,3,11,12,12,2,13,5,13,1,13,8,8,1,8,13,12,9,2,11,3,5,8,11,1,11,1,5,4,2,5])==[1,13])