我试图从预定的直道手中产生一张潜在的牌'游戏'列表(在这个游戏中,直线被定义为3+张牌 - 例如[3,4,5]
)。困难在于找到一种方法来获取已识别直道的列表(可能包括多个未连接的直道 - ['2D','3D','4D','5D','6D','8D','9D','10D']
)并将它们包含在其中的子直道和附加到播放列表中(对于给定的手,输出理想地为[['2D','3D','4D'],['3D','4D','5D'],['4D','5D','6D'],['2D','3D','4D','5D'],['3D','4D','5D','6D'],['8D','9D','10D']]
)
以下是当前的尝试;
seq = ['1D','2D','3D','4D', '6D', '7D','8D', '10D', '11D', '12D']
plays = []
for card in seq:
ind = seq.index(card)+1
try:
if int(seq[ind][0:len(seq[ind])-1]) - int(card[0:len(card)-1]) == 2:
for num in xrange(len(seq[0:ind])):
if len(seq[0:(ind-num)]) > 3:
plays.append(seq[0:(ind-num)])
plays.append(seq[num+1:ind])
elif len(seq[0:(ind-num)]) == 3:
plays.append(seq[0:(ind-num)])
print plays #debug
except IndexError:
print 'error'
#append from the last appended chunk up until last element?
#arises from final element
[['1D','2D','3D','4D'],['2D','3D','4D'],['1D','2D','3D']]
[['1D','2D','3D','4D'],['2D','3D','4D'],['1D','2D','3D'], ['1D','2D','3D','4D','6D','7D','8D'] , ['2D','3D',' 4D','6D','7D','8D'] , ['1D','2D','3D','4D','6D','7D'] , ['3D','4D','6D','7D','8D'] , ['1D','2D','3D','4D ','6D'], ['4D','6D','7D','8D'] ** , ['1D','2D','3D','4D '],['6D','7D','8D'], ['1D',' 2D','3D'] ]
错误
以粗体显示的输出表示不需要的元素(重复或单独直道的连接)。 感谢您的投入!
(鉴于卡片是一系列的整数) 卡= [1,2,3,4,6,7,8,10,11,12]
def f(cards):
for i in range(len(cards)):
for j in range(i+3, len(cards)+1):
if cards[i:j] == range(cards[i], cards[i]+j-i):
plays.append(cards[i:j])
print plays
答案 0 :(得分:2)
这有帮助吗?
In [34]: def f(cards):
....: return [cards[i:j]
....: for i in range(len(cards))
....: for j in range(i+3, len(cards)+1)
....: if cards[i:j] == range(cards[i], cards[i]+j-i)]
....:
In [35]: f([1, 2, 3, 4, 6, 7, 8, 10, 11, 12])
Out[35]: [[1, 2, 3], [1, 2, 3, 4], [2, 3, 4], [6, 7, 8], [10, 11, 12]]
In [36]: f([2, 3, 4, 5, 6, 8, 9, 10])
Out[36]:
[[2, 3, 4],
[2, 3, 4, 5],
[2, 3, 4, 5, 6],
[3, 4, 5],
[3, 4, 5, 6],
[4, 5, 6],
[8, 9, 10]]
推理:cards[i]
是直道的第一张牌; cards[j-1]
是最后一张牌。 range
返回连续的整数。 j-i
是直线的长度。
答案 1 :(得分:0)
如果你想同时显示排名和套件,这是另一种获取游戏列表的方法。
plays = []
hand = ['AD','2D','3D','4D', '6D', '7D','8D', 'TD', 'JD', 'QD']
ranks = sorted([('-A23456789TJQK'.index(r), r+s) for r, s in hand], reverse = True)
for j in range(3, len(hand)):
for i,r in enumerate(ranks):
rnk = [x[0] for x in ranks[i:i+j]]
if (max(rnk)-min(rnk) == j-1) and len(set(rnk)) == j:
plays.append([x[1] for x in ranks[i:i+j]])
print plays