thatoneguy - 1 year ago 43

Python Question

This assignment calls another function:

`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]

So my assignment wants me to add the sum of all possible points from numbers from a list of 52 numbers. This is my code so far.

`def getPointTotal(aList):`

points = []

for i in aList:

points += getPoints(i)

total = sum(points)

return aList, points, total

However the issue is that the integer 1 has two possible point values, 1 or 11. When I do the sum of the points it does everything correctly, but it adds 1 and 11 together whereas I need it to compute the sum if the integer is 1 and if the integer is 11.

So for example:

`>>>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.

Another example:

`>>>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

My output is:

`>>>getPointTotal([1,14])`

>>>[24] #It's adding all of the numbers 1+1+11+11 = 24.

So my question is, is how do I make it add the value 1 separately from the value 11 and vice versa. So that way when I do have 1 it would add all the values and 1 or it would add all the values and 11.

Answer Source

You make a mistake in storing all the values returned from `getPoints()`

. You should store only the possible totals for the points returned so far. You can store all those in a set, and update them with the all the possible values returned from `getPoints()`

. A set will automatically remove duplicate scores, such as 1+11 and 11+1. You can change the set to a sorted list at the end. Here is my code:

```
def getPointTotal(aList):
totals = {0}
for i in aList:
totals = {p + t for p in getPoints(i) for t in totals}
return sorted(list(totals))
```

I get these results:

```
>>> print(getPointTotal([1,26, 12]))
[21, 31]
>>> print(getPointTotal([1,14]))
[2, 12, 22]
```