-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountingvalleys.py
52 lines (40 loc) · 1.29 KB
/
countingvalleys.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/python
import math
import os
import random
import re
import sys
# Complete the countingValleys function below.
def countingValleys(n, s):
#Variables
#level as in altimeter
#valleys num of valleys to return, per the exercise it is any descend below sea level (0) and returning to sea level.
#invalley is a boolean 0=false 1=true to deterime if you are inside a valley
#vl is an array that pulls the individual string charaters so it can be iterated via a loop.
#number of steps is ignore as it is not needed for the exercise
level=0
valleys=0
invalley=0
vl=[char for char in s]
#iterate through valley list
for i in vl:
#first increase or decrease the level based on the step
if i == 'U':
level+=1
elif i == 'D':
level-=1
#determine if you've exited a valley
if level >= 0 and invalley == 1:
valleys+=1
invalley=0
#determine if you are in a valley
elif level < 0 and invalley == 0:
invalley=1
return valleys
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(raw_input())
s = raw_input()
result = countingValleys(n, s)
fptr.write(str(result) + '\n')
fptr.close()