[Python] A program to find the number of apples and oranges that can be harvested

[Python] A program to find the number of apples and oranges that can be harvested

This is a personal memo.

question

Count the number of apples that have entered the premises of the house (coordinates s ~ t).

image.png

s: The starting point of the house t: End of house a: Position of apple tree b: Position of the orange tree apples: Apple fall points (relative) oranges: Orange fall point (relative)

URL

▼sample input

python


s=2
t=3
a=1
b=5
apples=[-2,2,1]
oranges=[5,8,-2,-1]

▼sample output

python


2
1

▼my answer

python


def countApplesAndOranges(s, t, a, b, apples, oranges):
    yeiledApple = 0
    yeiledOrange = 0

    #apple
    for apple in apples:
        aloc = a + apple
        if s <= aloc and aloc <= t:
            yeiledApple += 1

    #orange
    for orange in oranges:
        oloc = b + orange
        if s <= oloc and oloc <= t:
            yeiledOrange += 1

    print(yeiledApple)
    print(yeiledOrange)
    
if __name__ == '__main__':
    st = input().split()
    s = int(st[0])
    t = int(st[1])
    ab = input().split()
    a = int(ab[0])
    b = int(ab[1])
    mn = input().split()
    m = int(mn[0])
    n = int(mn[1])
    apples = list(map(int, input().rstrip().split()))
    oranges = list(map(int, input().rstrip().split()))

    countApplesAndOranges(s, t, a, b, apples, oranges)

** ・ denotes ** Represents the red region denotes his house.

**-Method output format ** print. Because it ends with method execution. countApplesAndOranges(s, t, a, b, apples, oranges)


The problem is simple, but the questions are long ...

Recommended Posts

[Python] A program to find the number of apples and oranges that can be harvested
[Python] A program that counts the number of valleys
[Python] A program that finds the maximum number of toys that can be purchased with your money
[Python] A program that calculates the number of updates of the highest and lowest records
[Python] A program that rotates the contents of the list to the left
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
Python tricks: a combination of enumerate () and zip (), checking if a string can be converted to a number, sorting the string as a number
[Python] A program that calculates the number of chocolate segments that meet the conditions
Understand the probabilities and statistics that can be used for progress management with a python program
[Python] A program that compares the positions of kangaroos.
Article that can be a human resource who understands and masters the mechanism of API (with Python code)
A script that can perform stress tests according to the number of CPU cores
Write a python program to find the editing distance [python] [Levenshtein distance]
How to start a simple WEB server that can execute cgi of php and python
A story that struggled to handle the Python package of PocketSphinx
From a book that programmers can learn (Python): Find the mode
Try to write a program that abuses the program and sends 100 emails
A script that returns 0, 1 attached to the first Python prime number
[python] A note that started to understand the behavior of matplotlib.pyplot
[Python] A program that rounds the score
The mystery of the number that can be seen just by arranging 1s-The number of repunits and mysterious properties-
Find the white Christmas rate by prefecture with Python and map it to a map of Japan
[Python] A program that finds a pair that can be divided by a specified value
I made a program to check the size of a file in Python
[Python / Jupyter] Translate the comment of the program copied to the clipboard and insert it in a new cell
A story about creating a program that will increase the number of Instagram followers from 0 to 700 in a week
Find the number of days in a month
A discussion of the strengths and weaknesses of Python
[Python] The movement of the decorator that can be understood this time ② The decorator that receives the argument
A solution to the problem that the Python version in Conda cannot be changed
I want to create a priority queue that can be updated in Python (2.7)
How to count the number of elements in Django and output to a template
How to install a Python library that can be used by pharmaceutical companies
I want to exe and distribute a program that resizes images Python3 + pyinstaller
Build a python environment to learn the theory and implementation of deep learning
How to find the coefficient of the trendline that passes through the vertices in Python
I wanted to create a program in Reverse Polish Notation in Python (determining if a string can be converted to a number)
[Python3] Code that can be used when you want to change the extension of an image at once
python beginners tried to predict the number of criminals
How to get the number of digits in Python
Python Note: The mystery of assigning a variable to a variable
[Curse of dimensionality] If the number of sensors is changed to ∞, can anomaly be detected?
A python script that gets the number of jobs for a specified condition from indeed.com
A program that asks for a few kilograms to reach BMI and standard weight [Python]
How to identify the element with the smallest number of characters in a Python list?
Geographic information visualization of R and Python that can be expressed in Power BI
A program that summarizes the transaction history csv data of SBI SECURITIES stocks [Python3]
[Python] Introduction to web scraping | Summary of methods that can be used with webdriver
Find a guideline for the number of processes / threads to set in the application server
A note that runs an external program in Python and parses the resulting line
A mechanism to call a Ruby method from Python that can be done in 200 lines
How to find the first element that matches your criteria in a Python list
Try to find the probability that it is a multiple of 3 and not a multiple of 5 when one is removed from a card with natural numbers 1 to 100 using Ruby and Python.
A server that returns the number of people in front of the camera with bottle.py and OpenCV
I tried to find the entropy of the image with python
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
How to find the optimal number of clusters in k-means
Python --Read data from a numeric data file to find the covariance matrix, eigenvalues, and eigenvectors
Find out the apparent width of a string in python
[python] Change the image file name to a serial number
A library that monitors the life and death of other machines by pinging from Python