【发布时间】:2022-01-15 19:54:01
【问题描述】:
于是我尝试写了两段代码,其中一段试图找出一个代码序列是否为fourz。
fourz 基本上可以查看一张卡片是否有 4 个相同的第一个字符和一个不同的字符。
例如,
fourz(['3S', '3D', '3C', '3H', '5S']) returns True because there are 4 3 and one 5
fourz(['4S', '3D', '3C', '3H', '5S']) returns False because there are only three 3, and one 4 and one 5.
所以现在我有了这个代码:
def is_four_of_a_kind(h):
h = list(h)
values = [i[0] for i in h]
card_order_dict = {'2': 2, '3':3, '4':4, '5': 5, '6':6, '7':7,
'8':8, '9':9,'T':10, 'J':11, 'Q':12, 'K':13, 'A':14 }
count = 1
rank_values = [card_order_dict[i] for i in values]
rank_values = sorted(rank_values)
for i in range(1,len(rank_values)):
if rank_values[i] == rank_values[0]:
count += 1
if count == 4:
return True
else:
return False
当代码是 list 时,此代码正确返回输出,但当它是 元组集
时它不起作用因此,如果我的代码是这样的fourz({('3S', '3D', '3C', '3H', '5S')},我的代码将无法正常工作..
我必须对我的代码进行哪些更改才能使其正常工作?
【问题讨论】: