-3

I'm actually trying to understand if I'm following the right path in a calculation. So there's a team, say Resen FC, with results in a season of 34 weeks as follows;

"3,1,1,1,0,0,3,1,1,3,3,0,0,1,3,1,3,3,1,1,0,0,0,0,3,1,1,1,0,3,3,0,1,1"

These numbers aren't necessarily specified as points received by the team but as representations of;

wins=3, draws=1, and losses=0

If each win was worth 3 points, each draw is 1, and a loss gets nothing, my calculation of this team's overall season record, as of the end of week 34 would be;

results = [3,1,1,1,0,0,3,1,1,3,3,0,0,1,3,1,3,3,1,1,0,0,0,0,3,1,1,1,0,3,3,0,1,1]
print(sum(results))

which gives the output of 44 points.

The team was supposed to be relegated had it got less than 40 points at the end of 34 weeks.

So I have couple of questions at this point:

  1. Is there an alternative way for calculating the results without having to hand-write them in a list as I did above?

  2. (might be tied to the first question) What if the representation of wins, draws and losses remained the same, but the points received for each result had been different?

Example:

 "3,1,1,1,0,0,3,1,1,3,3,0,0,1,3,1,3,3,1,1,0,0,0,0,3,1,1,1,0,3,3,0,1,1"

Each win (3) is worth 5 points,

Each draw(1) is worth 2 points,

Each loss(0) is worth 1 point.

How could I have been able to calculate the team's end-of-season record? Could I have used a function which receives the results as input and do the job for any input matching the format?

Note: This is my first question ever. So I'd be appreciated if you could just consider that before canceling me out :)

Thanks y'all.

goodyonsen
  • 71
  • 4
  • Also see Stack Overflow guidance on [homework](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions). Simply dumping your assignment here is unacceptable. – Prune Jan 25 '21 at 23:20
  • FYI: This is not a homework. – goodyonsen Jan 25 '21 at 23:22
  • 1
    Fine; you can ignore the commentary on school policy. However, the other principles about learning progress still apply. – Prune Jan 25 '21 at 23:24

1 Answers1

0

You can simply convert your comma-separated string to a list by calling split(","), which will split your string by commas.

mystring = "3,1,1,1,0,0,3,1,1,3,3,0,0,1,3,1,3,3,1,1,0,0,0,0,3,1,1,1,0,3,3,0,1,1"
mylist = mystring.split(",")
#['3', '1', '1', '1', '0', '0', '3', '1', '1', '3', '3', '0', '0', '1', '3', '1', '3', '3', '1', '1', '0', '0', '0', '0', '3', '1', '1', '1', '0', '3', '3', '0', '1', '1']

To change the points of wins, draws and losses, you might want to create a dict.

winpoints, drawpoints, losepoints = 5,2,0
scoredict = {3:winpoints, 1:drawpoints, 0:losepoints}

Then, you can generate a new list of points and compute the sum.

newsum = sum([scoredict[int(v)] for v in mylist])
#78

In a regular for loop it would look like this

newlist = []
for v in mylist:
    newlist.append(scoredict[int(v)])
newsum = sum(newlist)
Mitchell Olislagers
  • 1,758
  • 1
  • 4
  • 10
  • Thank you very much Mitch. I got the desired output just fine. I wanted to see if I could convert your newsum into plain for loop but just couldn't manage to. Here's my attempt; I got the last value in the list instead the sums: `for v in mylist: newsum = sum(scoredict[int(v)]) print(newsum)` I'm a rookie and trying to figure out why this conversion didn't work. I'd be appreciated if you could give me a clue. Thanks again sir. – goodyonsen Jan 26 '21 at 18:38
  • I've included a regular for loop in my updated answer. Have a look at what's going and try to figure out how the list comprehension works; they are very helpful and most of all much faster than regular for loops. If the answer has helped you, you can accept my answer. – Mitchell Olislagers Jan 26 '21 at 21:19
  • I'd put all the likes I have if I could but I'm a newbie around. Site doesn't let me. Thank you very much ! – goodyonsen Jan 27 '21 at 08:43