Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
stacks solution in Clear category for Flood Area by kurosawa4434
from typing import Iterable
from collections import defaultdict
BS, SL = '\\', '/'
def flood_area(diagram: str) -> Iterable[int]:
flood = defaultdict(int)
stacks = []
for cur, terrain in enumerate(diagram):
if terrain == BS:
stacks.append(cur)
elif terrain == SL and stacks:
first = stacks.pop()
for (f_first, f_last) in list(flood):
if first < f_first < cur:
flood[(first, cur)] += flood.pop((f_first, f_last))
flood[(first, cur)] += cur - first
return flood.values()
if __name__ == '__main__':
print("Example:")
print(flood_area(r'\\//'))
assert list(flood_area(r'\\//')) == [4], 'valley'
assert list(flood_area(r'/\\///\_/\/\\\\/_/\\///__\\\_\\/_\/_/')) == [4, 2, 1, 19, 9], 'mountains'
assert list(flood_area(r'_/-\_')) == [], 'hill'
print("Coding complete? Click 'Check' to earn cool rewards!")
Feb. 21, 2019
Comments: