[Python] A program that counts the number of valleys

[Python] A program that counts the number of valleys

▼ Question

--Start from 0 height --If it is U, it is on 1. If it is D, it goes down by 1. ――The end will always be at a height of 0. --Calculate the number of valleys from the start to the end.

url

▼sample input

python


8
UDDDUDUU

▼sample output

python


1

Image of the road


_/\      _
   \    /
    \/\/

▼my answer

python


def countingValleys(n, s):
    #U and D as 1-Convert to 1
    ss = list(map(int, (s.replace("U","1 ").replace("D","-1 ").split())))

    #Condition to become a valley (0->-Find the number of occurrences of 1)
    x=0
    ans=0
    for i in ss:
        if x==0 and x+i < 0:
            ans+=1
        x += i
    return ans

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

・ Avid hiker avid: enthusiastic hiker: hiker

・ Emeticulously Pay close attention He tracks his hikes meticulously. A detailed record of hikes.

・ Topography terrain Paying close attention to small details like topography.


## Make the for statement one sentence.

▼ my answer (if is a culture)

python


def countingValleys(n, s):
    #U and D as 1-Convert to 1
    ss = list(map(int, (s.replace("U","1 ").replace("D","-1 ").split())))

    #Write if in one sentence
    x = ans=0
    for i in ss:
        ans += 1 if x==0 and x+i < 0 else 0
        x += i
    return ans

[Expression for True] if [Condition] else [Expression for False]


For further simplification, I tried to use the comprehension notation, but I didn't know how to write the comprehension notation when there are multiple expressions in the for statement.

Recommended Posts

[Python] A program that counts the number of valleys
[Python] A program that calculates the number of chocolate segments that meet the conditions
[Python] A program that calculates the number of socks to be paired
[Python] A program that compares the positions of kangaroos.
[Python] A program that calculates the number of updates of the highest and lowest records
[Python] A program that rounds the score
[Python] A program that finds the shortest number of steps in a game that crosses clouds
[Python] A program that rotates the contents of the list to the left
[Python] A program to find the number of apples and oranges that can be harvested
[Python] A program that finds the maximum number of toys that can be purchased with your money
The story of writing a program
Get the number of specific elements in a python list
[Python] A program that finds the most common bird types
A Python script that compares the contents of two directories
A python script that gets the number of jobs for a specified condition from indeed.com
A program that summarizes the transaction history csv data of SBI SECURITIES stocks [Python3]
[Python] A program that creates stairs with #
A story that struggled to handle the Python package of PocketSphinx
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
A function that measures the processing time of a method in python
[Ev3dev] Create a program that captures the LCD (screen) using python
[python] [meta] Is the type of python a type?
A program that determines whether a number entered in Python is a prime number
A script that returns 0, 1 attached to the first Python prime number
Set the process name of the Python program
The story of blackjack A processing (python)
A program that plays rock-paper-scissors using Python
[python] A note that started to understand the behavior of matplotlib.pyplot
The story of making a module that skips mail with python
Get the number of readers of a treatise on Mendeley in Python
Create a compatibility judgment program with the random module of python.
Get the number of searches with a regular expression. SeleniumBasic VBA Python
Check the in-memory bytes of a floating point number float in Python
I made a program to check the size of a file in Python
From a book that makes the programmer's way of thinking interesting (Python)
[Python] Note: A self-made function that finds the area of the normal distribution
Output the number of CPU cores in Python
Get the caller of a function in Python
Calculate the total number of combinations with python
A program that removes duplicate statements in Python
Make a copy of the list in Python
Find the number of days in a month
A program that searches for the same image
A note about the python version of python virtualenv
[Python] A rough understanding of the logging module
Output in the form of a python array
A discussion of the strengths and weaknesses of Python
A shell program that displays the Fibonacci sequence
the zen of Python
A story about creating a program that will increase the number of Instagram followers from 0 to 700 in a week
Around the authentication of PyDrive2, a package that operates Google Drive with Python
Create a BOT that displays the number of infected people in the new corona
[Python] A program that finds the minimum and maximum values without using methods
[Python] A program that calculates the difference between the total numbers on the diagonal line.
A program that sends a fixed amount of mail at a specified time by Python
Check if the string is a number in python
python beginners tried to predict the number of criminals
A story that reduces the effort of operation / maintenance
I made a prime number generation program in Python
How to get the number of digits in Python
Cut a part of the string using a Python slice