def max_number(x):
"""字符串中最长的数字串, 如果遇到RED可以抵消"""
res, i = 0, 0
tmp = ''
while i < len(x):
if x[i] in '0123456789':
tmp += x[i]
elif x[i:i+3]=='RED':
i += 3
continue
else:
tmp = ''
i += 1
if tmp:
res = max(res, int(tmp))
return res
def solve(array):
"""一个数组中只有0和1,求0和1个数相等的最大连续子序列"""
for i in range(len(array)):
if array[i] == 0:
array[i] = -1
l, r = 0, len(array)-1
while l < r:
print l ,r,array[l:r], sum(array[l:r])
if sum(array[l:r]) == 0:
return r - l
if sum(array[l:r]) > 0:
if array[r] == 1:
r -= 1
else:
l += 1
else:
if array[r] == 0:
r -= 1
else:
l += 1
return
def max_subarray(x, array):
"""求长为x的最大平均子数组"""
tmp, res = 0, 0
for i in range(len(array)-x):
tmp = float(sum(array[i: i+x]))/x
res = max(tmp, res)
return res