Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
itertools.accumulate solution in Clear category for Flood Area by David_Jones
from itertools import accumulate
GRADIENTS = {
'\\' : -1,
'_' : 0,
'/' : 1
}
def flood_area(diagram):
levels = (0,) + tuple(accumulate(map(GRADIENTS.get, diagram)))
end = 0
for start, (level, slope) in enumerate(zip(levels, diagram)):
if start >= end and slope == '\\':
try:
end = levels.index(level, start+1)
except ValueError:
continue
yield sum(level-levels[i] for i in range(start+1, end))
June 6, 2019
Comments: