此赋值调用另一个函数:
def getPoints(n):
n = (n-1) % 13 + 1
if n == 1:
return [1] + [11]
if 2 <= n <= 10:
return [n]
if 11 <= n <= 13:
return [10]
所以我的任务是把52个数字的列表中所有可能的点加起来。这是我目前的代码。你知道吗
def getPointTotal(aList):
points = []
for i in aList:
points += getPoints(i)
total = sum(points)
return aList, points, total
但是问题是整数1有两个可能的点值,1或11。当我做点的和时,它做的一切都是正确的,但是它把1和11加在一起,而如果整数是1,如果整数是11,我需要它来计算和。你知道吗
例如:
>>>getPointTotal([1,26, 12]) # 10-13 are worth 10 points( and every 13th number that equates to 10-13 using n % 13.
>>>[21,31] # 21 if the value is 1, 31 if the value is 11.
另一个例子:
>>>getPointTotal([1,14]) # 14 is just 14 % 13 = 1 so, 1 and 1.
>>>[2, 12, 22] # 1+1=2, 1+11=12, 11+11=22
我的输出是:
>>>getPointTotal([1,14])
>>>[24] #It's adding all of the numbers 1+1+11+11 = 24.
所以我的问题是,我如何让它分别加上值1和值11,反之亦然。所以当我有1的时候,它会把所有的值和1相加,或者把所有的值和11相加。你知道吗
罗里道尔顿的答案是一个很好的,它有效地给你不同的总数是可能的。我想提供另一种方法,它不一定比那个更好,只是有点不同。我的方法的好处是,您可以看到导致给定总数的分数序列,而不仅仅是最后的总数。你知道吗
这段代码的关键部分是对
itertools.product(*card_scores)
的调用。这将获取从getPoints
获得的输入列表中每个卡的列表,并获取所有组合。所以product([1, 11], [1, 11], [10])
会给出(1, 1, 10)
,(1, 11, 10)
,(11, 1, 10)
,和(11, 11, 10)
。你知道吗对于21点得分来说,这可能有点过分了,因为对于给定的一组牌来说,得分不会有太多变化。但是对于一个不同的问题(即
getPoints
函数的不同实现),它可能非常有趣。你知道吗存储从
getPoints()
返回的所有值时出错。您应该只存储到目前为止返回的点数的可能总数。您可以将所有这些存储在一个集合中,并使用从getPoints()
返回的所有可能值来更新它们。集合将自动删除重复的分数,例如1+11和11+1。您可以将集合更改为末尾的排序列表。这是我的密码:我得到这些结果:
相关问题 更多 >
编程相关推荐