Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 09ec03d

Browse files
Create 23_counting-valleys.py
1 parent a60cde0 commit 09ec03d

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

‎algorithms/23_counting-valleys.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/python3
2+
3+
import math
4+
import os
5+
import random
6+
import re
7+
import sys
8+
9+
#
10+
# Complete the 'countingValleys' function below.
11+
#
12+
# The function is expected to return an INTEGER.
13+
# The function accepts following parameters:
14+
# 1. INTEGER steps
15+
# 2. STRING path
16+
#
17+
18+
def countingValleys(steps, path):
19+
# Write your code here
20+
level = valley = 0
21+
22+
for i in range(steps):
23+
if path[i] == "U":
24+
level +=1
25+
if level ==0:
26+
valley +=1
27+
else:
28+
level-=1
29+
return valley
30+
31+
if __name__ == '__main__':
32+
fptr = open(os.environ['OUTPUT_PATH'], 'w')
33+
34+
steps = int(input().strip())
35+
36+
path = input()
37+
38+
result = countingValleys(steps, path)
39+
40+
fptr.write(str(result) + '\n')
41+
42+
fptr.close()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /