-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounting Valleys
More file actions
62 lines (33 loc) · 755 Bytes
/
Counting Valleys
File metadata and controls
62 lines (33 loc) · 755 Bytes
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
53
54
55
56
57
58
59
60
61
62
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the countingValleys function below.
def countingValleys(n, s):
c=0
list1=[]
for i in range(0,len(s)):
if s[i]=="D":
list1.append(-1)
elif s[i]=="U":
list1.append(1)
#print(list1)
if list1[0]==-1:
f=-1
elif list1[0]==1:
f=1
for j in range(1,len(list1)):
f=f+list1[j]
#print(f)
if list1[j]==1 and f==0:
c=c+1
return c
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input())
s = input()
result = countingValleys(n, s)
fptr.write(str(result) + '\n')
fptr.close()