[Python] A program that compares the positions of kangaroos.

[Python] A program that compares the positions of kangaroos. (while statement)

This is a memo for myself.

▼ Question

――Two kangaroos jump over a certain distance. --Starting points are x1 and x2, respectively. --The jump distances from the second time onward are v1, v2 ――If the two animals come to the same position after several jumps, return "YES". If not, return "NO".



▼sample input


x1 = 43
v1 = 2
x2 = 70
v2 = 2

▼sample output



▼my answer


def kangaroo(x1, v1, x2, v2):
    n = 0
    condition = True
    while condition:
        n += 1
        c1 = x1 + v1*n
        c2 = x2 + v2*n

        #Can't catch up forever
        if x1 <= x2 and v1 < v2:
            return "NO"
        elif x1 < x2 and v1 == v2:
            return "NO"
        elif x2 <= x1 and v2 > v1:
            return "NO"
        elif x1 > x2 and v1 == v2:
            return "NO"

        #May catch up
        elif x1 <= x2 and v1 > v2:
            if c1 == c2:
                return "YES"
            elif c1 > c2:
                return "NO"
        elif x2 <= x1 and v2 > v1:
            if c1 == c2:
                return "YES"
            elif c2 > c1:
                return "NO"

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

    x1V1X2V2 = input().split()

    x1 = int(x1V1X2V2[0])

    v1 = int(x1V1X2V2[1])

    x2 = int(x1V1X2V2[2])

    v2 = int(x1V1X2V2[3])

    result = kangaroo(x1, v1, x2, v2)

    fptr.write(result + '\n')


** ・ choreographing ** Choreography You are choreographing a circus show with various animals. Animal choreography circus show

** ・ while **


while conditional expression:

-Repeat the process while the conditional expression is True. ・ Normally, a process that becomes False is inserted. -When ending the return, there is no need to process it as False.

Recommended Posts

[Python] A program that compares the positions of kangaroos.
[Python] A program that counts the number of valleys
A Python script that compares the contents of two directories
[Python] A program that rounds the score
[Python] A program that rotates the contents of the list to the left
[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
The story of writing a program
[Python] A program that finds the most common bird types
[Python] A program that finds the shortest number of steps in a game that crosses clouds
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
A function that measures the processing time of a method in python
[Ev3dev] Create a program that captures the LCD (screen) using python
Set the process name of the Python program
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
[Python] A program to find the number of apples and oranges that can be harvested
Create a compatibility judgment program with the random module of python.
[Python] A program that compares each element of list one by one and wins or loses. zip ()
[Python] A program that finds the maximum number of toys that can be purchased with your money
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
Get the caller of a function in Python
A program that removes duplicate statements in Python
Make a copy of the list in Python
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
[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 story that reduces the effort of operation / maintenance
Cut a part of the string using a Python slice
Make a BOT that shortens the URL of Discord
Python points from the perspective of a C programmer
# Function that returns the character code of a string
Python that merges a lot of excel into one excel
A shell program that becomes aho in multiples of 3
Python program that looks for the same file name
Tasks at the start of a new python project
A story that analyzed the delivery of Nico Nama.
The attitude that programmers should have (The Zen of Python)
A Python program that converts ical data into text
Python Note: The mystery of assigning a variable to a variable
A python script that generates a sample dataset for checking the operation of a classification tree
A program that notifies slack of the operating status of fully automatic botanical photography equipment
A python script that gets the number of jobs for a specified condition from indeed.com
A memo that implements the job of loading a GCS file into BigQuery in Python
[Python / C] I made a device that wirelessly scrolls the screen of a PC remotely.
About the ease of Python
About the features of Python
The Power of Pandas: Python
A tool that automatically turns the gacha of a social game
A Python program in "A book that gently teaches difficult programming"