[Python] A program that finds the shortest number of steps in a game that crosses clouds

[Python] A program that finds the shortest number of steps in a game that crosses clouds

▼ Question

--A list containing multiple 0s or 1s is given. --0 is an ordinary cloud. 1 is a thunderstorm. --The index number becomes the cloud number. ――If you follow the clouds and reach the last index number, you will reach the goal. --You can jump +1 or +2 when following the clouds. --Avoid thunderstorms. --Find the shortest number of jumps required to reach the goal (you can always reach the goal)

image.png

URL

▼sample input

python


6
0 0 0 0 1 0

▼sample output

python


3

▼my answer

python


def jumpingOnClouds(c):
    i= ans = 0
    while i <= len(c)-2:
        #In the case of one before the final point
        if i+2 == len(c):
            ans += 1
            return ans
            
        #Is it possible to skip two?
        elif c[i+2] != 1:
            i += 2
        else:
            i += 1
        ans += 1
    return ans

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')
    n = int(input())
    c = list(map(int, input().rstrip().split()))
    result = jumpingOnClouds(c)
    fptr.write(str(result) + '\n')
    fptr.close()

** ・ Do not apply an expression to be assigned after return ** ☓ return ans += 1 ◯ return ans

** ・ len (c) ** Find the number of elements. ※1 or more c [len (c)] does not exist.

** ・ "!" ** ! Comes in front ☓ a =! 3 ◯ a != 3

Recommended Posts

[Python] A program that finds the shortest number of steps in a game that crosses clouds
[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 finds the maximum number of toys that can be purchased with your money
[Python] A program that compares the positions of kangaroos.
[Python] A program that calculates the number of updates of the highest and lowest records
Get the number of specific elements in a python list
[Python] A program that finds the most common bird types
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
[Python] A program to find the number of apples and oranges that can be harvested
A function that measures the processing time of a method in python
A program that determines whether a number entered in Python is a prime number
[Python] A program that rotates the contents of the list to the left
Get the number of readers of a treatise on Mendeley in Python
[Python] A program that rounds the score
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
[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
A program that removes duplicate statements in Python
Make a copy of the list in Python
Find the number of days in a month
Output in the form of a python array
A story about creating a program that will increase the number of Instagram followers from 0 to 700 in a week
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
Check if the string is a number in python
How to get the number of digits in Python
Get the size (number of elements) of UnionFind in Python
A shell program that becomes aho in multiples of 3
I made a prime number generation program in Python 2
A reminder about the implementation of recommendations in Python
A python script that gets the number of jobs for a specified condition from indeed.com
How to identify the element with the smallest number of characters in a Python list?
A memo that implements the job of loading a GCS file into BigQuery in Python
A program that summarizes the transaction history csv data of SBI SECURITIES stocks [Python3]
[Python] Representing the number of complaints from life insurance companies in a bar graph
A note that runs an external program in Python and parses the resulting line
A tool that automatically turns the gacha of a social game
Find out the apparent width of a string in python
A general-purpose program that formats Linux command strings in python
Measure the execution result of the program in C ++, Java, Python.
Python --Find out number of groups in the regex expression
I tried "a program that removes duplicate statements in Python"
[Note] Import of a file in the parent directory in Python
[Homology] Count the number of holes in data with Python
A set of script files that do wordcloud in Python3
Find the eigenvalues of a real symmetric matrix in Python
A Python script that compares the contents of two directories
A server that returns the number of people in front of the camera with bottle.py and OpenCV
A program that automatically resizes the iOS app icon to the required image size in Python
Summary of points to keep in mind when writing a program that runs on Python 2.5
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
A Python script that stores 15 years of MLB game data in MySQL in 10 minutes (Baseball Hack!)
I made a program in Python that changes the 1-minute data of FX to an arbitrary time frame (1 hour frame, etc.)
Play a sound in Python assuming that the keyboard is a piano keyboard
Count the number of Thai and Arabic characters well in Python
How to determine the existence of a selenium element in Python
Save the result of the life game as a gif with python
I made a program that solves the spot the difference in seconds