HackerRank Solution: Counting Valleys

Sakshi Singh
1 min readApr 17, 2019

--

(IN PYTHON 3)

import math

import os

import random

import re

import sys

# Complete the countingValleys function below.

def countingValleys(n, s):

c = v = 0

for i in s:

if(c == -1 and i == ‘U’):

v +=1

if(i == ‘D’):

c -=1

else:

c +=1

return v

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()

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Sakshi Singh
Sakshi Singh

No responses yet

Write a response