Remember the way you walked

Subject:
Insert picture description here
Insert picture description here
Code:

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'countingValleys' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
#  1. INTEGER steps
#  2. STRING path
#

def countingValleys(steps, path):
    # Write your code here
    seaLevel = valley = 0

    for step in range(steps):
        if path[step] == 'U':
            seaLevel += 1
        else:
            seaLevel -= 1
        
        if path[step] == 'U' and seaLevel == 0:
            valley += 1
    
    return valley

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    steps = int(input().strip())

    path = input()

    result = countingValleys(steps, path)

    fptr.write(str(result) + '\n')

    fptr.close()

If you think it's good, just like and follow the message~
Thank you for joining us~

Guess you like

Origin blog.csdn.net/BSCHN123/article/details/113591208