An avid hiker keeps meticulous records of their hikes. During the last hike that took exactly steps
steps, for every step it was noted if it was an uphill,U
, or a downhill,D
step. Hikes always start and end at sea level, and each step up or down represents a 1 unit change in altitude. We define the following terms:
Given the sequence of up and down steps during a hike, find and print the number of valleys walked through.
def countingValleys(steps, path):
# Write your code here
v = 0 #to check valley
answer = 0
for i in range(steps):
if path[i] == 'U':
v += 1
if v == 0: # if v turns - to 0
answer += 1
else:
v -= 1
return answer
valley를 확인하기 위해 sea level보다 아래에서 sea level과 같아지면 answer에 +1 해준다.