For some reason, evalRow(list(array([0, 1, 0, 0, 0])))they evalRow([0, 1, 0, 0, 0])give different results. However, if I use magicConvert(here to debug this) instead list, to go from a numpy array to a list, it works as expected. Is this a bug in numpy?
def magicConvert(a):
ss = str(list(a))[1:-1]
return map(int, ss.split(","))
from itertools import *
def evalRow(r):
grouped = map(
lambda (v, l): (v, len(tuple(l))),
groupby(chain([2], r, [2])))
result = 0
for player in (1, -1):
for (pre, mid, post) in allTuples(grouped, 3):
if mid[0] == player:
result += player * streakScore(mid[1], (pre[0] == 0) + (post[0] == 0))
return result
def streakScore(size, blanks):
return 0 if blanks == 0 else (
100 ** (size - 1) * (1 if blanks == 1 else 10))
def allTuples(l, size):
return map(lambda i: l[i : i + size], xrange(len(l) - size + 1))
source
share