Counting Valleys

Photo by Jakob Owens on Unsplash
def countingValleys(steps, path):
sea_level,up,down,valley=0,0,0,0
for step in path:
if step=="U":
up+=1
else:
down+=1
if up==down:
sea_level+=1
if (sea_level!=0) and (step=='U'):
valley+=1
sea_level,up,down=0,0,0
return valley

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store