For our lecture, we need to create a program. Now we are at a point where we need help.
Let´s assume we have this following list:
[['W'], ['B'], ['B'], ['W'], ['B'], ['B'], ['B']]
Now, every letter ('W', 'B') has a specific value Just when there are two letters after each other, a player will get the points. Example:
In the list above, player 'W' would not get any point, but Player 'B' would get 5 points. 2 Points since the second element and the third element are after each other, and 3 points since the fifth, the sixth and the seventh element are after each other.
I hope it is clear what i am talking about :)
So, how do i determine, how much elements after each other are of the same value?
Thank you very much!
You can use itertools.groupby
to iterate "groups" of equal elements in a list. Just determine the length of those groups and increase the scores accordingly:
lst = ['W', 'B', 'B', 'W', 'B', 'B', 'B']
scores = {c: 0 for c in set(lst)}
for k, g in itertools.groupby(lst):
n = len(list(g))
if n > 1:
scores[k] += n
(Note: The solution is intentionally incomplete and using a slightly different input.)
Update: Assignment seems to be over; added full code.